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; |
19 | private
Bitmap bmp_old; |
20 | private
Bitmap bmp_9path; |
22 | public
MySurfaceView(Context 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 ); |
32 | sfh =
this .getHolder();
|
33 | sfh.addCallback( this );
|
35 | paint.setAntiAlias( true );
|
38 | public
void surfaceCreated(SurfaceHolder holder) {
|
39 | Log.v( "Himi" ,
"surfaceCreated" );
|
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 ); |
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 );
|
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);
|
61 | np.draw(canvas, rectf_9path_two);
|
62 | np.draw(canvas, rectf_9path_third);
|
63 | sfh.unlockCanvasAndPost(canvas);
|
71 | }
catch (Exception ex) {
|
75 | public
void surfaceChanged(SurfaceHolder holder,
int format, int
width, int
height) { |
76 | Log.v( "Himi" ,
"surfaceChanged" );
|
78 | public
void surfaceDestroyed(SurfaceHolder holder) {
|
79 | Log.v( "Himi" ,
"surfaceDestroyed" );
|
.9.png 格式的文件的特点
与传统的png 格式图片相比, 9.png 格式图片在图片四周有一圈一个像素点组成的边沿,该边沿用于对图片的可扩展区和内容显示区进行定义。
这种格式的图片在android 环境下具有自适应调节大小的能力。
(1)允许开发人员定义可扩展区域,当需要延伸图片以填充比图片本身更大区域时,可扩展区的内容被延展。
(2)允许开发人员定义内容显示区,用于显示文字或其他内容
如下图所示:
左侧和上方的黑线交叉的部分即可扩展区域
右侧和下方的黑线交叉的部分即内容显示区