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 }
如果你想,當你拖動你的手指畫,你需要編寫代碼來實現在通過Fingerpaint試玩會'MotionEvent'常量... – andre3wap