zoukankan      html  css  js  c++  java
  • 多点触摸的操作与图片放大缩小

     1 import android.app.Activity;
     2 import android.os.Bundle;
     3 import android.view.MotionEvent;
     4 import android.view.View;
     5 import android.widget.FrameLayout;
     6 import android.widget.FrameLayout.LayoutParams;
     7 import android.widget.ImageView;
     8 
     9 public class MainActivity extends Activity {
    10     
    11     private ImageView iv;
    12     private FrameLayout root;
    13 
    14     @Override
    15     protected void onCreate(Bundle savedInstanceState) {
    16         super.onCreate(savedInstanceState);
    17         setContentView(R.layout.activity_main);
    18 
    19         root = (FrameLayout) findViewById(R.id.container);
    20         iv = (ImageView) findViewById(R.id.iv);
    21 
    22         root.setOnTouchListener(new View.OnTouchListener() {
    23 
    24             float currentDistance;
    25             float lastDistance = -1; // 记录最有一次所记录的值
    26 
    27             @Override
    28             public boolean onTouch(View v, MotionEvent event) {
    29 
    30                 switch (event.getAction()) {
    31                 case MotionEvent.ACTION_DOWN: // 触摸按下
    32                     System.out.println("action down");
    33                     break;
    34                 case MotionEvent.ACTION_MOVE: // 触摸移动
    35                     // System.out.println("action move");
    36                     System.out.println("触摸点个数:" + event.getPointerCount());
    37 
    38                     if (event.getPointerCount() >= 2) { // 判断大于等于两个触摸点
    39 
    40                         float offsetX = event.getX(0) - event.getX(1);
    41                         float offsetY = event.getY(0) - event.getY(1);
    42                         // 计算出两个点之间的距离
    43                         currentDistance = (float) Math.sqrt(offsetX * offsetX
    44                                 + offsetY * offsetY);
    45 
    46                         if (lastDistance < 0) { // 初始值
    47                             lastDistance = currentDistance;
    48                         } else {
    49                             // 当前距离比上次距离>5个像素,可以识别为放大。
    50                             if (currentDistance - lastDistance > 5) { 
    51                                 System.out.println("放大");
    52 
    53                                 FrameLayout.LayoutParams lp = (LayoutParams) iv
    54                                         .getLayoutParams();
    55                                 lp.width = (int) (1.1f * iv.getWidth());
    56                                 lp.height = (int) (1.1f * iv.getHeight());
    57 
    58                                 iv.setLayoutParams(lp);
    59 
    60                                 lastDistance = currentDistance;
    61                             } else if (lastDistance - currentDistance > 5) { //
    62                                 System.out.println("缩小");
    63 
    64                                 FrameLayout.LayoutParams lp = (LayoutParams) iv
    65                                         .getLayoutParams();
    66                                 lp.width = (int) (0.9f * iv.getWidth());
    67                                 lp.height = (int) (0.9f * iv.getHeight());
    68 
    69                                 iv.setLayoutParams(lp);
    70 
    71                                 lastDistance = currentDistance;
    72                             }
    73                         }
    74                     }
    75                     break;
    76                 case MotionEvent.ACTION_UP: // 触摸弹起
    77                     // System.out.println("action up");
    78                     break;
    79                 }
    80 
    81                 return true;
    82             }
    83         });
    84     }
    85 
    86 }
     1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:id="@+id/container"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     tools:context="com.jikexueyuan.multouch.MainActivity"
     7     tools:ignore="MergeRootFrame" >
     8 
     9     <ImageView
    10         android:id="@+id/iv"
    11         android:layout_width="wrap_content"
    12         android:layout_height="wrap_content"
    13         android:src="@drawable/ic_launcher" />
    14 
    15 </FrameLayout>
  • 相关阅读:
    UNIX网络编程——Socket通信原理和实践
    UNIX环境高级编程——单实例的守护进程
    UNIX环境高级编程——初始化一个守护进程
    UNIX环境高级编程——创建孤儿进程
    UNIX环境高级编程——实现uid to name
    CentOS7中使用yum安装Nginx的方法
    centos7配置IP地址
    关于Dubbo的原理以及详细配置
    关于Java大数操作(BigInteger、BigDecimal)
    关于JSON 与 对象 、集合之间的转换
  • 原文地址:https://www.cnblogs.com/androidsj/p/3948584.html
Copyright © 2011-2022 走看看