2016-06-21 111 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