android 触摸变形,Android变形(Transform)之Matrix用法

x' = x + 100;

y' = y - 100;

也即在原始的基础上右移100,上移100,单位为像素。第三列第三行为2,表示为以前比例的1/2,记住这块容易弄错。

下面给出具体坐标对应变形的属性

|scaleX, skewX, translateX|

|skewY, scaleY, translateY|

|0       ,0        , scale       |

实践

通过代码来看看具体的用法

public class MatrixTransformView extends View {

private Matrix mMatrix;

private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

private Bitmap mBitmap;

public MatrixTransformView(Context context) {

super(context);

}

public MatrixTransformView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public void setDrawable(int resId) {

mBitmap = BitmapFactory.decodeResource(getContext().getResources(), resId);

}

/*

* 设置矩阵,并重绘

*/

public void setMatrixValues(float[] array) {

if (mMatrix == null) {

mMatrix = new Matrix();

}

mMatrix.reset();

mMatrix.setValues(array);

invalidate();

}

public void resetMatrix() {

if (mMatrix != null) {

mMatrix.reset();

}

invalidate();

}

@Override

protected void onDraw(Canvas canvas) {

if (mMatrix != null) {

Paint paint = mPaint;

canvas.drawBitmap(mBitmap, mMatrix, paint);

}

super.onDraw(canvas);

}

}

通过Matrix的setValues方法,将3*3的矩阵坐标值进行设置即可。

强调的一点是,在调用setMatrixValues的时候需要调用invalidate方法,让View进行调用onDraw进行重绘。

矩阵的基本用法就是这些,往往在开发过程中,不直接通过矩阵坐标去实现变形,因为如果要实现选择,那么就比较复杂了,涉及到三角函数,对于数据早已经忘差不多的人,很是痛苦,当然如果非要用的话,算起来也不难。

那么为了避免直接使用矩阵坐标来操作变形,Matrix类提供方法来进行变:

set方式:setScale, setSkew, setTranslate, setRotate

post方式:postScale, postSkew, postTranslate, postRotate

pre方式:preScale, preSkew, preTranslate, preRotate

set方式为直接设置,每一次调用set方法都会先重置矩阵。post可以理解成设置多次有效,效果是累加的。pre这里暂且理解成和post方式完全一样,后面3D的时候再纠结。

看代码:

public class MatrixTransformView extends View {

private Matrix mMatrix;

private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

private Bitmap mBitmap;

public MatrixTransformView(Context context) {

super(context);

}

public MatrixTransformView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public void setDrawable(int resId) {

mBitmap = BitmapFactory.decodeResource(getContext().getResources(), resId);

}

/*

* 设置矩阵,并重绘

*/

public void setMatrixValues(float[] array) {

if (mMatrix == null) {

mMatrix = new Matrix();

}

mMatrix.reset();

mMatrix.setValues(array);

invalidate();

}

public void postMatrixScale(float scaleX, float scaleY, float centerX, float centerY) {

if (mMatrix == null) {

mMatrix = new Matrix();

}

mMatrix.preScale(scaleX, scaleY, centerX, centerY);

invalidate();

}

public void postMatrixSkew(float skewX, float skewY, float centerX, float centerY) {

if (mMatrix == null) {

mMatrix = new Matrix();

}

mMatrix.postSkew(skewX, skewY, centerX, centerY);

invalidate();

}

public void postMatrixTranslate(float translateX, float translateY) {

if (mMatrix == null) {

mMatrix = new Matrix();

}

mMatrix.postTranslate(translateX, translateY);

invalidate();

}

public void postMatrixRotate(float degree, float centerX, float centerY) {

if (mMatrix == null) {

mMatrix = new Matrix();

}

mMatrix.postRotate(degree, centerX, centerY);

invalidate();

}

public void resetMatrix() {

if (mMatrix != null) {

mMatrix.reset();

}

invalidate();

}

@Override

protected void onDraw(Canvas canvas) {

if (mMatrix != null) {

Paint paint = mPaint;

canvas.drawBitmap(mBitmap, mMatrix, paint);

}

super.onDraw(canvas);

}

}

Matrix的基本用法就这么多。

扩展

变形是需要canvas来进行绘制的,canvas的绘制需要bitmap,所以这块利用一个继承自View的控件,通过setDrawable方式设置bitmap,那么选择目标必须是个bitmap,在文章的demo中,通过参数为int型resource的setDrawable方法进行bitmap获取,如果想对别的控件进行变形,例如ViewGroup,可以通过如下方式:

Matrix m = new Matrix();

m.setValues(new float[] {

1, 0, 0,

0, 1, 0,

0, 0, 1

});

Bitmap bp = Bitmap.createBitmap(viewGroup.getWidth(), viewGroup.getHeight(), Bitmap.Config.RGB_565);

Canvas can = new Canvas(bp);

viewGroup.draw(can);

bp = Bitmap.createBitmap(bp, 0, 0, bp.getWidth(), bp.getHeight(), m, true);

img.setImageBitmap(bp);

通过将ViewGroup转换成Bitmap,然后自定义一个Image来变形,隐藏ViewGroup来达到效果。

疑问

1.如果谁知道post,pre的区别,请告诉我下,看看我的理解是否正确。

2.能否实现ViewGroup直接变形,而非我上面讲的那种。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值