如何实现对整个活动的多点触控? [英] How to achieve multitouch for whole activity?

查看:81
本文介绍了如何实现对整个活动的多点触控?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用线布局一个活动,这里面布置有几个线性布局和每一个线性布局,如果有按钮和文本视图设置。我想实现多点触摸功能对于整个屏幕意味着,如果用户进行放大,缩小使用他的手指那么它应该放大和缩小整个屏幕(增加和减少的所有按钮,文本视图的大小相应的一次)。

I have one activity which is having linear layout and inside this layout there are several linear layouts and each linear layout is having set if buttons and text views. I want to achieve multi touch feature for whole screen means if user perform zoom in-zoom out using his finger then it should zoom in and zoom out whole screen(increase and decrease all buttons,text views size accordingly once).

如何采用了Android 2.1能实现吗?

How to achieve it using android 2.1?

问候,
皮克斯

regards, Piks

推荐答案

这可能会给你一个想法:

This might give you an idea:

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

这篇关于如何实现对整个活动的多点触控?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆