2016-06-21 90 views
0

我正在尝试构建一个绘图应用程序。首先,我试图在屏幕上绘制一些东西,这是行得通的。现在我正在努力制作一个圈子来跟随我的手指。 这不适用于应用程序运行,编译时没有错误或警告。怎么了,我该如何解决?没有反应onTouchEvent Android

我认为onTouchEvent永远不会被调用。

我viewClass类:

package gorrebeeck.david.dgor.eazydraw; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.drawable.ShapeDrawable; 
import android.graphics.drawable.shapes.OvalShape; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Toast; 

public class CustomDrawableView extends View { 
    private ShapeDrawable mDrawable; 

    int x = 10; 
    int y = 10; 

    @Override 
    public boolean onTouchEvent(MotionEvent e) { 
     this.x=(int)e.getX(); 
     this.y=(int)e.getY(); 
     this.invalidate(); 
     return true; 
    } 

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

     int width = 50; 
     int height = 50; 

     mDrawable = new ShapeDrawable(new OvalShape()); 
     mDrawable.getPaint().setColor(0xff74AC23); 
     mDrawable.setBounds(x, y, x + width, y + height); 
    } 

    protected void onDraw(Canvas canvas) { 
     mDrawable.draw(canvas); 
    } 
} 

类加载这就是:

package gorrebeeck.david.dgor.eazydraw; 

import android.content.Intent; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Toast; 

import java.util.Random; 

public class workspace extends AppCompatActivity { 

    CustomDrawableView mCustomDrawableView; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mCustomDrawableView = new CustomDrawableView(this); 

     setContentView(mCustomDrawableView); 

    } 
} 

警告运行时:

06-21 16:05:14.834 2380-2543/gorrebeeck.david.dgor.eazydraw E/Surface:getSlotFromBufferLocked: unknown buffer: 0x7ffe7a162850 
06-21 16:05:14.839 2380-2543/gorrebeeck.david.dgor.eazydraw D/OpenGLRenderer: endAllStagingAnimators on 0x7ffe71d90400 (RippleDrawable) with handle 0x7ffe71d1aac0 
06-21 16:05:16.719 2380-2380/gorrebeeck.david.dgor.eazydraw W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=435.89355, y[0]=759.9121, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=63310, downTime=61173, deviceId=0, source=0x1002 } 
06-21 16:05:16.719 2380-2380/gorrebeeck.david.dgor.eazydraw W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=435.89355, y[0]=759.9121, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=63310, downTime=61173, deviceId=0, source=0x1002 } 
06-21 16:05:16.719 2380-2380/gorrebeeck.david.dgor.eazydraw W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=435.89355, y[0]=759.9121, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=63310, downTime=61173, deviceId=0, source=0x1002 } 
06-21 16:05:16.719 2380-2380/gorrebeeck.david.dgor.eazydraw W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=435.89355, y[0]=759.9121, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=63310, downTime=61173, deviceId=0, source=0x1002 } 
+0

如果你想,当你拖动你的手指画,你需要编写代码来实现在通过Fingerpaint试玩会'MotionEvent'常量... – andre3wap

回答

0

这里是一个样本,试试这个

public class MainActivity extends Activity { 

DrawingView dv ; 
private Paint mPaint;  

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    dv = new DrawingView(this); 
    setContentView(dv); 
    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 
    mPaint.setColor(Color.GREEN); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.ROUND); 
    mPaint.setStrokeCap(Paint.Cap.ROUND); 
    mPaint.setStrokeWidth(12); 
} 

public class DrawingView extends View { 

    public int width; 
    public int height; 
    private Bitmap mBitmap; 
    private Canvas mCanvas; 
    private Path mPath; 
    private Paint mBitmapPaint; 
    Context context; 
    private Paint circlePaint; 
    private Path circlePath; 

    public DrawingView(Context c) { 
     super(c); 
     context=c; 
     mPath = new Path(); 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 
     circlePaint = new Paint(); 
     circlePath = new Path(); 
     circlePaint.setAntiAlias(true); 
     circlePaint.setColor(Color.BLUE); 
     circlePaint.setStyle(Paint.Style.STROKE); 
     circlePaint.setStrokeJoin(Paint.Join.MITER); 
     circlePaint.setStrokeWidth(4f); 
    } 

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

     mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     mCanvas = new Canvas(mBitmap); 
    } 

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

     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
     canvas.drawPath(mPath, mPaint); 
     canvas.drawPath(circlePath, circlePaint); 
    } 

    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; 

      circlePath.reset(); 
      circlePath.addCircle(mX, mY, 30, Path.Direction.CW); 
     } 
    } 

    private void touch_up() { 
     mPath.lineTo(mX, mY); 
     circlePath.reset(); 
     // 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; 
    } 
} 

}

开始由SDK样本

+0

谢谢,代码正常工作,我现在正在植入。 – user3623638