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">
- Matrix m = new Matrix();
- m.postRotate(30 );
- m.postTranslate(100 , 100 );
Matrix m = new Matrix();
m.postRotate(30);
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">
- Matrix m = new Matrix();
- m.setTranslate(100 , 100 );
- m.preRotate(30 );
Matrix m = new Matrix();
m.setTranslate(100, 100);
m.preRotate(30);
旋轉、縮放和傾斜都可以圍繞一個中心點來進行,如果不指定,預設情況下,是圍繞(0,0)點來進行。
- package com.eoe android .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.View Group.Layout Params;
- 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);
- }
- }
複制代碼
這裡沒用定義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);
}
}
}