博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android——单点触控移动,多点触控放大缩小
阅读量:6039 次
发布时间:2019-06-20

本文共 5674 字,大约阅读时间需要 18 分钟。

xml

java

package com.example.touchdemo;import android.os.Bundle;import android.app.Activity;import android.content.res.Resources;import android.util.DisplayMetrics;import android.util.Log;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.Toast;public class MainActivity extends Activity {    private ImageView image;    float lastDistance = -1;    float x0 = 0;    float y0 = 0;     private int screenWidth;        private int screenHeight;         @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                image = (ImageView) findViewById(R.id.imgage);        //获得屏幕宽高 不让出界         DisplayMetrics dm = getResources().getDisplayMetrics();             screenWidth = dm.widthPixels;             screenHeight = dm.heightPixels-100;         image.setOnTouchListener(new OnTouchListener() {                         private float x;            private float y;            @Override            public boolean onTouch(View v,final MotionEvent event) {                // TODO Auto-generated method stub                int action = event.getAction();                Log.i("TAG","触摸开始......");                switch (action) {                case MotionEvent.ACTION_DOWN:                    //getX()是表示Widget相对于自身左上角的x坐标,而getRawX()是表示相对于屏幕左上角的x坐标值(注意:这个屏幕左上角是手机屏幕左上角,不管activity是否有titleBar或是否全屏幕)                    x = event.getRawX();                    y = event.getRawX();                    break;                                case MotionEvent.ACTION_MOVE:                    //得到触摸点的个数                    int count = event.getPointerCount();                    Log.i("TAG","触摸点的个数:"+count);                                        if(count > 1){                        Log.i("TAG", event.getX(0)+","+event.getY(0)+"|"+event.getX(1)+","+event.getY(1));                        //获得两点的坐标差                        float distanceX = event.getX(0) - event.getX(1);                        float distanceY = event.getY(0) - event.getY(1);                        //获得两点之间的距离                        float betweenDistance = (float) Math.sqrt(distanceX*distanceX + distanceY*distanceY);                        Log.i("TAG","当前两点之间的新距离:"+betweenDistance);                                            if(betweenDistance < 1){                            lastDistance = betweenDistance;                                                    }else if((betweenDistance - lastDistance) > 5){                            //放大                            lastDistance = betweenDistance;                            //获得布局参数                            RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) image.getLayoutParams();                            params.width = (int) (image.getWidth()*1.1f);                            params.height = (int) (image.getHeight()*1.1f);                            image.setLayoutParams(params);                        }else if((lastDistance - betweenDistance) > 5){                            lastDistance = betweenDistance;                            //获得布局参数                            RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) image.getLayoutParams();                            params.width = (int) (image.getWidth()*0.9f);                            params.height = (int) (image.getHeight()*0.9f);                            image.setLayoutParams(params);                        }                                            }else if(count == 1){                                                //移动距离                        float rawX = event.getRawX()-x;                        float rawy = event.getRawY()-y;                        //定义新的                        int left = (int) (image.getLeft()+rawX);                        int top = (int) (image.getTop()+rawy);                        int right = (int) (image.getRight()+rawX);                        int bottom = (int) (image.getBottom()+rawy);                         // 设置不能出界                           if (left < 0) {                               left = 0;                               right = left + v.getWidth();                           }                                      if (right > screenWidth) {                               right = screenWidth;                               left = right - v.getWidth();                           }                                      if (top < 0) {                               top = 0;                               bottom = top + v.getHeight();                           }                                      if (bottom > screenHeight) {                               bottom = screenHeight;                               top = bottom - v.getHeight();                           }                                                                         //赋值                        image.layout(left, top, right, bottom);                        //改成新的按下作标                        x=event.getRawX();                        y=event.getRawY();                        /*return false;*/                    }                    break;                              }                                return true;            }        });    }    //全局移动   @Overridepublic boolean onTouchEvent(MotionEvent event) {    // TODO Auto-generated method stub      /*image.setX(event.getX());        image.setY(event.getY()); */    return true;}    }

 

转载于:https://www.cnblogs.com/1426837364qqcom/p/5377405.html

你可能感兴趣的文章
我的友情链接
查看>>
SERVLET容器简介与JSP的关系
查看>>
《服务器SSH Public Key认证指南》-补充
查看>>
我的友情链接
查看>>
Java break continue return 的区别
查看>>
算法(Algorithms)第4版 练习 1.3.4
查看>>
jquery easyUI checkbox复选项获取并传后台
查看>>
浅析NopCommerce的多语言方案
查看>>
设计模式之简单工厂模式
查看>>
C++中变量的持续性、链接性和作用域详解
查看>>
2017 4月5日上午
查看>>
Google Chrome开发者工具
查看>>
第一阶段冲刺报告(一)
查看>>
使用crontab调度任务
查看>>
【转载】SQL经验小记
查看>>
zookeeper集群搭建 docker+zk集群搭建
查看>>
Vue2.5笔记:Vue的实例与生命周期
查看>>
论JVM爆炸的几种姿势及自救方法
查看>>
联合体、结构体简析
查看>>
使用throw让服务器端与客户端进行数据交互[Java]
查看>>