android: clean my view when I click button

江枫思渺然 提交于 2019-12-11 17:16:32

问题


I create a view and use canvas drawing something.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyViewCircle myViewCircle = new MyViewCircle(this);
        setContentView(R.layout.main);
        Button evolve = (Button) findViewById(R.id.evolve); 
        img01.addView(myViewCircle);
        evolve.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
    }
private class MyViewCircle extends View {

        public MyViewCircle(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.RED);
            canvas.drawCircle(50, 50, 100, paint);
        }

    }

How can I write the code to clean up my canvas when I click the button? Also, how can change my shape when I click the button? I want to do change coordinates:

canvas.drawCircle(150, 150, 200, paint);

回答1:


onDraw() is called if you click the button and your button changes, so you can check in the onDraw() method if the button was clicked.




回答2:


Try

 @Override
    protected void onDraw(Canvas canvas) {
        if(shouldDraw){
           super.onDraw(canvas);
           Paint paint = new Paint();
           paint.setAntiAlias(true);
           paint.setColor(Color.RED);
           canvas.drawCircle(50, 50, 100, paint);
        }else
           Canvas.drawColor(Color.BLACK);

    }

and in the onClick

shouldDraw = false;
view.invalidate();



回答3:


what is img01, if its another layout/view, you can call Ex: img01.removeAllViews().



来源:https://stackoverflow.com/questions/8261239/android-clean-my-view-when-i-click-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!