多点触控不起作用

时间:2011-03-04 11:52:07

标签: android android-layout

我已创建自己的自定义视图,并使用画布绘制。之后我将自定义视图添加到线性布局中。我无法对线性布局应用多点触控。多点触摸适用于线性布局吗?如果是这样,请分享一个示例代码段。

这是我的代码段:

package com.hcl.MobileIdeaNapkin;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

public class NapkinActivity extends GraphicsActivity implements ColorPickerDialog.OnColorChangedListener {    


    private int item_id;
    public Canvas canvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(3);    
    }

    private Paint  mPaint;

    public void colorChanged(int color) {
        mPaint.setColor(color);
    }

    public class MyView extends View {

        private Bitmap  mBitmap;
        private Canvas  mCanvas ;
        private Path    mPath;
        private Paint   mBitmapPaint;

        public MyView(Context c) {
            super(c);

            mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(0xFFAAAAAA);

            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

            if(item_id==3){
                canvas.drawText(AppModel.contents, 100, 100, mPaint);
            }
            else{
                canvas.drawPath(mPath, mPaint);
            }

        }

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y) {
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }
        private void touch_move(float x, float y) {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;
            }
        }
        private void touch_up() {
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath.reset();
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;
            }
            return true;
        }
    }

    private static final int COLOR_MENU_ID = Menu.FIRST;
    private static final int ERASE_MENU_ID = Menu.FIRST + 1;
    private static final int TEXT_MENU_ID=Menu.FIRST+2;


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, COLOR_MENU_ID, 0, "Color");
        menu.add(0, ERASE_MENU_ID, 0, "Erase");
        menu.add(0,TEXT_MENU_ID,0,"Text");

       return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mPaint.setXfermode(null);
        mPaint.setAlpha(0xFF);

        switch (item.getItemId()) {

            case COLOR_MENU_ID:
                System.out.println(item.getItemId());
                new ColorPickerDialog(this, this, mPaint.getColor()).show();
                return true;

            case ERASE_MENU_ID:
                System.out.println(item.getItemId());
                mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
                return true;

            case TEXT_MENU_ID:
                item_id=item.getItemId();
                System.out.println(item.getItemId());
                new TextDialog(this).show();
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:4)

您的代码应该没有问题。你添加了:

<uses-feature android:name="android.hardware.touchscreen.multitouch"/>

到您的AndroidManifest.xml?此外,我相信您必须以Android 2.0及更高版本为目标才能运行。

只是一些建议。

-Dan