1 package com.nan.scale;
2 3 import android.app.Activity;
4 import android.graphics.Bitmap;
5 import android.graphics.BitmapFactory;
6 import android.graphics.Canvas;
7 import android.graphics.Color;
8 import android.graphics.Matrix;
9 import android.graphics.Rect;
10 import android.os.Bundle;
11 import android.view.MotionEvent;
12 import android.view.ScaleGestureDetector;
13 import android.view.SurfaceHolder;
14 import android.view.SurfaceView;
15 import android.view.View;
16 import android.widget.Button;
17 18 19 20 public class MyScaleActivity
extends Activity
21 {
22 private Button mButton =
null;
23 private SurfaceView mSurfaceView =
null;
24 private SurfaceHolder mSurfaceHolder =
null;
25 private ScaleGestureDetector mScaleGestureDetector =
null;
26 private Bitmap mBitmap =
null;
27 28 /** Called when the activity is first created. */ 29 @Override
30 public void onCreate(Bundle savedInstanceState)
31 {
32 super.onCreate(savedInstanceState);
33 setContentView(R.layout.main);
34 35 mSurfaceView = (SurfaceView)
this.findViewById(R.id.surfaceview);
36 mSurfaceHolder = mSurfaceView.getHolder();
37 mScaleGestureDetector =
new ScaleGestureDetector(
this,
new ScaleGestureListener());
38 mButton = (Button)
this.findViewById(R.id.button);
39 //按钮监听
40 mButton.setOnClickListener(
new View.OnClickListener()
41 {
42 43 @Override
44 public void onClick(View v)
45 {
46 // TODO Auto-generated method stub
47 mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mmm);
48 //锁定整个SurfaceView
49 Canvas mCanvas = mSurfaceHolder.lockCanvas();
50 //画图
51 mCanvas.drawBitmap(mBitmap, 0f, 0f,
null);
52 //绘制完成,提交修改
53 mSurfaceHolder.unlockCanvasAndPost(mCanvas);
54 //重新锁一次
55 mSurfaceHolder.lockCanvas(
new Rect(0, 0, 0, 0));
56 mSurfaceHolder.unlockCanvasAndPost(mCanvas);
57 }
58 });
59 60 }
61 62 @Override
63 public boolean onTouchEvent(MotionEvent event)
64 {
65 //返回给ScaleGestureDetector来处理
66 return mScaleGestureDetector.onTouchEvent(event);
67 }
68 69 70 public class ScaleGestureListener
implements ScaleGestureDetector.OnScaleGestureListener
71 {
72 73 @Override
74 public boolean onScale(ScaleGestureDetector detector)
75 {
76 // TODO Auto-generated method stub
77 78 Matrix mMatrix =
new Matrix();
79 //缩放比例
80 float scale = detector.getScaleFactor()/3;
81 mMatrix.setScale(scale, scale);
82 83 //锁定整个SurfaceView
84 Canvas mCanvas = mSurfaceHolder.lockCanvas();
85 //清屏
86 mCanvas.drawColor(Color.BLACK);
87 //画缩放后的图
88 mCanvas.drawBitmap(mBitmap, mMatrix,
null);
89 //绘制完成,提交修改
90 mSurfaceHolder.unlockCanvasAndPost(mCanvas);
91 //重新锁一次
92 mSurfaceHolder.lockCanvas(
new Rect(0, 0, 0, 0));
93 mSurfaceHolder.unlockCanvasAndPost(mCanvas);
94 95 return false;
96 }
97 98 @Override
99 public boolean onScaleBegin(ScaleGestureDetector detector)
100 {
101 // TODO Auto-generated method stub
102 //一定要返回true才会进入onScale()这个函数
103 return true;
104 }
105 106 @Override
107 public void onScaleEnd(ScaleGestureDetector detector)
108 {
109 // TODO Auto-generated method stub
110 111 }
112 113 }
114 115 }