欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android手势控制实现缩放、移动图片

程序员文章站 2024-02-23 16:30:34
本文实例为大家分享了android手势控制实现缩放、移动图片的方法,供大家参考,具体内容如下 新建一个触摸监听器类 package com.liyong.btp...

本文实例为大家分享了android手势控制实现缩放、移动图片的方法,供大家参考,具体内容如下

新建一个触摸监听器类

package com.liyong.btprinter; 

import android.graphics.matrix; 
import android.graphics.pointf; 
import android.util.floatmath; 
import android.view.motionevent; 
import android.view.view; 
import android.view.view.ontouchlistener; 
import android.widget.imageview; 

public class mulitpointtouchlistener implements ontouchlistener { 

private static final string tag = "touch"; 
  // these matrices will be used to move and zoom image 
  matrix matrix = new matrix(); 
  matrix savedmatrix = new matrix(); 

  // 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; 

  // remember some things for zooming 
  pointf start = new pointf(); 
  pointf mid = new pointf(); 
  float olddist = 1f; 

  @override 
  public boolean ontouch(view v, motionevent event) { 

      imageview view = (imageview) v; 
      // log.e("view_width", 
      // view.getimagematrix()..tostring()+"*"+v.getwidth()); 
      // dump touch event to log 
      dumpevent(event); 

      // handle touch events here... 
      switch (event.getaction() & motionevent.action_mask) { 
      case motionevent.action_down: 

          matrix.set(view.getimagematrix()); 
          savedmatrix.set(matrix); 
          start.set(event.getx(), event.gety()); 
          //log.d(tag, "mode=drag"); 
          mode = drag; 

          
          //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; 
      case motionevent.action_up: 
      case motionevent.action_pointer_up: 
          mode = none; 
          //log.e("view.getwidth", view.getwidth() + ""); 
          //log.e("view.getheight", view.getheight() + ""); 

          break; 
      case motionevent.action_move: 
          if (mode == drag) { 
              // ... 
              matrix.set(savedmatrix); 
              matrix.posttranslate(event.getx() - start.x, event.gety() 
                      - start.y); 
          } 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; 
      } 

      view.setimagematrix(matrix); 
      return true; // indicate event was handled 
  } 

  private void dumpevent(motionevent event) { 
      string names[] = { "down", "up", "move", "cancel", "outside", 
              "pointer_down", "pointer_up", "7?", "8?", "9?" }; 
      stringbuilder sb = new stringbuilder(); 
      int action = event.getaction(); 
      int actioncode = action & motionevent.action_mask; 
      sb.append("event action_").append(names[actioncode]); 
      if (actioncode == motionevent.action_pointer_down 
              || actioncode == motionevent.action_pointer_up) { 
          sb.append("(pid ").append( 
                  action >> motionevent.action_pointer_id_shift); 
          sb.append(")"); 
      } 
      sb.append("["); 
      for (int i = 0; i < event.getpointercount(); i++) { 
          sb.append("#").append(i); 
          sb.append("(pid ").append(event.getpointerid(i)); 
          sb.append(")=").append((int) event.getx(i)); 
          sb.append(",").append((int) event.gety(i)); 
          if (i + 1 < event.getpointercount()) 
              sb.append(";"); 
      } 
      sb.append("]"); 
      //log.d(tag, sb.tostring()); 
  } 

  
  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); 
  } 


}

编写一个xml文件

<?xml version="1.0" encoding="utf-8"?> 

  <linearlayout 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_height="fill_parent" 
  android:layout_width="fill_parent" 
  android:id="@+id/linearlayout1" 
  android:weightsum="1"> 
    <imageview android:layout_width="fill_parent" 
    android:id="@+id/priviewimage" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:scaletype="matrix" 
    > 
   </imageview> 
  </linearlayout> 

注意其中的: android:scaletype="matrix"

priviewimage.setimagebitmap(activitypreviewimage.click_bitmap); 
priviewimage=(imageview)findviewbyid(r.id.priviewimage); 
priviewimage.setontouchlistener(new mulitpointtouchlistener()); 

以上就是android手势控制缩放移动图片的全部代码,希望对大家的学习有所帮助。