Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3534157
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: 嵌入式

2012-09-04 17:21:13

转自:http://www.cnblogs.com/lknlfy/archive/2012/03/11/2390274.html

      ScaleGestureDetector这个类是专门用来检测两个手指在屏幕上做缩放的手势用的,最简单的应用就是用来缩放图片或者缩放网页。

     利用ScaleGestureDetector这个类实现图片缩放。


实现

     新建工程MyScale,修改main.xml文件,在里面添加一个Button和一个SurfaceView,如下:

复制代码
1 xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android=""
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <Button
8 android:id="@+id/button"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:text="显示图片"
12 />
13
14 <SurfaceView
15 android:id="@+id/surfaceview"
16 android:layout_width="fill_parent"
17 android:layout_height="fill_parent"
18 />
19
20 LinearLayout>
复制代码

修改MyScaleActivity.java文件,主要是实现按钮的监听和定义一个类实现ScaleGestureDetector.OnScaleGestureListener接口:

 

复制代码
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 }
复制代码




阅读(1444) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~