Android多点触摸操作

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android多点触摸操作

风风风一样   2022-06-02 我要评论

Android中的多点触摸可以实现图片的放大、缩小和旋转等处理,供大家参考,具体内容如下

主要通过setOnTouchListener方法来监听用户的触摸事件,通过event.getX(0)和 event.getX(1)来获取第一个触控点和第二个触控点的x轴(或者y轴)坐标,接下来在MotionEvent.ACTION_DOWN、MotionEvent.ACTION_MOVE、MotionEvent.ACTION_UP这几种情况中来对获取到的x轴或者y轴进行处理,就能实现我们想要的效果了。

下面这个小Demo实现了对图片的放大和缩小处理:

package com.example.administrator.translation;
 
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
 
 
public class MainActivity extends ActionBarActivity {
 
    private RelativeLayout layout;
    private ImageView iv;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        layout = (RelativeLayout) findViewById(R.id.layout);
        iv = (ImageView) findViewById(R.id.iv);
 
 
        layout.setOnTouchListener(new View.OnTouchListener() {
 
            float currentDistance;
            float lastDistance = -1;
 
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
 
                        break;
                    case MotionEvent.ACTION_MOVE:
                        //判断几个触控点
                        if (event.getPointerCount() >= 2) {
                            //两点之间x的坐标差
                            float offsetX = event.getX(0) - event.getX(1);
                            //两点之间y的坐标差
                            float offsetY = event.getY(0) - event.getY(1);
                            //两点之间的距离
                            currentDistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY);
                            if (lastDistance < 0) {
                                //没有缩放
                                lastDistance = currentDistance;
                            } else {
                                if (currentDistance - lastDistance > 5) {//放大
                                    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) iv.getLayoutParams();
                                    lp.width = (int) (1.1f * iv.getWidth());
                                    lp.height = (int) (1.1f * iv.getHeight());
                                    iv.setLayoutParams(lp);
                                    lastDistance = currentDistance;
                                } else if (currentDistance - lastDistance < -5) {//缩小
                                    int currentIvWidth = iv.getWidth();
                                    int currentIvHeight = iv.getHeight();
                                    if (currentIvWidth > 50 && currentIvHeight >50) {
                                        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) iv.getLayoutParams();
                                        lp.width = (int) (0.9f * iv.getWidth());
                                        lp.height = (int) (0.9f * iv.getHeight());
                                        iv.setLayoutParams(lp);
                                        lastDistance = currentDistance;
                                    }
 
                                }
                            }
                        }
                        break;
                    case MotionEvent.ACTION_UP:
 
                        break;
                }
                return true;
            }
        });
 
 
    }
 
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
 
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
 
        return super.onOptionsItemSelected(item);
    }
}

xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".MainActivity">
 
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:src="@mipmap/a"
        android:layout_height="wrap_content" />
 
</RelativeLayout>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们