由於android軟體的顯示控制項上要求長度超過一定的數值就截取並加省略符號,所以寫了這個方法:
package com.eoeandroid.demo.testcode; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ImageView.ScaleType; public class bitmaptest extends Activity { public void onCreate(Bundle icicle) { super.onCreate(icicle); setTitle("eoeAndroid教程: 縮放和旋轉圖片 -by:IceskYsl"); LinearLayout linLayout = new LinearLayout(this); // 載入需要操作的圖片,這裡是eoeAndroid的logo圖片 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.eoe_android); //擷取這個圖片的寬和高 int width = bitmapOrg.getWidth(); int height = bitmapOrg.getHeight(); //定義預轉換成的圖片的寬度和高度 int newWidth = 200; int newHeight = 200; //計算縮放率,新尺寸除原始大小 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 建立操作圖片用的matrix對象 Matrix matrix = new Matrix(); // 縮放圖片動作 matrix.postScale(scaleWidth, scaleHeight); //旋轉圖片 動作 matrix.postRotate(45); // 建立新的圖片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); //將上面建立的Bitmap轉換成Drawable對象,使得其可以使用在ImageView, ImageButton中 BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); //建立一個ImageView ImageView imageView = new ImageView(this); // 設定ImageView的圖片為上面轉換的圖片 imageView.setImageDrawable(bmd); //將圖片置中顯示 imageView.setScaleType(ScaleType.CENTER); //將ImageView添加到布局模板中 linLayout.addView(imageView, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT ) ); // 設定為本activity的模板 setContentView(linLayout); } }