当前位置:  编程技术>移动开发

android 多点触摸图片缩放的具体实现方法

    来源: 互联网  发布时间:2014-10-20

    本文导语:  布局: 代码如下:                                             程序代码: 代码如下:package taokun.demo.MutilTouchDemo; import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Ma...

布局:

代码如下:



   
   

   

       

           
       
   


程序代码:

代码如下:

package taokun.demo.MutilTouchDemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;

public class MutilTouchDemoActivity extends Activity implements OnTouchListener, OnClickListener {
   private static final String TAG = "Touch" ;

   // These matrices will be used to move and zoom image
   Matrix matrix = new Matrix();
   Matrix savedMatrix = new Matrix();
   PointF start = new PointF();
   PointF mid = new PointF();
   float  oldDist;
   private ImageView view;
   private Button zoomIn, zoomOut;

   //button zoom
   private float scaleWidth = 1;
        private float scaleHeight = 1;
        private Bitmap bmp, zoomedBMP;
        private int zoom_level = 0;
        private static final double ZOOM_IN_SCALE = 1.25;//放大系数
        private static final double ZOOM_OUT_SCALE = 0.8;//缩小系数

   // We can be in one of these 3 states
   static final int NONE = 0;
   static final int DRAG = 1;
   static final int ZOOM = 2;
   int mode = NONE;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      //放大按钮
      zoomIn = (Button) findViewById(R.id.zoom_in);
       //缩小按钮
      zoomOut = (Button) findViewById(R.id.zoom_out);
      zoomIn.setOnClickListener(this);
      zoomOut.setOnClickListener(this);
      view = (ImageView) findViewById(R.id.imageView);
      view.setOnTouchListener(this);
      //取得drawable中图片,放大,缩小,多点触摸的作用对象    
       bmp = BitmapFactory.decodeResource(MutilTouchDemoActivity.this.getResources(), R.drawable.splash);

              }

   public boolean onTouch(View v, MotionEvent event) {
      // Handle touch events here...
      ImageView view = (ImageView) v;

      // Handle touch events here...
      switch (event.getAction() & MotionEvent.ACTION_MASK) {
        //设置拖拉模式
        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            Log.d(TAG, "mode=DRAG" );
            mode = DRAG;
            break;

         case MotionEvent.ACTION_UP:
         case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            Log.d(TAG, "mode=NONE" );
            break;
         //设置多点触摸模式
         case MotionEvent.ACTION_POINTER_DOWN:
            oldDist = spacing(event);
            Log.d(TAG, "oldDist=" + oldDist);
            if (oldDist > 10f) {
               savedMatrix.set(matrix);
               midPoint(mid, event);
               mode = ZOOM;
               Log.d(TAG, "mode=ZOOM" );
            }
            break;
          //若为DRAG模式,则点击移动图片
         case MotionEvent.ACTION_MOVE:
            if (mode == DRAG) {
               matrix.set(savedMatrix);
               // 设置位移
               matrix.postTranslate(event.getX() - start.x,
              event.getX() - start.x);
            }
            //若为ZOOM模式,则多点触摸缩放
               else if (mode == ZOOM) {
               float newDist = spacing(event);
               Log.d(TAG, "newDist=" + newDist);
               if (newDist > 10f) {
                  matrix.set(savedMatrix);
                  float scale = newDist / oldDist;
                  //设置缩放比例和图片中点位置
                  matrix.postScale(scale, scale, mid.x, mid.y);
               }
            }
            break;
      }

      // Perform the transformation
      view.setImageMatrix(matrix);

      return true; // indicate event was handled
   }
//计算移动距离
   private float spacing(MotionEvent event) {
      float x = event.getX(0) - event.getX(1);
      float y = event.getY(0) - event.getY(1);
      return FloatMath.sqrt(x * x + y * y);
   }
// 计算中点位置
   private void midPoint(PointF point, MotionEvent event) {
      float x = event.getX(0) + event.getX(1);
      float y = event.getY(0) + event.getY(1);
      point.set(x / 2, y / 2);
   }

//放大,缩小按钮点击事件
@Override
public void onClick(View v) {
        if(v == zoomIn){
                enlarge();
        }else if (v == zoomOut) {
                small();
        }

}

//按钮点击缩小函数
private void small() {

        int bmpWidth = bmp.getWidth();
        int bmpHeight = bmp.getHeight();

        scaleWidth = (float) (scaleWidth * ZOOM_OUT_SCALE);
        scaleHeight = (float) (scaleHeight * ZOOM_OUT_SCALE);

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        zoomedBMP = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix,
                        true);
        view.setImageBitmap(zoomedBMP);
}

//按钮点击放大函数
private void enlarge() {
        try {
                int bmpWidth = bmp.getWidth();
                int bmpHeight = bmp.getHeight();

                scaleWidth = (float) (scaleWidth * ZOOM_IN_SCALE);
                scaleHeight = (float) (scaleHeight * ZOOM_IN_SCALE);

                Matrix matrix = new Matrix();
                matrix.postScale(scaleWidth, scaleHeight);
                zoomedBMP = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix,
                                true);
                view.setImageBitmap(zoomedBMP);
        } catch (Exception e) {

        }

}
}


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • Android修改源码解决Alertdialog触摸对话框边缘消失的问题
  • Android1.3触摸屏问题
  • android中处理各种触摸事件的方法浅谈
  • 申请Android Map 的API Key(v2)的最新申请方式(SHA1密钥)
  • Android瀑布流实例 android_waterfall
  • Android开发需要的几点注意事项总结
  • Android系统自带样式 (android:theme)
  • android 4.0 托管进程介绍及优先级和回收机制
  • Android网络共享软件 Android Wifi Tether
  • Android访问与手机通讯相关类的介绍
  • Android 图标库 Android GraphView
  • Android及andriod无线网络Wifi开发的几点注意事项
  • 轻量级Android开发工具 Android Tools
  • Android 2.3 下StrictMode介绍
  • Android 开发环境 Android Studio
  • IDEA的Android开发插件 idea-android
  • Android手机事件提醒 Android Notifier
  • XBMC的Android客户端 android-xbmcremote
  • Android小游戏 Android Shapes
  • Android电池监控 Android Battery Dog
  • android开发:“android:WindowTitle”没有对应项no resource
  • Android 上类似IOS 的开关控件。 Android ToggleButton
  • Android 将 android view 的位置设为右下角的解决方法
  • Android 2D游戏引擎 Android Angle


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    HTML标签参考手册 iis7站长之家