---
title: "Android drawRegion 绘制"
author: "大器晚成"
date: "2011-05-13T17:15:56Z"
tags: []
categories: ["移动开发"]
slug: "android-drawregion"
draft: false
lastmod: "2011-05-13T17:15:56Z"
id: 1042685
url: "https://snake.iteye.com/blog/1042685"
---

public void drawRegion1(Image img, int x_src, int y_src, int width,
  		   int height, int transform, int x_dest, int y_dest, int anchor)  {
		canvas.save();
		int ix = 0, iy = 0;
		switch (transform) {
		case TRANS_NONE: // 0
			break;
		case TRANS_ROT90: // 90
			canvas.rotate(90, x_dest, y_dest);
			iy = height;
			break;
		case TRANS_ROT180: // 180
			canvas.rotate(180, x_dest, y_dest);
			iy = height;
			ix = width;
			break;
		case TRANS_ROT270: // 270
			canvas.rotate(270, x_dest, y_dest);
			ix = width;
			break;
		case TRANS_MIRROR: // M
			canvas.scale(-1, 1, x_dest, y_dest);// 镜像
			ix = width;
			break;
		case TRANS_MIRROR_ROT90: // M90 j2me<-->android 270
			canvas.scale(-1, 1, x_dest, y_dest);// 镜像
			canvas.rotate(270, x_dest, y_dest);
			ix = width;
			iy = height;
			break;
		case TRANS_MIRROR_ROT180: // M180
			canvas.scale(-1, 1, x_dest, y_dest);// 镜像
			canvas.rotate(180, x_dest, y_dest);
			iy = height;
			break;
		case TRANS_MIRROR_ROT270: // M270 j2me<-->android 90
			canvas.scale(-1, 1, x_dest, y_dest);// 镜像90
			canvas.rotate(90, x_dest, y_dest);
			break;
		}
		canvas.clipRect(x_dest - ix, y_dest - iy, x_dest - ix + width, y_dest
				- iy + height);
		canvas.drawBitmap(img.getBitmap(), x_dest - ix - x_src, y_dest - iy - y_src,
				paint);
		canvas.restore(); 
		
	}
	
	    protected Rect srcRec = new Rect();
		protected RectF dstRec = new RectF();
	    
	    public void drawRegion2(Image src, int x_src, int y_src, int width,
	    		int height, int transform, int x_dest, int y_dest, int anchor) {
	    		// TODO deal with parameter transform
	    		srcRec.left = x_src;
	    		srcRec.top = y_src;
	    		srcRec.right = srcRec.left + width;
	    		srcRec.bottom = srcRec.top + height;
	    		dstRec.left = x_dest;
	    		dstRec.top = y_dest;
	    		dstRec.right = dstRec.left + width;
	    		dstRec.bottom = dstRec.top + height;
	    		this.canvas.drawBitmap(src.getBitmap(), srcRec, dstRec, paint);
	    		}