Android中的多点触摸交互处理,可以达到缩放图片的效果

比较重要的方法:
(1)root.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {。。。。。。。。。。。。。。。。。。。。。  在这里view指的就是调用了setOnTouchListener方法的root.官方文档是这样说的。
(2)
RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams()。这个方法中得到的是某个view的layoutParams。
(3)
event.getPointerCount()。这个方法中是计算有多少个触摸点在手机上。
(4)
event.getX(0) , event.getX(1)里面的参数指定的是第几个触摸点的X的坐标
(5)图片缩放的基本原理:首先我们指定是只有俩个触摸点。然后我们判断来个触摸点之间的距离。如果现在的俩个点之间的距离比先前的俩个触摸点之间的距离大的话,那么就进行放大,否则的话,就是缩小了。
核心代码如下:
if (event.getPointerCount() >= 2) {
    float offsetX = event.getX(0) - event.getX(1);
    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) {
        lastdistance=currentdistance;
        RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        layoutParams.width = (int) (1.1f * imageView.getWidth());
        layoutParams.height = (int) (1.1f * imageView.getHeight());
        imageView.setLayoutParams(layoutParams);
        System.out.println("放大");
    }else if (lastdistance - currentdistance > 5) {
        lastdistance=currentdistance;
        RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams();
        layoutParams.width = (int) (0.9f * imageView.getWidth());
        layoutParams.height = (int) (0.9f * imageView.getHeight());
        imageView.setLayoutParams(layoutParams);
        System.out.println("缩小");
    }
}

布局文件中,只有一个ImageView.
MainActivity中的内容:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {
    private RelativeLayout root;
    private ImageView imageView;

    private float  currentdistance;
    private float lastdistance=-1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        root = (RelativeLayout) findViewById(R.id.root);
        imageView = (ImageView) findViewById(R.id.iv);
        root.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        System.out.println("down");
                        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);
                            currentdistance = (float) Math.sqrt(offsetX * offsetX + offsetY * offsetY);
                        }
                        if (lastdistance < 0) {
                            lastdistance = currentdistance;
                        }else {
                            if (currentdistance - lastdistance > 5) {
                                lastdistance=currentdistance;
                                RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams();
                                layoutParams.width = (int) (1.1f * imageView.getWidth());
                                layoutParams.height = (int) (1.1f * imageView.getHeight());
                                imageView.setLayoutParams(layoutParams);
                                System.out.println("放大");
                            }else if (lastdistance - currentdistance > 5) {
                                lastdistance=currentdistance;
                                RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams();
                                layoutParams.width = (int) (0.9f * imageView.getWidth());
                                layoutParams.height = (int) (0.9f * imageView.getHeight());
                                imageView.setLayoutParams(layoutParams);
                                System.out.println("缩小");
                            }
                        }
                       /* RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) imageView.getLayoutParams();
                        layoutParams.topMargin= (int) event.getX();
                        layoutParams.leftMargin = (int) event.getY();
                        imageView.setLayoutParams(layoutParams);
                        System.out.println("move");*/
                        break;
                    case MotionEvent.ACTION_UP:
                        System.out.println("up");
                        break;
                }
                return true;
            }
        });


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @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);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱coding的同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值