android - 如何实现整个 Activity 的多点触控?

标签 android android-layout android-widget multi-touch

我有一个具有线性布局的 Activity ,在这个布局中有几个线性布局,每个线性布局都设置了按钮和 TextView 。我想为整个屏幕实现多点触控功能意味着如果用户使用他的手指执行放大 - 缩小然后它应该放大和缩小整个屏幕(相应地增加和减少所有按钮, TextView 大小一次)。

如何使用android 2.1实现?

问候, 皮克斯

最佳答案

这可能会给你一个想法:

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
//import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;

public class MultitouchView extends View {
    private static final int STROKE_WIDTH = 1;
    private static final int CIRCLE_RADIUS = 20;

    private ArrayList<PointF> touchPoints = null;
    private Paint drawingPaint = null;
    private boolean isMultiTouch = false;
    private int pathEffectPhase = 0;

    public MultitouchView(Context context) {
        super(context);

        initialize(context);
    }

    public MultitouchView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        initialize(context);
    }

    public MultitouchView(Context context, AttributeSet attrs) {
        super(context, attrs);

        initialize(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(touchPoints.size() > 0)
        {
            DashPathEffect effect = new DashPathEffect(new float[] {7,7}, pathEffectPhase);
            PointF midpt = null;

            drawingPaint.setPathEffect(effect);

            for(int index=1; index<touchPoints.size(); ++index)
            {
                midpt = getMidPoint(
                        touchPoints.get(index - 1).x,touchPoints.get(index - 1).y,
                        touchPoints.get(index).x,touchPoints.get(index).y);

                canvas.drawCircle(
                        touchPoints.get(index - 1).x,touchPoints.get(index - 1).y, 
                        1, drawingPaint);
                canvas.drawCircle(
                        touchPoints.get(index - 1).x,touchPoints.get(index - 1).y, 
                        CIRCLE_RADIUS, drawingPaint);

                canvas.drawCircle(touchPoints.get(index).x,touchPoints.get(index).y, 
                        1, drawingPaint);
                canvas.drawCircle(touchPoints.get(index).x,touchPoints.get(index).y, 
                        CIRCLE_RADIUS, drawingPaint);

                canvas.drawLine(
                        touchPoints.get(index - 1).x,touchPoints.get(index - 1).y,
                        touchPoints.get(index).x,touchPoints.get(index).y,
                        drawingPaint);

                canvas.drawCircle(midpt.x,midpt.y, 10, drawingPaint);
            }

            ++pathEffectPhase;

            invalidate();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);

        int action = event.getAction() & MotionEvent.ACTION_MASK;

        switch(action)
        {
            case MotionEvent.ACTION_DOWN:
            {
                invalidate();

                break;
            }
            case MotionEvent.ACTION_POINTER_DOWN:
            {
                isMultiTouch = true;

                setPoints(event);
                invalidate();

                break;
            }
            case MotionEvent.ACTION_POINTER_UP:
            {
                isMultiTouch = false;

                break;
            }
            case MotionEvent.ACTION_MOVE:
            {
                if(isMultiTouch)
                {
                    setPoints(event);
                    invalidate();
                }

                break;
            }
        }

        return true;
    }   

    private void initialize(Context context){
        drawingPaint = new Paint();

        drawingPaint.setColor(Color.RED);
        drawingPaint.setStrokeWidth(STROKE_WIDTH);
        drawingPaint.setStyle(Paint.Style.STROKE);
        drawingPaint.setAntiAlias(true);

        touchPoints = new ArrayList<PointF>();
    }

    public void setPoints(MotionEvent event){
        touchPoints.clear();

        int pointerIndex = 0;

        for(int index=0; index<event.getPointerCount(); ++index)
        {
            pointerIndex = event.getPointerId(index);

            touchPoints.add(new PointF(event.getX(pointerIndex),event.getY(pointerIndex)));
        }
    }

    private PointF getMidPoint(float x1,float y1, float x2, float y2) {
        PointF point = new PointF();

        float x = x1 + x2;
        float y = y1 + y2;

        point.set(x / 2, y / 2);

        return point;
    }
}

关于android - 如何实现整个 Activity 的多点触控?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10946240/

相关文章:

java - 小部件未显示在小部件列表中

android - 更改搜索栏拇指的大小

Android - 如何在设备上安装签名的 APK?

android - 使用 Kotlin 的 fragment 中的 runOnUiThread() 方法

android - 以编程方式设置色调后,EditText 可绘制隐藏

android - 以编程方式更改布局的背景颜色

android - 在 x 轴上旋转动画

java - setOnClickListener 错误

java - R.java 包含错误的资源指针

java - 单击android中的按钮时数据库导出到xls文件?