最近在使用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,mHeight / 2,mWIDth,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,p1); //画出倒影图 canvas.drawBitmap(flection,mHeight+gap,p1); Paint shaderPaint = new Paint(); linearGradIEnt shader = new linearGradIEnt(0,mHeight,flection.getHeight(),0x70ffffff,0x00ffffff,TileMode.MIRROR); shaderPaint.setShader(shader); shaderPaint.setXfermode(new PorterDuffXfermode(androID.graphics.PorterDuff.Mode.DST_IN)); //画出渐变颜色 canvas.drawRect(0,background.getHeight(),shaderPaint); return background; } }
实现的效果如下图:
以上是内存溢出为你收集整理的Android中使用Matrix控制图形变换和制作倒影效果的方法全部内容,希望文章能够帮你解决Android中使用Matrix控制图形变换和制作倒影效果的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)