如何以编程方式将
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);