微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android:如何以编程方式将ImageView的宽度设置在TableRow中

如何以编程方式将 ImageView的宽度设置为TableRow?我不希望单元格的宽度改变,只有单元格中的图像的宽度.

布局:(省略号可以减少代码)

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
<TableRow android:gravity="center_vertical">
<Button>...</Button>
<ImageView
    android:id="@+id/fruit"
    android:layout_width="120dp"
    android:layout_height="44dp"
    android:src="@drawable/apple"
    android:scaleType="matrix"
></ImageView>
<Button>...</Button>
</TableRow>
<TableRow>...</TableRow>
</TableLayout>

Java的:

ImageView iv = (ImageView) findViewById(R.id.fruit);
LayoutParams frame = iv.getLayoutParams();
frame.width = 100;
iv.setLayoutParams(frame);

编辑:我想要将图像裁剪到我设置的边界内,并将表格单元格作为原始宽度.下面是代码示例,为我工作,感谢parag的建议:

Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.apple); 
int width = 50;
int height = 44; 
Bitmap resizedbitmap = Bitmap.createBitmap(bmp,iv.getScrollX(),iv.getScrollY(),width,height);
iv.setimageBitmap(resizedbitmap);

解决方法

你调整位图的另一种方式
ImageView img=(ImageView)findViewById(R.id.ImageView01);
    Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.sc01);
    int width=200;
    int height=200;
    Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp,height,true);
    img.setimageBitmap(resizedbitmap);

原文地址:https://www.jb51.cc/android/312177.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐