·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> Android开发 >> Android中使用Matrix控制图形变换和制作倒影效果的方法

Android中使用Matrix控制图形变换和制作倒影效果的方法

作者:佚名      Android开发编辑:admin      更新时间:2022-07-23

最近在使用Matrix进行绘图的操作。对Matrix的一些方法有了一些更深的体会,记下来,以便日后复习。
Matrix常用的方法:

一、变换方法:

Matrix提供了translate(平移)、rotate(旋转)、scale(缩放)、skew(倾斜)四种操作,这四种操作的内部实现过程都是通过matrix.setValues(…)来设置矩阵的值来达到变换图片的效果。
Matrix的每种操作都有set、pre、post三种操作,set是清空队列再添加,pre是在队列最前面插入,post是在队列最后面插入。
pre方法表示矩阵前乘,例如:变换矩阵为A,原始矩阵为B,pre方法的含义即是A*B
post方法表示矩阵后乘,例如:变换矩阵为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°(rotate)之后四个点的坐标。(你可以尝试着自己计算,你会发现很复杂,还不精确)

需要注意的是,matrix的某些方法使用到中心点的时候,如果不设置,默认是以(0,0)为中心点的。

记下来,以免忘记。


三、制作倒影效果
利用matrix可以实现各种图片的特效,接下来就用marix加上渐变色实现图片倒影的效果,步骤如下:
1. 获取需要倒影效果的图片,这里取原图片的一半
2. 添加颜色渐变到倒影图片上
具体的实现如下面代码所述,我们以一种自定义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;
  }
 
}

实现的效果如下图:

2016415152957303.png (720×1280)