画布绘图在android中不起作用

时间:2013-07-24 11:25:40

标签: android touch android-canvas

我已经为学习目的做了一个简单的android绘画程序,但是ontouch笔画不是绘图,我试过如下: 的 main.java

package com.example.mypen;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.Menu;
import android.view.*;
import android.view.MotionEvent;

public class MainActivity extends Activity{
    private Paint paint = new Paint();
      private Path path = new Path();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
    }

    public void onDraw(Canvas canvas){
        canvas.drawPath(path,paint);
        }
    public boolean onTouchEvent(MotionEvent me){
        float eventX=me.getX();
        float eventY=me.getY();

        switch (me.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(eventX, eventY);
            break;
        case MotionEvent.ACTION_UP:
            break;
        }
        return true;

    }

}

1 个答案:

答案 0 :(得分:1)

尝试在onTouchEvent方法

中使视图无效