安卓图像多点触碰

基于Empty Activity模板创建安卓应用后导入图片和背景/
在这里插入图片描述
打开String更改标题
在这里插入图片描述
约束布局改为线性布局并增加图像控件
在这里插入图片描述
然后在MainActivity里编辑剩余代码。

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private LinearLayout root;
    private ImageView ivMickey;
    private float x1, y1;
    private float x2, y2;
    private float nextX1, nextY1;
    private float nextX2, nextY2;
    private float distance;
    private float nextDistance;
    private LinearLayout.LayoutParams layoutParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        root = findViewById(R.id.root);
        ivMickey = findViewById(R.id.iv_mickey);
        root.setFocusable(true);
        root.requestFocus();
        layoutParams = (LinearLayout.LayoutParams) ivMickey.getLayoutParams();
        root.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getPointerCount() == 1) {
                    if (event.getAction() == MotionEvent.ACTION_MOVE) {
                        layoutParams.leftMargin = (int) (event.getX() - ivMickey.getWidth() / 2);
                        layoutParams.topMargin = (int) (event.getY() - ivMickey.getHeight() / 2);
                    }
                } else if (event.getPointerCount() == 2) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            x1 = event.getX(0);
                            y1 = event.getY(0);
                            x2 = event.getX(1);
                            y2 = event.getY(1);
                            distance = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
                            break;
                        case MotionEvent.ACTION_MOVE:
                            nextX1 = event.getX(0);
                            nextY1 = event.getY(0);
                            nextX2 = event.getX(1);
                            nextY2 = event.getY(1);
                            nextDistance = (float) Math.sqrt((nextX1 - nextX2) * (nextX1 - nextX2) + (nextY1 - nextY2) * (nextY1 - nextY2));
                            break;

                        case MotionEvent.ACTION_UP:
                            break;
                    }
                    if (nextDistance > distance) {
                        if (layoutParams.width < 1200) {
                            layoutParams.width = (int) (layoutParams.width * 1.05);
                            layoutParams.height = (int) (layoutParams.height * 1.05);
                        }
                    } else {
                        if (layoutParams.width > 10) {
                            layoutParams.width = (int) (layoutParams.width / 1.05);
                            layoutParams.height = (int) (layoutParams.height / 1.05);
                        }
                    }
                    x1 = nextX1;
                    y1 = nextY1;
                    x2 = nextX2;
                    y2 = nextY2;
                    distance = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));



                }
                ivMickey.setLayoutParams(layoutParams);
                return true;
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值