代码实现9patch格式

package com.himi; 
02 import android.content.Context; 
03 import android.graphics.Bitmap; 
04 import android.graphics.BitmapFactory; 
05 import android.graphics.Canvas; 
06 import android.graphics.Color; 
07 import android.graphics.NinePatch; 
08 import android.graphics.Paint; 
09 import android.graphics.RectF; 
10 import android.util.Log; 
11 import android.view.SurfaceHolder; 
12 import android.view.SurfaceView; 
13 import android.view.SurfaceHolder.Callback; 
14 public class MySurfaceView extends SurfaceView implements Callback, Runnable { 
15     private Thread th = new Thread(this); 
16     private SurfaceHolder sfh; 
17     private Canvas canvas; 
18     private Paint paint; 
19     private Bitmap bmp_old; 
20     private Bitmap bmp_9path; 
21     private NinePatch np; 
22     public MySurfaceView(Context context) { 
23         super(context); 
24         this.setKeepScreenOn(true); 
25         bmp_old = BitmapFactory.decodeResource(getResources(), R.drawable.himi_old); 
26         bmp_9path = BitmapFactory.decodeResource(getResources(), R.drawable.himi_9path); 
27         np = new NinePatch(bmp_9path, bmp_9path.getNinePatchChunk(), null); 
28         //创建一个ninePatch的对象实例,第一个参数是bitmap、第二个参数是byte[],这里其实要求我们传入 
29         //如何处理拉伸方式,当然我们不需要自己传入,因为“.9.png”图片自身有这些信息数据, 
30         //也就是我们用“9妹”工具操作的信息! 我们直接用“.9.png”图片自身的数据调用getNinePatchChunk()即可 
31         //第三个参数是图片源的名称,这个参数为可选参数,直接null~就OK~ 
32         sfh = this.getHolder(); 
33         sfh.addCallback(this); 
34         paint = new Paint(); 
35         paint.setAntiAlias(true); 
36         setFocusable(true); 
37     
38     public void surfaceCreated(SurfaceHolder holder) { 
39         Log.v("Himi", "surfaceCreated"); 
40         th.start(); 
41     
42     /**
43      * @author Himi
44      */ 
45     public void draw() { 
46         canvas = sfh.lockCanvas(); 
47         canvas.drawColor(Color.BLACK); 
48         RectF rectf_old_two = new RectF(0, 50, bmp_old.getWidth() * 2, 120 + bmp_old.getHeight() * 2);//备注1 
49         RectF rectf_old_third = new RectF(0, 120 + bmp_old.getHeight() * 2, bmp_old.getWidth() * 3
50                 140 + bmp_old.getHeight() * 2 + bmp_old.getHeight() * 3); 
51         // --------下面是对正常png绘画方法----------- 
52         canvas.drawBitmap(bmp_old, 0, 0, paint); 
53         canvas.drawBitmap(bmp_old, null, rectf_old_two, paint); 
54         canvas.drawBitmap(bmp_old, null, rectf_old_third, paint); 
55         RectF rectf_9path_two = new RectF(250, 50, 250 + bmp_9path.getWidth() * 2, 90 + bmp_9path.getHeight() * 2); 
56         RectF rectf_9path_third = new RectF(250, 120 + bmp_9path.getHeight() * 2, 250 + bmp_9path.getWidth() * 3
57                     140 + bmp_9path.getHeight() * 2 
58                 + bmp_9path.getHeight() * 3); 
59         canvas.drawBitmap(bmp_9path, 250, 0, paint); 
60         // --------下面是".9.png"图像的绘画方法----------- 
61         np.draw(canvas, rectf_9path_two); 
62         np.draw(canvas, rectf_9path_third); 
63         sfh.unlockCanvasAndPost(canvas); 
64     
65     public void run() { 
66         // TODO Auto-generated method stub 
67         while (true) { 
68             draw(); 
69             try
70                 Thread.sleep(100); 
71             } catch (Exception ex) { 
72             
73         
74     
75     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
76         Log.v("Himi", "surfaceChanged"); 
77     
78     public void surfaceDestroyed(SurfaceHolder holder) { 
79         Log.v("Himi", "surfaceDestroyed"); 
80     
81

.9.png 格式的文件的特点

   与传统的png 格式图片相比, 9.png 格式图片在图片四周有一圈一个像素点组成的边沿,该边沿用于对图片的可扩展区和内容显示区进行定义。 

   这种格式的图片在android 环境下具有自适应调节大小的能力。

   (1)允许开发人员定义可扩展区域,当需要延伸图片以填充比图片本身更大区域时,可扩展区的内容被延展。

   (2)允许开发人员定义内容显示区,用于显示文字或其他内容

    如下图所示:

    左侧和上方的黑线交叉的部分即可扩展区域

    右侧和下方的黑线交叉的部分即内容显示区


你可能感兴趣的:(exception,null,扩展,Path,import,patch)