How can I crop a bitmap for ImageView?

I know this should be simple, but android:scaleType="centerCrop" does not android:scaleType="centerCrop" image

I got an image with a width of 1950 pixels and needed to crop it to the width of the parent. But android:scaleType="centerCrop" does not android:scaleType="centerCrop" image. What do I need to do in the layout to show only the first 400 pixels, for example, or any screen / parent width -

Sorry for the simple question - Google tried this - there are only complicated questions. And I'm new, so don’t take a top-down shot)

 <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 only a way to programmatically trim it - please give me advice using the method

+7
java android imageview crop
source share
4 answers

Ok, I will insert a comment as an 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); } 

Greetings :)

+15
source share

You can also crop the image 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 and height, which you can change to suit your requirements.

+9
source share

Do android:scaleType="fitStart" and android:layout_height="400px"

+1
source share

You can do this programmatically (this ensures that you get the correct 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); 
+1
source share

All Articles