Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 中使用 Matrix 控制圖形變換並制作倒影效果

編輯:關於 Android 程式設計

近來頻繁使用 Matrix 進行繪圖操作,對它的一些方法有更深體會,特此記錄以便日後復習。

一、變換方法

Matrix 提供了 translate(平移)、rotate(旋轉)、scale(縮放)、skew(傾斜)四種操作,其內部都是通過 matrix.setValues(…) 設置矩陣值來達到變換效果。

每種操作都有 set、pre、post 三種方式:set 是清空隊列再添加,pre 是在隊列最前面插入,post 是在隊列最後面插入。pre 表示矩陣前乘(A*B),post 表示矩陣後乘(B*A)。

例如:

 1. matrix.preScale(0.5f, 1); 2. matrix.preTranslate(10, 0); 3. matrix.postScale(0.7f, 1); 4. matrix.postTranslate(15, 0); 等價於: translate(10, 0) -> scale(0.5f, 1) -> scale(0.7f, 1) -> translate(15, 0) 注意:後調用的 pre 操作先執行,而後調用的 post 操作則後執行。

set 方法一旦調用即會清空之前 matrix 中的所有變換。例如:

 1. matrix.preScale(0.5f, 1); 2. matrix.setScale(1, 0.6f); 3. matrix.postScale(0.7f, 1); 4. matrix.preTranslate(15, 0); 等價於 translate(15, 0) -> scale(1, 0.6f) -> scale(0.7f, 1) matrix.preScale(0.5f, 1) 將不起作用。

二、映射方法

Matrix 提供了 mapXXX 系列方法,用於獲取經 matrix 映射之後的值,主要有 mapPoints、mapRects、mapVectors 等。它們的用武之地在於:當你需要記住 matrix 操作之後的數值時,比如記住矩形旋轉 34° 後四個點的坐標(手動計算既複雜又不精確)。

需特別注意,matrix 某些方法在用到中心點時,若不設置,默認是以(0,0)為中心點的。

三、制作倒影效果

利用 matrix 可以實現各種圖片特效,接下來就用 matrix 加上漸變色實現圖片倒影。步驟為:先獲取需要倒影的圖片(這裡取原圖的一半),再為倒影圖片添加顏色漸變。以自定義 View 形式實現如下:

 package com.flection.view; import com.flection.main.R; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuffXfermode; import android.graphics.Shader.TileMode; import android.graphics.drawable.BitmapDrawable; import android.util.AttributeSet; import android.view.View; public class FlectionView extends View { Context mContext=null; public FlectionView(Context context) { super(context); } public FlectionView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext=context; } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { this.setBackgroundColor(Color.parseColor("#8B8378")); Bitmap oldBitmap = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.dropbox); Bitmap newBitmap = createFlectionBitmap(oldBitmap); canvas.drawBitmap(newBitmap,newBitmap.getWidth() ,newBitmap.getHeight(), new Paint()); this.invalidate(); } // 獲取原圖+倒影圖的 bitmap private Bitmap createFlectionBitmap(Bitmap oldBitmap) { int mWidth = oldBitmap.getWidth(); int mHeight = oldBitmap.getHeight(); int gap = 2; Matrix matrix = new Matrix(); matrix.preScale(1, -1); Bitmap flection = Bitmap.createBitmap(oldBitmap, 0, mHeight / 2, mWidth, mHeight / 2, matrix, false); Bitmap background = Bitmap.createBitmap(mWidth, mHeight+gap+mHeight/2, Config.ARGB_8888); Canvas canvas = new Canvas(background); Paint p1 = new Paint(); canvas.drawBitmap(oldBitmap, 0, 0, p1); canvas.drawBitmap(flection, 0, mHeight+gap, p1); Paint shaderPaint = new Paint(); LinearGradient shader = new LinearGradient(0, mHeight, 0, flection.getHeight(), 0x70ffffff, 0x00ffffff, TileMode.MIRROR); shaderPaint.setShader(shader); shaderPaint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN)); canvas.drawRect(0, mHeight+gap, mWidth, background.getHeight(), shaderPaint); return background; } }

實現效果如下圖:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved