如何为ImageView裁剪位图?

8

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

我的图片宽度为1950个像素,需要按父级的宽度进行裁剪。但是android:scaleType="centerCrop"不能裁剪图片。我需要在布局中做什么才能只显示前400个像素或屏幕/父级宽度的任何部分?

抱歉,问题很简单 - 尝试在Google上搜索 - 只有复杂的问题。而且我是新手,请不要贬低我)

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

如果唯一的方法是通过编程来裁剪它,请给我建议和方法。


你能这样做吗:首先 Bitmap bmp= BitmapFactory.decodeResource(getResources(), R.drawable.ver_bottom_panel_tiled_long); 然后 bmp = Bitmap.createBitmap(bmp, 0, 0, width, height); <- 在此处粘贴所需的宽度和高度。然后通过编程方式 ImageView iv = (ImageView)v.findViewById(R.id.ver_bottompanelprayer); if (iv != null) { iv.setImageBitmap(bmp); } - g00dy
4个回答

18

好的,我会将评论粘贴为答案 :) ->

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

然后在代码中:

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

干杯 :)


好的,太棒了,现在我需要宽度参数等于父元素的宽度,高度等于图像的高度,因为我只想裁剪图像的宽度。 - user2234594
我也把这个放进去了 :) - g00dy
好的,rl的宽度和高度都是0,但这不是最糟糕的问题 - bm的高度为170,尽管它实际上有227个像素高。我试图以多种方式避免这个问题,但在某些情况下,Android会缩小此图像。例如,如果我将其用作ImageView的源,则它为227个像素。但是,如果我将其放入bitmap.xml中,它将因无法解释的原因变为170个像素。但我希望它确切地为227个像素。 - user2234594
让我检查一下,这看起来真的很奇怪。在我看来,缩小似乎是由于设备的密度而发生的。我将在一秒钟内使用正确的缩小比例更新答案。 - g00dy
1
通过 DisplayMetrics sampleSize = getResources().getDisplayMetrics(); :-) - g00dy
显示剩余13条评论

10

您还可以通过编程使用createBitmap来裁剪图像。

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

这里的400是你的宽度和高度,你可以根据自己的需求进行更改。


我想这被称为盲目的团队/队友支持,我无法更好地描述它。 - g00dy

1
您可以通过编程来实现此操作(这可以确保您获得正确的高度/宽度):
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

将其设置为android:scaleType="fitStart",并为android:layout_height="400px"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接