Java 如何为 ImageView 裁剪位图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18232034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:34:57  来源:igfitidea点击:

How can I crop a bitmap for ImageView?

javaandroidimageviewcrop

提问by user2234594

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

我知道这应该很简单,但android:scaleType="centerCrop"不会裁剪图像

I got image 1950 pixelswide 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

我得到了1950 像素宽的图像,需要按父级的宽度裁剪它。但android:scaleType="centerCrop"不裁剪图像。我需要在布局中做什么才能只显示前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

如果唯一的方法是以编程方式裁剪它 - 请给我一个方法的建议

采纳答案by g00dy

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 :)

干杯:)

回答by Onur A.

Make it android:scaleType="fitStart"and for android:layout_height="400px"

制作它android:scaleType="fitStart"并为android:layout_height="400px"

回答by Niranj Patel

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 是您的宽度和高度,您可以根据您的要求进行更改。

回答by Phil

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);