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