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);
}
}
}