How can I crop a bitmap for ImageView?(如何裁剪 ImageView 的位图?)
问题描述
我知道这应该很简单,但是 android:scaleType="centerCrop"
不会裁剪图像
I know this should be simple , but android:scaleType="centerCrop"
doesn't crop Image
我的图像 1950 像素 宽,需要按照父级的宽度进行裁剪.但是 android:scaleType="centerCrop"
不会裁剪图像.我需要在布局中做什么才能仅显示前 400 像素,例如,或者任何屏幕/父宽度是
I got image 1950 pixels wide 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
抱歉,问题很简单 - 尝试用谷歌搜索 - 那里只有复杂的问题.而且我是新人,所以请不要投反对票)
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
推荐答案
好的,我将把评论粘贴为答案:) ->
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());
然后在代码中:
ImageView iv = (ImageView)v.findViewById(R.id.ver_bottompanelprayer);
if (iv != null){
iv.setImageBitmap(bmp);
}
干杯:)
这篇关于如何裁剪 ImageView 的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何裁剪 ImageView 的位图?


- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 转换 ldap 日期 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01