1日得到屏幕设备高度和宽度 第二缩放图像到图像的三分之一值,并重新分配给它
检查这个代码
main.xml中
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
这里是MainActivity.java文件
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.Display;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img[] = new ImageView[9];
img[0] = (ImageView) findViewById(R.id.imageView1);
img[1] = (ImageView) findViewById(R.id.imageView2);
img[2] = (ImageView) findViewById(R.id.imageView3);
img[3] = (ImageView) findViewById(R.id.imageView4);
img[4] = (ImageView) findViewById(R.id.imageView5);
img[5] = (ImageView) findViewById(R.id.imageView6);
img[6] = (ImageView) findViewById(R.id.imageView7);
img[7] = (ImageView) findViewById(R.id.imageView8);
img[8] = (ImageView) findViewById(R.id.imageView9);
Display mDisplay= getWindowManager().getDefaultDisplay();
int Devicewidth= mDisplay.getWidth();
int DeviceHeight= mDisplay.getHeight();
for(int i=0; i<9 ; i++){
//You can use different images here since i have only one I have used only one
Bitmap oldBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap newBitmap = getResizedBitmap(oldBitmap, DeviceHeight/3, Devicewidth/3);
img[i].setImageBitmap(newBitmap);
}
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
}
这可能会工作,但我不愿意使用任何涉及Java代码,因为布局是静态的,因此可以完全在XML中完成。 – 2013-04-08 07:10:46
@CalvinLi:java代码只是为了安全目的,如果你不使用java代码,只使用我在这里写的XML,那么它仍然可以工作。但请记住所有9张图片的尺寸应该相同。请先检查一下 – 2013-04-08 08:49:18