Fighting

分寸 , 克制

android 触摸手指动作放大和缩小图片
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;
            }
        });


    }
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33048603/article/details/51007534
个人分类: android
所属专栏: android 学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭