简体   繁体   English

如何为ImageView裁剪位图?

[英]How can I crop a bitmap for ImageView?

I know this should be simple , but android:scaleType="centerCrop" doesn't crop Image 我知道这应该很简单,但android:scaleType="centerCrop"不会裁剪图像

I got image 1950 pixels wide and need it to be cropped by parent's width. 我得到1950像素宽的图像,需要按父母的宽度裁剪。 But android:scaleType="centerCrop" doesn't crop Image. android:scaleType="centerCrop"不会裁剪图像。 What do I need to do in layout to show only first 400 pixels , for instance or whatever screen/parent width is 我需要在布局中做什么才能显示前400个像素 ,例如或任何屏幕/父宽度

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 如果有唯一的方法是programmaticaly裁剪它 - 请给我一个方法的建议

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 . 您还可以使用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. 这里400是您的宽度和高度,您可以根据您的要求更改。

让它成为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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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