【问题标题】:How can I crop a bitmap for ImageView?如何裁剪 ImageView 的位图?
【发布时间】:2013-08-16 10:09:41
【问题描述】:

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

我的图像 1950 像素 宽,需要按照父级的宽度进行裁剪。但是android:scaleType="centerCrop" 不会裁剪图像。我需要在布局中做什么才能仅显示前 400 像素,例如,或者任何屏幕/父宽度是

抱歉,问题很简单 - 尝试用谷歌搜索 - 那里只有复杂的问题。而且我是新人,所以请不要投反对票)

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

标签: java android imageview crop


【解决方案1】:

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

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

干杯:)

【讨论】:

  • 好吧,太好了,现在我需要宽度参数等于父宽度,高度是图像高度,因为我只想裁剪图像的宽度
  • aaaaand width 是 parent 的宽度?:)
  • aaaa我也放了这个:)
  • 所以...一般来说没有办法获得父母的宽度吗?我需要裁剪约 15 张图像,想要制作一种适用于所有人的方法
  • DisplayMetrics sampleSize = getResources().getDisplayMetrics(); :-)
【解决方案2】:

您还可以使用createBitmap 以编程方式裁剪图像。

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

这里 400 是您的宽度和高度,您可以根据需要更改。

【讨论】:

  • 好吧,我想这就是所谓的盲目团队/队友支持,我无法更好地描述它。
【解决方案3】:

设为android:scaleType="fitStart"android:layout_height="400px"

【讨论】:

    【解决方案4】:

    您可以以编程方式执行此操作(这可确保您获得正确的高度/宽度):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 2015-07-08
      • 1970-01-01
      相关资源
      最近更新 更多