天天看點

android Matrix 操作

Android中使用Matrix實作圖檔的縮放和旋轉,通過本文學習 ,你将學會如何通過Matrix操作圖像。

Matrix的操作,總共分為translate(平移),rotate(旋轉),scale(縮放)和skew(傾斜)四種,每一種變換在

Android的API裡都提供了set, post和pre三種操作方式,除了translate,其他三種操作都可以指定中心點。

    set是直接設定Matrix的值,每次set一次,整個Matrix的數組都會變掉。

    post是後乘,目前的矩陣乘以參數給出的矩陣。可以連續多次使用post,來完成所需的整個變換。例如,要将一個圖檔旋

轉30度,然後平移到(100,100)的地方,那麼可以這樣做:

< type="application/x-shockwave-flash" width="14" height="15" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=Matrix%20m%20%3D%20new%20Matrix()%3B%0A%0Am.postRotate(30)%3B%0A%0Am.postTranslate(100%2C%20100)%3B%20%20" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">

  1. Matrix m =  new  Matrix();  
  2. m.postRotate(30 );  
  3. m.postTranslate(100 ,  100 );    

[java]  view plain copy

  1. Matrix m = new Matrix();  
  2. m.postRotate(30);  
  3. m.postTranslate(100, 100);    

這樣就達到了想要的效果。

    pre是前乘,參數給出的矩陣乘以目前的矩陣。是以操作是在目前矩陣的最前面發生的。例如上面的例子,如果用pre的話

,就要這樣:

< type="application/x-shockwave-flash" width="14" height="15" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=Matrix%20m%20%3D%20new%20Matrix()%3B%0A%0Am.setTranslate(100%2C%20100)%3B%0A%0Am.preRotate(30)%3B" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">

  1. Matrix m =  new  Matrix();  
  2. m.setTranslate(100 ,  100 );  
  3. m.preRotate(30 );  

[java]  view plain copy

  1. Matrix m = new Matrix();  
  2. m.setTranslate(100, 100);  
  3. m.preRotate(30);  

    旋轉、縮放和傾斜都可以圍繞一個中心點來進行,如果不指定,預設情況下,是圍繞(0,0)點來進行。

  1. package com.eoe android .demo.testcode;
  2. import android.app .Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Matrix;
  6. import android.graphics.drawable.BitmapDrawable;
  7. import android.os.Bundle;
  8. import android.view.View Group.Layout Params;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11. import android.widget.ImageView.ScaleType;
  12. public class bitmaptest extends Activity {
  13. public void onCreate(Bundle icicle) {
  14.         super.onCreate(icicle);
  15.         setTitle("eoeAndroid教程: 縮放和旋轉圖檔 -by:IceskYsl");
  16.         LinearLayout linLayout = new LinearLayout(this);
  17.         // 加載需要操作的圖檔,這裡是eoeAndroid的logo圖檔
  18.         Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
  19.                R.drawable.eoe_android);
  20.         //擷取 這個圖檔的寬和高
  21.         int width = bitmapOrg.getWidth();
  22.         int height = bitmapOrg.getHeight();
  23.         //定義 預轉換成的圖檔的寬度和高度
  24.         int newWidth = 200;
  25.         int newHeight = 200;
  26.         //計算縮放率,新尺寸除原始尺寸
  27.         float scaleWidth = ((float) newWidth) / width;
  28.         float scaleHeight = ((float) newHeight) / height;
  29.         // 建立操作圖檔用的matrix對象
  30.         Matrix matrix = new Matrix();
  31.         // 縮放圖檔動作
  32.         matrix.postScale(scaleWidth, scaleHeight);
  33.         //旋轉圖檔 動作
  34.         matrix.postRotate(45);
  35.         // 建立新的圖檔
  36.         Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
  37.                           width, height, matrix, true);
  38.         //将上面建立的Bitmap轉換成Drawable對象,使得其可以使用在ImageView, ImageButton中
  39.         BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
  40.         //建立一個ImageView
  41.         ImageView imageView = new ImageView(this);
  42.         // 設定 ImageView的圖檔為上面轉換的圖檔
  43.         imageView.setImageDrawable(bmd);
  44.         //将圖檔居中顯示
  45.         imageView.setScaleType(ScaleType.CENTER);
  46.         //将ImageView添加到布局模闆中
  47.         linLayout.addView(imageView,
  48.           new LinearLayout.LayoutParams(
  49.                       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
  50.                 )
  51.         );
  52.         // 設定為本activity 的模闆
  53.         setContentView(linLayout);
  54.     }
  55. }

複制代碼

這裡沒用定義XML 布局模闆,而是直接在代碼中生成了需要的模闆和視圖元件,你可以可以定義XML模闆,其他原理是一樣的。

在遊戲 開發 中,自定義 View 是一個相當重要的功能 ,下面先講一講在View上繪制所需的四個基本主鍵:

Bitmap:用于容納像素點(android .graphics.Bitmap)

Canvas:負責調用繪制方法,是整個過程的入口

要繪制的對象:比如繪制一個Bitmap,矩形或者圓

Paint: 設定 繪制圖形的顔色和樣式

Matrix:它包含一個3x3的矩陣,用于做變換比對(圖像進行中有講),Matrix沒有一個結構體,它必須被初始化,通過實作reset()方法或者set..()方法來實作。

下面來看代碼 :

import android.app .Activity;

import android.content.Context;

import android.content.res.Resources;

import android.os.Bundle;

import android.view.View;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

//import android.graphics.Color;

import android.graphics.Matrix;

import android.graphics.Canvas;

import android.graphics.Paint;

//import android.graphics.Rect;

public class TestMartix extends Activity {

//建立Bitmap,Canvas和Paint

private Bitmap img,r_img;

private Canvas canvas;

private Paint paint;

//由于是自定義view,是以不需要調用Layout 檔案 

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//調用自定義View

setContentView(new MyView(this));

}

public class MyView extends View{

//View的初始化

public MyView(Context context) {

super(context);

//BitmapFactory:從源建立一個Bitmap對象,這些源包括:檔案,流或者數組

img = BitmapFactory.decodeResource(getResources(),R.drawable.img);

//建立一個Matrix對象

Matrix matrix = new Matrix();

//讓矩陣實作翻轉,參數為FLOAT型

matrix.postRotate(90);

//matrix.postRotate(0);

//擷取 Bitmap的高與寬

int width = img.getWidth();

int height = img.getHeight();

//源Bitmap通過一個Matrix變化後,傳回一個不可變的Bitmap

b_img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);

paint = new Paint();

}

//在自定義VIEW時,必須實作此方法

public void onDraw(Canvas canvas){

//在重寫父類的方法時,必須先調用父類的方法

super.onDraw(canvas);

//利用Canvas在View上繪制一個Bitmap,并設定它的樣式和顔色

canvas.drawBitmap(b_img, 10, 10, paint);

//該方法是用來更新View的方法,多與線程結合使用。

//this.invalidate ()

//下面三段代碼用于在View上繪制一個實心矩形,設定顔色為綠色,

//paint.setColor(Color.GREEN);

//paint.setAntiAlias(true);

//canvas.drawRect(new Rect(30,30,100,100), paint);

}

}