2012年3月18日 星期日

android -- image (Matrix)


******************************************
縮小圖:

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        view = (ImageView) findViewById(R.id.imageView1);
       
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.butterfly);
        bmpWidth = bitmap.getWidth();
        bmpHeight = bitmap.getHeight();
        scaleOriginal = 1;
        loadImg();

}



private void loadImg(){
     Matrix matrix = new Matrix();
     scaleOriginal *= 0.5;
     matrix.postScale(scaleOriginal, scaleOriginal);
     Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
     view.setImageBitmap(resizedBitmap);
    }


**********************************************************************

 DisplayMetrics dm = new DisplayMetrics();
     
        //取得手機解析度
        getWindowManager().getDefaultDisplay().getMetrics(dm);
     
        //設定顯示寬高
        dispalyWidth = dm.widthPixels;
        displayHeight = dm.heightPixels - 80;
     
     
        //linearLayout = (LinearLayout)findViewById(R.id.linearLayout01);
*******************************************************************


DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
int statusBarHeight;
switch (displayMetrics.densityDpi) {
    case DisplayMetrics.DENSITY_HIGH:
        statusBarHeight = HIGH_DPI_STATUS_BAR_HEIGHT;
        break;
    case DisplayMetrics.DENSITY_MEDIUM:
        statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
        break;
    case DisplayMetrics.DENSITY_LOW:
        statusBarHeight = LOW_DPI_STATUS_BAR_HEIGHT;
        break;
    default:
        statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT;
}

*************************************************




//縮放圖片
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);

//重新縮放 Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);

沒有留言:

張貼留言