色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > Android應(yīng)用開發(fā) ScaleGestureDetector(縮放手勢

          Android應(yīng)用開發(fā) ScaleGestureDetector(縮放手勢

          作者: 時間:2016-09-12 來源:網(wǎng)絡(luò) 收藏

          一、概述

          本文引用地址:http://cafeforensic.com/article/201609/305037.htm

          ScaleGestureDetector這個類是專門用來檢測兩個手指在屏幕上做縮放的手勢用的,最簡單的應(yīng)用就是用來縮放圖片或者縮放網(wǎng)頁。

          二、要求

          利用ScaleGestureDetector這個類實現(xiàn)圖片縮放。

          三、實現(xiàn)

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

          android:layout_width=fill_parent

          android:layout_height=fill_parent

          android:orientation=vertical >

          android:id=@+id/button

          android:layout_width=fill_parent

          android:layout_height=wrap_content

          android:text=顯示圖片

          />

          android:id=@+id/surfaceview

          android:layout_width=fill_parent

          android:layout_height=fill_parent

          />

          修改MyScaleActivity.java文件,主要是實現(xiàn)按鈕的監(jiān)聽和定義一個類實現(xiàn)ScaleGestureDetector.OnScaleGestureListener接口:

          package com.nan.scale;

          import android.app.Activity;

          import android.graphics.Bitmap;

          import android.graphics.BitmapFactory;

          import android.graphics.Canvas;

          import android.graphics.Color;

          import android.graphics.Matrix;

          import android.graphics.Rect;

          import android.os.Bundle;

          import android.view.MotionEvent;

          import android.view.ScaleGestureDetector;

          import android.view.SurfaceHolder;

          import android.view.SurfaceView;

          import android.view.View;

          import android.widget.Button;

          public class MyScaleActivity extends Activity

          {

          private Button mButton = null;

          private SurfaceView mSurfaceView = null;

          private SurfaceHolder mSurfaceHolder = null;

          private ScaleGestureDetector mScaleGestureDetector = null;

          private Bitmap mBitmap = null;

          @Override

          public void onCreate(Bundle savedInstanceState)

          {

          super.onCreate(savedInstanceState);

          setContentView(R.layout.main);

          mSurfaceView = (SurfaceView)this.findViewById(R.id.surfaceview);

          mSurfaceHolder = mSurfaceView.getHolder();

          mScaleGestureDetector = new ScaleGestureDetector(this,new ScaleGestureListener());

          mButton = (Button)this.findViewById(R.id.button);

          //按鈕監(jiān)聽

          mButton.setOnClickListener(new View.OnClickListener()

          {

          @Override

          public void onClick(View v)

          {

          // TODO Auto-generated method stub

          mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mmm);

          //鎖定整個SurfaceView

          Canvas mCanvas = mSurfaceHolder.lockCanvas();

          //畫圖

          mCanvas.drawBitmap(mBitmap, 0f, 0f, null);

          //繪制完成,提交修改

          mSurfaceHolder.unlockCanvasAndPost(mCanvas);

          //重新鎖一次

          mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));

          mSurfaceHolder.unlockCanvasAndPost(mCanvas);

          }

          });

          }

          @Override

          public boolean onTouchEvent(MotionEvent event)

          {

          //返回給ScaleGestureDetector來處理

          return mScaleGestureDetector.onTouchEvent(event);

          }

          public class ScaleGestureListener implements ScaleGestureDetector.OnScaleGestureListener

          {

          @Override

          public boolean onScale(ScaleGestureDetector detector)

          {

          // TODO Auto-generated method stub

          Matrix mMatrix = new Matrix();

          //縮放比例

          float scale = detector.getScaleFactor()/3;

          mMatrix.setScale(scale, scale);

          //鎖定整個SurfaceView

          Canvas mCanvas = mSurfaceHolder.lockCanvas();

          //清屏

          mCanvas.drawColor(Color.BLACK);

          //畫縮放后的圖

          mCanvas.drawBitmap(mBitmap, mMatrix, null);

          //繪制完成,提交修改

          mSurfaceHolder.unlockCanvasAndPost(mCanvas);

          //重新鎖一次

          mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));

          mSurfaceHolder.unlockCanvasAndPost(mCanvas);

          return false;

          }

          @Override

          public boolean onScaleBegin(ScaleGestureDetector detector)

          {

          // TODO Auto-generated method stub

          //一定要返回true才會進入onScale()這個函數(shù)

          return true;

          }

          @Override

          public void onScaleEnd(ScaleGestureDetector detector)

          {

          // TODO Auto-generated method stub

          }

          }

          }



          關(guān)鍵詞:

          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉