Matrix ,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放、平移、旋转等操作。
没专业工具,画的挺难看。解释一下,上面的 sinX 和 cosX ,表示旋转角度的 cos 值和 sin 值,注意,旋转角度是按顺时针方向计算的。 translateX 和 translateY 表示 x 和 y 的平移量。 scale 是缩放的比例, 1 是不变, 2 是表示缩放 1/2 ,这样子。
- public
class MyView extends View { -
-
private Bitmap mBitmap; -
-
private Matrix mMatrix = new Matrix(); -
-
public MyView(Context context) { -
-
super(context); -
-
initialize(); -
-
} -
-
private void initialize() { -
-
mBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap(); -
-
float cosValue = (float) Math.cos(-Math.PI/6); -
-
float sinValue = (float) Math.sin(-Math.PI/6); -
-
mMatrix.setValues( -
-
new float[]{ -
-
cosValue, -sinValue, 100, -
-
sinValue, cosValue, 100, -
-
0, 0, 2}); -
-
} -
-
@Override protected void onDraw(Canvas canvas) { -
- //
super.onDraw(canvas); //当然,如果界面上还有其他元素需要绘制,只需要将这句话写上就行了。 -
-
canvas.drawBitmap(mBitmap, mMatrix, null); -
-
} -
- }
public class MyView extends View {
private Bitmap mBitmap;
private Matrix mMatrix = new Matrix();
public MyView(Context context) {
super(context);
initialize();
}
private void initialize() {
mBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap();
float cosValue = (float) Math.cos(-Math.PI/6);
float sinValue = (float) Math.sin(-Math.PI/6);
mMatrix.setValues(
new float[]{
cosValue, -sinValue, 100,
sinValue, cosValue, 100,
0, 0, 2});
}
@Override protected void onDraw(Canvas canvas) {
// super.onDraw(canvas); //当然,如果界面上还有其他元素需要绘制,只需要将这句话写上就行了。
canvas.drawBitmap(mBitmap, mMatrix, null);
}
}
这里讲的直接赋值的方式也许有点不好理解,不过还好, andrid 提供了对矩阵的更方便的方法,
列方便的接口。
Android的API里都提供了set, post和pre三种操作方式,除了translate,其他三种操作都可以指定中心点。
转30度,然后平移到(100,100)的地方,那么可以这样做:
Matrix m = new Matrix(); m.postRotate(30); m.postTranslate(100, 100);
这样就达到了想要的效果。
,就要这样:
Matrix m = new Matrix(); m.setTranslate(100, 100); m.preRotate(30);
- package
chroya.demo.graphics; -
- import
android.content.Context; - import
android.graphics.Bitmap; - import
android.graphics.Canvas; - import
android.graphics.Matrix; - import
android.graphics.Rect; - import
android.graphics.drawable.BitmapDrawable; - import
android.util.DisplayMetrics; - import
android.view.MotionEvent; - import
android.view.View; -
- public
class MyView extends View { -
-
private Bitmap mBitmap; -
private Matrix mMatrix = new Matrix(); -
-
public MyView(Context context) { -
super(context); -
initialize(); -
} -
-
private void initialize() { -
-
Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap(); -
mBitmap = bmp; -
-
mMatrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight()); -
//平移到(100,100)处 -
mMatrix.postTranslate(100, 100); -
//倾斜x和y轴,以(100,100)为中心。 -
mMatrix.postSkew(0.2f, 0.2f, 100, 100); -
} -
-
@Override protected void onDraw(Canvas canvas) { - //
super.onDraw(canvas); //如果界面上还有其他元素需要绘制,只需要将这句话写上就行了。 -
-
canvas.drawBitmap(mBitmap, mMatrix, null); -
} - }
package chroya.demo.graphics;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
public class MyView extends View {
private Bitmap mBitmap;
private Matrix mMatrix = new Matrix();
public MyView(Context context) {
super(context);
initialize();
}
private void initialize() {
Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.show)).getBitmap();
mBitmap = bmp;
mMatrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight());
//平移到(100,100)处
mMatrix.postTranslate(100, 100);
//倾斜x和y轴,以(100,100)为中心。
mMatrix.postSkew(0.2f, 0.2f, 100, 100);
}
@Override protected void onDraw(Canvas canvas) {
// super.onDraw(canvas); //如果界面上还有其他元素需要绘制,只需要将这句话写上就行了。
canvas.drawBitmap(mBitmap, mMatrix, null);
}
}

原文地址::http://chroya.iteye.com/blog/712078
本文介绍了Android中Matrix类在图像处理中的应用,包括矩阵运算基础、Matrix的构造和变换方法。通过实例展示了如何进行缩放、旋转、平移和倾斜操作,并解释了set、post和pre的不同用法。还提供了围绕特定中心点进行变换的示例代码,帮助读者深入理解Matrix的使用。
1283

被折叠的 条评论
为什么被折叠?



