Ricardo Cristian Ramirez
Ricardo Cristian Ramirez

Reputation: 1234

Cropping a bitmap considering display size

I have a large image (1024x1024) and want to crop it in order to fit screen correctly considering the display size (e.g. 480x800). However, as seen below, it doesn't fit. How can I fit it?

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/bgview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        Point size = new Point();
        Display display = getWindowManager().getDefaultDisplay();
        display.getSize(size);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.sample);
        Bitmap bmp2 = Bitmap.createBitmap(bmp, 0, 0, size.x, size.y);
        ImageView bg = (ImageView) findViewById(R.id.bgview);

        bg.setImageBitmap(bmp2);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Current view Background image

Upvotes: 1

Views: 118

Answers (2)

ejohansson
ejohansson

Reputation: 2882

I suggest you use:

Example from their site:

Picasso.with(context)
    .load(url)
    .fit()
    .centerCrop()
    .into(imageView);

Play around with it and set your android:layout_width="match_parent" to fill the screen width

Upvotes: 1

Quark
Quark

Reputation: 412

In your ImageView tag add this: android:scaleType="centerCrop" You can also try android:scaleType="fitXY"

Upvotes: 1

Related Questions