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

所属分类: 软件编程 / Android 阅读数: 201
收藏 0 赞 0 分享

最近在使用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)

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多