我的Android应用程序中的onTouch方法问题

时间:2016-02-05 07:16:03

标签: android

我正在开发一个Android项目,在onTouch触发MotionEvent.ACTION_UP时我无法删除@Override public boolean onTouch(View v, MotionEvent event) { int x = 0; int y = 0; View point = null; switch (event.getAction()){ case MotionEvent.ACTION_DOWN: x = (int) event.getX(); y = (int) event.getY(); if(touch_circle(x,y)) { point = new Circle_draw(getApplicationContext(),x,y); layout.addView(point); } break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } return true; } 方法的视图。

我的代码如下

public class Circle_draw extends View{

    int width;
    int height;
    public Circle_draw(Context context,int width,int height) {
        super(context);
        this.width = width;
        this.height = height;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLUE);
        canvas.drawCircle(width,height,5,paint);
    }

}

以及与此相关的更多代码如下

Action_UP

如何从我的活动类中删除此视图?使用public class MainActivity extends Activity implements View.OnTouchListener{ RelativeLayout layout; int height; int width; View control; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setVariables(); layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); layout.addView(control); control.setOnTouchListener(this); } public void setVariables(){ layout = (RelativeLayout) findViewById(R.id.controllerLayout); height = getWindowManager().getDefaultDisplay().getHeight(); width = getWindowManager().getDefaultDisplay().getWidth(); control = new ThumbController(getApplicationContext()); } @Override public boolean onTouch(View v, MotionEvent event) { int x = 0; int y = 0; View point = null; switch (event.getAction()){ case MotionEvent.ACTION_DOWN: x = (int) event.getX(); y = (int) event.getY(); if(touch_circle(x,y)) { point = new Circle_draw(getApplicationContext(),x,y); layout.addView(point); } break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } return true; } public boolean touch_circle(int x,int y){ boolean touch = false; x=x-width; y=y-height; int distance = (int) Math.sqrt(Math.pow(x,2) + Math.pow(y,2)); //Toast.makeText(getApplicationContext(),"distance=" + String.valueOf(distance),Toast.LENGTH_SHORT).show(); if(distance<=120){ touch = true; } return touch; } public class ThumbController extends View{ public ThumbController(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int radius = 120; Paint paint = new Paint(); width = width-75; height = height-150; paint.setStyle(Paint.Style.FILL); paint.setColor(Color.GREEN); canvas.drawCircle(width, height, radius, paint); } } public class Circle_draw extends View{ int width; int height; public Circle_draw(Context context,int width,int height) { super(context); this.width = width; this.height = height; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(Color.BLUE); canvas.drawCircle(width,height,5,paint); } }

此活动的整体代码如下: -

// Enable the trash button if the asset can be deleted.
    BOOL isTrashable = NO;
   if (self.assetCollection) {
    isTrashable = [self.assetCollection canPerformEditOperation:PHCollectionEditOperationRemoveContent];
} 
   else {
     isTrashable = [self.asset canPerformEditOperation:PHAssetEditOperationDelete];
}
   self.trashButton.enabled = isTrashable;
   [self updateImage];

}

0 个答案:

没有答案