/*
* translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种*/
public class MyView extends View{
private Matrix matrix=new Matrix();
private Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.hello);
private float width=bitmap.getWidth();
private float heigth=bitmap.getHeight();
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.translate(240.0f, 400.0f);//画布移动
// canvas.rotate(50);//画布旋转
// canvas.scale(2, 2, 240.0f, 400.0f);
// matrix.postRotate(30);//绕原点旋转30度
// matrix.postRotate(30, width/2, heigth/2);//绕某个点旋转30度,这里选择的原点是图片的中心点
// matrix.postScale(2, 1);//两个参数为缩放比例。按比例缩放,宽为原来的2倍,1为正常所以高不变,但参考点事坐标原点
// matrix.postTranslate(240-width/2, 400-heigth/2);//参考点为坐标原点(0,0)移动到(240-width/2,400-heigth/2)
// matrix.postScale(2, 2, 240-width/2,400-heigth/2);//以 (240-width/2,400-heigth/2)为缩放中心
// matrix.postSkew(0.2f, 0.2f);//参数为x,y轴倾斜角度
// matrix.postScale(2.0f, 2.0f);
matrix.postTranslate(100, 100);
// matrix.postTranslate(100, 100);
matrix.postTranslate(-100, -100);
canvas.drawBitmap(bitmap, matrix, null);
/*canvas.scale(2, 2, 240.0f, 400.0f);的原型为如下:
* scale(float sx, float sy, float px, float py){
translate(px,py);
scale(sx,sy);
translate(-px,-py);
}*/
}
}
简单的学习Matrix的缩放,平移,旋转,倾斜
最新推荐文章于 2024-06-01 10:14:18 发布