• Android_Touch_Test


      1 package com.example.qjm3662.android_touch_test;
      2 
      3 import android.os.Bundle;
      4 import android.support.design.widget.FloatingActionButton;
      5 import android.support.design.widget.Snackbar;
      6 import android.support.v7.app.AppCompatActivity;
      7 import android.support.v7.widget.Toolbar;
      8 import android.util.Log;
      9 import android.view.MotionEvent;
     10 import android.view.View;
     11 import android.view.Menu;
     12 import android.view.MenuItem;
     13 import android.widget.ImageView;
     14 import android.widget.LinearLayout;
     15 
     16 public class MainActivity extends AppCompatActivity {
     17 
     18     private String TAG = "MainActivity";
     19     private LinearLayout root;
     20     private ImageView iv;
     21     @Override
     22     protected void onCreate(Bundle savedInstanceState) {
     23         super.onCreate(savedInstanceState);
     24         setContentView(R.layout.activity_main);
     25 
     26         root = (LinearLayout) findViewById(R.id.container);
     27         iv = (ImageView) findViewById(R.id.iv);
     28 
     29         root.setOnTouchListener(new View.OnTouchListener() {
     30 
     31             float offsetX;
     32             float offsetY;
     33             float curruntDistance;      //当前距离
     34             float lastDistance = -1;    //前一次的距离
     35             @Override
     36             public boolean onTouch(View v, MotionEvent event) {
     37                 switch(event.getAction()){
     38                     case MotionEvent.ACTION_DOWN:
     39                         Log.e(TAG,"down");
     40                         break;
     41                     case MotionEvent.ACTION_UP:
     42                         Log.e(TAG,"up");
     43                         break;
     44                     case MotionEvent.ACTION_MOVE:
     45                         if(event.getPointerCount() > 1){
     46                             
     47                             //两个触摸点之间的距离
     48                             offsetX = event.getX(0) - event.getX(1);
     49                             offsetY = event.getY(0) - event.getY(1);
     50 
     51                             //当前距离
     52                             curruntDistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY);
     53 
     54                             //第一次给lastDistance赋值
     55                             if(lastDistance < 0){
     56                                 lastDistance = curruntDistance;
     57                             }else{
     58                                 //获取ImageView的layoutParms
     59                                 LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) iv.getLayoutParams();
     60                                 
     61                                 //与上一次相比距离增大(5px是为了考虑操作误差)
     62                                 if(curruntDistance - lastDistance > 5){
     63                                     
     64                                     //放大图片操作
     65                                     layoutParams.width = (int) (1.1f*iv.getWidth());
     66                                     layoutParams.height = (int) (1.1f*iv.getHeight());
     67 
     68                                     //操作完务必给lastDistance重新赋值
     69                                     lastDistance = curruntDistance;
     70                                     iv.setLayoutParams(layoutParams);
     71                                     Log.e(TAG,"放大");
     72                                 }
     73                                 //与上一次相比距离减小(5px是为了考虑操作误差)
     74                                 else if(lastDistance - curruntDistance > 5){
     75                                     layoutParams.width = (int) (0.9f*iv.getWidth());
     76                                     layoutParams.height = (int) (0.9f*iv.getHeight());
     77                                     lastDistance = curruntDistance;
     78                                     iv.setLayoutParams(layoutParams);
     79                                     Log.e(TAG,"缩小");
     80                                 }
     81                             }
     82                         }
     83 
     84 //                        Log.e(TAG, "move");
     85 //                        //多点触控
     86 //                        Log.e(TAG,"Point count : " + event.getPointerCount());
     87 //                        if(event.getPointerCount() > 1){
     88 //                            Log.e(TAG,String.format("x1:%f,y1:%f x2:%f,y2:%f",event.getX(0),event.getY(0),event.getX(1),event.getY(1)));
     89 //                        }
     90                         
     91                         
     92 //                        //获取单个触摸点
     93 //                        LinearLayout.LayoutParams lp;
     94 //                        lp = (LinearLayout.LayoutParams) iv.getLayoutParams();
     95 //                        Log.e(TAG,String.format("x:%f,y:%f",event.getX(),event.getY()));
     96 //                        lp.leftMargin = (int) event.getX();
     97 //                        lp.topMargin = (int) event.getY();
     98 //                        iv.setLayoutParams(lp);
     99                         break;
    100                 }
    101                 return true;
    102             }
    103         });
    104     }
    105 
    106 }

  • 相关阅读:
    第15届创新英语大赛初赛第二阶段题目
    13.递归第一次
    13.递归第一次
    13.递归第一次
    13.递归第一次
    Algs4-1.3.19给出一段代码,删除链表的尾结点
    Algs4-1.3.18下面链表在代码的作用
    Algs4-1.3.17从文件中读取信息生成Transaction对象数组
    Algs4-1.3.16从文件中读取日期生成日期对象数组
    Algs4-1.3.15打印Queue中倒数第k个元素的值
  • 原文地址:https://www.cnblogs.com/qjm253/p/5374772.html
Copyright © 2020-2023  润新知