简体   繁体   中英

How can I crop a bitmap for ImageView?

I know this should be simple , but android:scaleType="centerCrop" doesn't crop Image

I got image 1950 pixels wide and need it to be cropped by parent's width. But android:scaleType="centerCrop" doesn't crop Image. What do I need to do in layout to show only first 400 pixels , for instance or whatever screen/parent width is

Sorry for simple question - tried to Google it - only complicated questions there. And I'm new, so don't downvote please)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color">

    <ImageView
            android:id="@+id/ver_bottompanelprayer"
            android:layout_width="match_parent"
            android:layout_height="227px"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:scaleType="matrix"

            android:background="@drawable/ver_bottom_panel_tiled_long" />

</RelativeLayout>

if there's only way is to programmaticaly crop it - please give me an advice with a method

Alright, I will paste the comment as answer :) ->

RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl1);

final Options bitmapOptions=new Options();
DisplayMetrics metrics = getResources().getDisplayMetrics();
bitmapOptions.inDensity = metrics.densityDpi;
bitmapOptions.inTargetDensity=1;

/*`final` modifier might be necessary for the Bitmap*/
Bitmap bmp= BitmapFactory.decodeResource(getResources(), R.drawable.ver_bottom_panel_tiled_long, bitmapOptions);
bmp.setDensity(Bitmap.DENSITY_NONE);
bmp = Bitmap.createBitmap(bmp, 0, 0, rl.getWidth(), bmp.getHeight());

Then in the code:

ImageView iv = (ImageView)v.findViewById(R.id.ver_bottompanelprayer);
if (iv != null){
  iv.setImageBitmap(bmp);
}

Cheers :)

You can also crop image by programmatically using createBitmap .

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
bm = Bitmap.createBitmap(bm, 0, 0, 400, 400);
your_imageview.setImageBitmap(bm);

Here 400 is your width & height you can change as per your requirement.

让它成为android:scaleType="fitStart" ,对于android:layout_height="400px"

You can do this programmatically (this ensures you get the right height/width):

ImageView image = (ImageView) findVieById(R.id.ver_bottompanelprayer);
DisplayMetrics dm = getResources().getDisplayMetrics();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(1950, dm.heightPixels);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
image.setLayoutParams(params);
image.setScaleType(ScaleType.FIT_XY);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM