Draw 9-Patch使用方法

使用Draw 9-patch工具可以允许你容易的创建NinePatch图像,以下步骤指导你使用Draw 9-patch工具,由PNG图像创建NinePatch图像。

(1)   在你的电脑上找到SDK/tools目录,启动draw9patch程序。

(2)   加载PNG图像到Draw 9-patch窗口,工作区域就打开了。

(3)   在图像的左边和上边的周边,左键选择拉伸区域和内容区。使用右键取消选择的区域。

(4)   保存你的图像。图像的后缀名是.9.png

选项的意思:

Zoom:调整画图区域图像的显示的大小

Patch scale:调整图像在预览区域的大小

Show lock:当鼠标悬停在图标上时,显示不可以绘制的区域。

Show patchs:预览图像拉伸的区域

Show content:当前操作的像素点在拉伸预览区域中的相对位置和效果。

Show bad patchs:为可能人为的拉伸区域设置红色的边框。如果你消除所有的坏块,你伸展的图像视觉的连贯性将保持。

注意事项:

在使用Ninepatch图像时,必须实现Ninepatch类,使用其draw()方法,使用CanvasdrawBitmap不能实现显示效果。

代码:

package com.example.ninepatchtest;

import android.content.Context;
import android.content.res.Resources.Theme;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements Callback, Runnable {

	private SurfaceHolder surfaceHolder;
	private Canvas canvas;
	private Paint paint;
	private Bitmap bird_old;
	private Bitmap bird_9patch;
	private NinePatch nPatch;
	private Thread thread;
	private Rect rect;
	private Rect rect2;
	private Rect rect3;
	private Rect rect_patch;
	private Rect rect_patch2;
	private Rect rect_patch3;
	private boolean flag = false;
	public MySurfaceView(Context context) {
		super(context);
		this.setKeepScreenOn(true);
		surfaceHolder = getHolder();
		surfaceHolder.addCallback(this);
		bird_old = BitmapFactory.decodeResource(getResources(), R.drawable.bird_old);
		bird_9patch = BitmapFactory.decodeResource(getResources(), R.drawable.bird_9patch);
		nPatch = new NinePatch(bird_9patch, bird_9patch.getNinePatchChunk(), null);
		paint = new Paint();
		paint.setAntiAlias(true);
		rect = new Rect(0, 0, bird_old.getWidth(), bird_old.getHeight());
		rect2 = new Rect(0, bird_old.getHeight() + 20, bird_old.getWidth()*2, bird_old.getHeight()*3 + 20);
		rect3 = new Rect(0, bird_old.getHeight()*3 + 40, bird_old.getWidth()*3, bird_old.getHeight()*6 + 40);
		rect_patch = new Rect(300, 0, 300 + bird_9patch.getWidth(), bird_9patch.getHeight());
		rect_patch2 = new Rect(300, bird_9patch.getHeight() +20, 300 + bird_9patch.getWidth()*2, bird_9patch.getHeight()*3 + 20);
		rect_patch3 = new Rect(300, bird_9patch.getHeight()*3 + 40, 300 + bird_9patch.getWidth()*3, bird_9patch.getHeight()*6 +40);
		thread = new Thread(this);
		this.setFocusable(true);
	}

	public void draw() {
		canvas = surfaceHolder.lockCanvas();
		try {
			if (canvas != null) {
				canvas.drawColor(Color.BLACK);
				canvas.drawBitmap(bird_old, null, rect, paint);
				canvas.drawBitmap(bird_old, null, rect2, paint);
				canvas.drawBitmap(bird_old, null, rect3, paint);
				nPatch.draw(canvas, rect_patch);
				nPatch.draw(canvas, rect_patch2);
				nPatch.draw(canvas, rect_patch3);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if (canvas != null) {
				surfaceHolder.unlockCanvasAndPost(canvas);
			}
		}
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (flag) {
			draw();
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		flag = true;
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		flag = false;
	}

}


Draw 9-Patch使用方法_第1张图片

一位大牛的NinePatach文章:http://blog.csdn.net/feng88724/article/details/6170121

工程下载:http://download.csdn.net/detail/zhaoshiqing7/4488658

你可能感兴趣的:(Draw 9-Patch使用方法)