`

Matrix操作贴图

 
阅读更多
public class MyView extends View {

	//图片的引用
	Bitmap myBitmap;
	Paint paint;
	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		//调用初始化
		initBitmap();
	}

	//初始化图片
	public void initBitmap(){
		paint = new Paint();
		myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		//打开抗锯齿
		paint.setAntiAlias(true);
		paint.setColor(Color.WHITE);
		paint.setTextSize(15);
		//绘制图片
		canvas.drawBitmap(myBitmap, 10, 10, paint);
		//保存画布状态
		canvas.save();
		//创建Matrix对象
		Matrix m1 = new Matrix();
		//平移矩阵
		m1.setTranslate(500, 10);
		Matrix m2 = new Matrix();
		//以一定角度旋转矩阵
		m2.setRotate(15);
		Matrix m3 = new Matrix();
		m3.setConcat(m1, m2);
		//缩放矩阵
		m1.setScale(0.8f, 0.8f);
		m2.setConcat(m3, m1);
		//绘制图片 m2
		canvas.drawBitmap(myBitmap, m2, paint);
		//恢复画布状态,取消所有Matrix
		canvas.restore();
		canvas.save();
		//设置透明度
		paint.setAlpha(180);
		//平移矩阵
		m1.setTranslate(200, 100);
		//缩放矩阵
		m2.setScale(1.3f, 1.3f);
		m3.setConcat(m1, m2);
		//绘制图片 m3
		canvas.drawBitmap(myBitmap, m3, paint);
		//恢复画笔设置
		paint.reset();
		canvas.restore();
		paint.setTextSize(40);
		paint.setColor(0xffFFFFFF);
		canvas.drawText("图片的宽度:" + myBitmap.getWidth(), 20, 220, paint);
		canvas.drawText("图片的高度:" + myBitmap.getHeight(), 150, 220, paint);
		paint.reset();
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics