关闭

android 触摸手指动作放大和缩小图片

1083人阅读 评论(0) 收藏 举报
分类:
private RelativeLayout root;
    private ImageView moveImage;
    float lastDestance=-1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        root= (RelativeLayout) findViewById(R.id.Main_Activity);
        moveImage = (ImageView) findViewById(R.id.moveImage);

        root.setOnTouchListener(new View.OnTouchListener() {    //侦听控件的触摸事件
            @Override
            public boolean onTouch(View v, MotionEvent event) {


                float currentDestance;

                switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN ://监听点击一个点
                        System.out.println("按下");
                        break;
                    case MotionEvent.ACTION_MOVE ://触摸移动 手指移动时

                        if (event.getPointerCount()>=2)
                        {

                            float offSetX =event.getX(0)-event.getX(1);//获取
                            float offSetY =event.getY(0)-event.getY(1 );
                            currentDestance = (float) Math.sqrt(offSetX*offSetX+offSetY*offSetY);//获取两点间距离 然后运用勾股定理 算出两个手指点之间的距离

                            System.out.println(lastDestance);
                            if (lastDestance<0) {

                                System.out.println("初始化变量");
                                System.out.println(currentDestance);
                               lastDestance= (float)currentDestance;//将第一次获取的两点距离  复制给lastDestance
                                System.out.println(lastDestance);
                            }
                            else {
                                System.out.println("进入手指动作判断");
                                if (currentDestance-lastDestance>5)
                                {
                                    System.out.println("手势 放大");
                                    RelativeLayout.LayoutParams lv= (RelativeLayout.LayoutParams) moveImage.getLayoutParams();

                                    lv.width = (int) (1.1f*moveImage.getWidth());//每一次两点间的距离放大 将图片的高和宽 分别*1.1 不停的放大
                                    lv.height = (int) (1.1f*moveImage.getHeight());
                                    moveImage.setLayoutParams(lv);//将每次放大的距离复制个image的高和宽
                                    lastDestance =currentDestance;//将这一次的两点间距离赋值给lastDestance  以便每次比较
                                }
                                else if (lastDestance-currentDestance>5){

                                    System.out.println("手势 缩小");
                                    RelativeLayout.LayoutParams lv= (RelativeLayout.LayoutParams) moveImage.getLayoutParams();

                                    lv.width = (int) (0.9f*moveImage.getWidth());//当两点间的距离在不断的缩小时
                                    lv.height = (int) (0.9f*moveImage.getHeight());
                                    moveImage.setLayoutParams(lv);
                                    lastDestance =currentDestance;
                                }
                            }

                        }


                        break;
                    case MotionEvent.ACTION_UP ://手指离开屏幕事件
                        System.out.println("弹起");
                        break;
                    default:
                        System.out.println("无");
                        break;

                }
                return true;
            }
        });


    }
2
0
查看评论

android手指控制图片放大缩小

  • 2015-06-19 16:48
  • 1.16MB
  • 下载

Android自定义控件ImageViwe(三)——随手指进行图片的缩放

No amount of plastic surgery and money will correct the emotional turmoil in your head that tells you that you are not good enough the way you are 花...
  • zl18603543572
  • zl18603543572
  • 2016-03-08 23:47
  • 966

Android之——根据手势简单缩放图片

很久没有发表关于Android的文章了,今天,给大家带来一篇如何根据手势简单缩放图片的文章。 一、原理 老样子,我们还是先讲讲原理性的东东,很简单,当手指触摸屏幕图片时,记录下当时手指的坐标点;手指滑动的时,计算手指之间的距离,如果手指之间的距离增大,则放大图片,如果手指之间的距离缩小,则缩小图片的...
  • l1028386804
  • l1028386804
  • 2015-11-26 22:38
  • 2864

android 手势触发图片缩放拖动(包含缩放图片时图片的大小判断以及拖动图片时图片的边界判断)

定义ImageView,实现功能如下: 1.初始化时图片垂直居中显示,拉伸图片宽度至ImageView宽度。 2.使用两根手指放大缩小图片,可设置最大放大倍数,当图片小于ImageView宽度时,在手指离开屏幕时恢复到ImageView宽度。 3.支持双击放大缩小。当图片处于未放大状态时,双击...
  • nangongyanya
  • nangongyanya
  • 2016-02-19 14:05
  • 3401

手势状态控制图片的缩放和移动(双击放大,俩指滑动图片缩放)

          手势状态控制图片的缩放和移动(双击放大,俩指滑动图片缩放) MainActivity: package com.example.pointerstouchevent; ...
  • Sjitayuyan
  • Sjitayuyan
  • 2017-04-26 18:13
  • 623

可以通过手势放大缩小图片的TouchImageView

package com.example.lianshou_test.view; import android.annotation.TargetApi; import android.content.Context; import android.content.res.Configuration;...
  • qq_17387361
  • qq_17387361
  • 2016-09-28 14:21
  • 634

js两只手指控制div图片放大缩小功能(2)

可以在github 上下载demo链接  https://github.com/fongdaBoy/hammer-pinch-master html代码 html> html lang="zh-cmn-Hans"> head...
  • iceking66
  • iceking66
  • 2017-10-01 10:36
  • 506

Android_双指缩放TextView

xml<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android&...
  • qq_33068255
  • qq_33068255
  • 2016-06-02 00:55
  • 340

android 如何利用手势放大和缩小一个图片?

                         &#...
  • yuanyuan_186
  • yuanyuan_186
  • 2014-08-13 16:02
  • 653

Android实现 通过手势随意缩放、移动ImageView图片

刚到石家庄工作一星期,表示以前没有写过博客,唯一写过一篇的还是我自己的天气app的介绍(当做外链),听同事说写博客可以丰富简历。。。好吧。。。我就是冲着这个目的。。。试着写一写。。。。。
  • cdzz11
  • cdzz11
  • 2016-06-13 14:25
  • 5077
    个人资料
    • 访问:112193次
    • 积分:1387
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:3篇
    • 译文:0篇
    • 评论:32条
    博客专栏
    最新评论