Я попытался изменить размер моего ImageView
но он не работал ни по растровому изображению, ни по другим методам. Я собираюсь разместить свой код, поэтому, если кто-нибудь может мне помочь, как по размеру ImageView
поместиться внутри строки таблицы thnx.
public class Test extends Activity
{
private TableLayout Table1;
protected void onCreate(Bundle savedInstanceState)
{
int[] ImageArray={R.raw.hospital_image,R.raw.hotel_image};
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites);
Table1 = (TableLayout) findViewById(R.id.Table);
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
for(int i=0;i<ImageArray.length;i++)
{
TableRow TR = new TableRow(this);
TR.setLayoutParams(tableRowParams);
ImageView img=new ImageView(this);
//What should i do here to resize it ???
Drawable d = getResources().getDrawable(ImageArray[i]);
img.setImageDrawable(d);
TR.addView(img, new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
Table1.addView(TR);
}
}
Мой XMl содержит только следующие:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TableLayout
android:id="@+id/Table1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</RelativeLayout>
попробуй это,
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ResizableImageView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d == null) {
super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
return;
}
int imageHeight = d.getIntrinsicHeight();
int imageWidth = d.getIntrinsicWidth();
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
float imageRatio = 0.0F;
if (imageHeight > 0) {
imageRatio = imageWidth / imageHeight;
}
float sizeRatio = 0.0F;
if (heightSize > 0) {
sizeRatio = widthSize / heightSize;
}
int width;
int height;
if (imageRatio >= sizeRatio) {
// set width to maximum allowed
width = widthSize;
// scale height
height = width * imageHeight / imageWidth;
} else {
// set height to maximum allowed
height = heightSize;
// scale width
width = height * imageWidth / imageHeight;
}
setMeasuredDimension(width, height);
}
}
используйте его в своем макете, поскольку вы бы просто ImageView
, просто измените тег, например,
<com.example.ResizableImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:padding="1px"
android:scaleType="fitCenter"
android:src="..." />
setLayoutParams()
должен сделать трюк, в вашем случае я считаю, что вам понадобится TableRow.LayoutParams
, например:
ImageView img = new ImageView(this);
img.setLayoutParams(new TableRow.LayoutParams(w, h));
редактировать
Попробуйте установить Drawable
перед setLayoutParams
:
ImageView img = new ImageView(this);
Drawable d = getResources().getDrawable(ImageArray[i]);
img.setImageDrawable(d);
img.setLayoutParams(new TableRow.LayoutParams(w, h));
Drawable
перед LayoutParams
и попробуйте