我正在尝试构建一个绘图应用程序。首先,我试图在屏幕上画一些东西,这是可行的。现在我正试着画一个圆圈来跟随我的手指。这在应用程序运行时不起作用,并且编译时没有错误或警告。出了什么问题,我该如何解决?
我认为onTouchEvent
永远不会被调用。
我的视图类:
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 }