Android應用開發基礎篇(16)—–ScaleGestureDetector(縮放手勢檢測)

來源:互聯網
上載者:User

一、概述

      ScaleGestureDetector這個類是專門用來檢測兩個手指在螢幕上做縮放的手勢用的,最簡單的應用就是用來縮放圖片或者縮放網頁。


二、要求

     利用ScaleGestureDetector這個類實現圖片縮放。


三、實現

     建立工程MyScale,修改main.xml檔案,在裡面添加一個Button和一個SurfaceView,如下:

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/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 }

運行該程式:

 

點擊一下“顯示圖片”按鈕:

 

然後用兩個手指縮小圖片:

 

放大圖片:

 

好了。可以發現,每次一開始縮放圖片的時候都有一個突變,如果用來做應用的話這個程式還需要改善。


PS:圖片來源於互連網


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.