zoukankan      html  css  js  c++  java
  • Android应用开发基础篇(16)ScaleGestureDetector(缩放手势检测)

    一、概述

          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:图片来源于互联网


  • 相关阅读:
    shell-脚本_系统监测
    shell-脚本_防火墙规则的简单应用
    shell-命令_find
    shell-命令_cut
    shell-条件测试语句_test
    Error:java: 不再支持源选项 5。请使用 6 或更高版本。
    android项目删除recycleview相对应的数据库数据后闪退
    大作业--社团管理系统总结
    北京地铁出行路线代码分析
    北京地铁出行路线规划设计
  • 原文地址:https://www.cnblogs.com/lknlfy/p/2390274.html
Copyright © 2011-2022 走看看