NinePatch 9Patch 绘制

public class NinePath extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(new NinePathView(this));
	}
}

class NinePathView extends SurfaceView implements Callback,Runnable{
	private SurfaceHolder mSurfaceHolder;
	private Thread mThread;
	private Canvas mCanvas;
	private boolean mFlag;
	private Paint mPaint;
	private Bitmap mBitmap;
	private Bitmap mBitmap9Path;
	private NinePatch mNinePath;
	
	public NinePathView(Context context) {
		super(context);
		mSurfaceHolder = this.getHolder();
		mSurfaceHolder.addCallback(this);
		mPaint = new Paint();
		mPaint.setColor(Color.WHITE);
		mPaint.setAntiAlias(true);
		mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.iamge_old);
		mBitmap9Path = BitmapFactory.decodeResource(getResources(), R.drawable.nine_image);
		mNinePath = new NinePatch(mBitmap9Path, mBitmap9Path.getNinePatchChunk(), null);
		
		setFocusable(true);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		
		mFlag=true;
		mThread=new Thread(this,"My Thread");
		mThread.start();
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		mFlag=false;
	}

	@Override
	public void run() {
		while (mFlag) {
			long start=System.currentTimeMillis();
			myDraw();
			logic();
			long end=System.currentTimeMillis();
			if ((end-start)<50) {
				try {
					Thread.sleep(50-(end-start));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	private void logic() {
		// TODO Auto-generated method stub
		
	}

	private void myDraw() {
		try {
			mCanvas=mSurfaceHolder.lockCanvas();
			if (mCanvas != null) {
				mCanvas.drawColor(Color.BLACK);
				//Draw original bitmap
				RectF rectFOne = new RectF(0, 50, mBitmap.getWidth() * 2, 120 + mBitmap.getHeight() * 2);
				RectF rectFTwo = new RectF(0, 120 + mBitmap.getHeight() * 2,
								mBitmap.getWidth() * 3, 140 + mBitmap.getHeight() * 2 + mBitmap.getHeight() * 3);
				mCanvas.drawBitmap(mBitmap, 0, 0, mPaint);
				mCanvas.drawBitmap(mBitmap, null, rectFOne, mPaint);
				mCanvas.drawBitmap(mBitmap, null, rectFTwo, mPaint);
				
				//Draw nine_path bitmap
				RectF rectF9PathOne = new RectF(250, 50, 250 + mBitmap9Path.getWidth() * 2, 120 + mBitmap9Path.getHeight() * 2);
				RectF rectF9PathTwo = new RectF(250, 120 + mBitmap9Path.getHeight() * 2,
						250 + mBitmap9Path.getWidth() * 3, 140 + mBitmap9Path.getHeight() * 2 +mBitmap9Path.getHeight() * 3);
				mCanvas.drawBitmap(mBitmap9Path, 250, 0, mPaint);
				mNinePath.draw(mCanvas, rectF9PathOne);
				mNinePath.draw(mCanvas, rectF9PathTwo);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			if (mCanvas != null) {
				mSurfaceHolder.unlockCanvasAndPost(mCanvas);
			}
		}
	}
	
}

NinePatch 9Patch 绘制_第1张图片

问题一:

刚开始画的时候位置和自己想的不一样,mCanvas.drawBitmap(mBitmap, 0, 0, mPaint);按理说应该是画在左上角的,但是实际的是出现在了右上角,原来是设置了屏幕的方向,setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);设置屏幕方向为横屏,然后整个绘图的坐标系都是按照横屏的时候的坐标系来算的,将手机横过来就可以了

你可能感兴趣的:(thread,exception,null,Path,callback,patch)