简体   繁体   English

按钮单击时无效的画布?

[英]Invalidate a canvas on button click?

Im trying to invalidate my canvas when i click on a button. 我点击一个按钮时试图使我的画布无效。 I have one layout with the buttons and under i have a canvas view. 我有一个带按钮的布局,下面有一个画布视图。 When i click on a button the circle should hide or be shown. 当我点击按钮时,圆圈应隐藏或显示。 In the code now I can invalidate my canvas only one time. 在代码现在我只能使我的画布无效一次。 When i press the button the first time it works. 当我第一次按下按钮时。 But when i press the second time it doesn't work. 但是当我第二次按它时它不起作用。 Example: If i press hide, then show it works. 示例:如果我按下隐藏,则显示它有效。 But when i press the hide button again it doesn't work. 但是,当我再次按下隐藏按钮时,它不起作用。 When I click on a button I want the CanvasView to invalidate everytime. 当我点击一个按钮时,我希望CanvasView每次都无效。 Not only the first time. 不仅是第一次。

public class CanvasWithButtonsActivity extends Activity {
    boolean showCircle = true;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout());

        findViewById(R.id.buttonHide).setOnClickListener(
                new OnClickListener() {      
                public void onClick(View v) {
                    //HIDE
                    showCircle = false;
                    //How do i invalidate my canvas from here?
                    CanvasView cv = new CanvasView(getApplicationContext());
                        cv.invalidate();
                }  
        });

        findViewById(R.id.buttonShow).setOnClickListener(
                new OnClickListener() {      
                public void onClick(View v) {
                    //SHOW
                    showCircle = true;
                    //How do i invalidate my canvas from here?
                    CanvasView cv = new CanvasView(getApplicationContext());
                        cv.invalidate();
                }  
        });
    }

    public RelativeLayout layout(){
        RelativeLayout mainLayout = new RelativeLayout(this);
        mainLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT, 1));
        mainLayout.setBackgroundColor(Color.WHITE);

        View buttonLayout = LayoutInflater.from(getBaseContext()).inflate(R.layout.main, null);
        buttonLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT, 1));
        CanvasView cv = new CanvasView(getApplicationContext());
        mainLayout.addView(cv);
        mainLayout.addView(buttonLayout);

        return mainLayout;
    }

    private class CanvasView extends View{

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

        protected void onDraw(Canvas myCanvas){
            Paint myPaint = new Paint();
            myPaint.setColor(Color.BLUE);
            if(showCircle == true)
                myCanvas.drawCircle(myCanvas.getWidth()/2, 100, 20, myPaint);
        }
    }
}

If someone have a solution to this question it would solve many of my problems. 如果有人能够解决这个问题,它将解决我的许多问题。

You seem to be creating a new canvas view instance in your button handler 您似乎在按钮处理程序中创建了一个新的画布视图实例

CanvasView cv = new CanvasView(getApplicationContext());
cv.invalidate();

You probably want to keep a reference to it, don't you? 你可能想继续引用它,不是吗?

EDIT: in your layout() method you create a view instance. 编辑:在layout()方法中创建一个视图实例。 cv here is a reference to it. cv这里是对它的引用。 it's local for your method: 它是您的方法的本地:

CanvasView cv = new CanvasView(getApplicationContext());
mainLayout.addView(cv);
mainLayout.addView(buttonLayout);

you want to make it global (ie class field). 你想让它成为全局的(即类字段)。 Basically a Java instance variable (like your boolean "showCircle " flag). 基本上是一个Java实例变量(就像你的布尔“showCircle”标志)。 You can use it from your listeners code, the same way you change the boolean "showCircle" flag. 您可以从侦听器代码中使用它,就像更改布尔“showCircle”标志一样。

Also I'd recommend reading more about creating layouts using XML and locating views using findViewById method. 另外,我建议阅读更多关于使用XML创建布局和使用findViewById方法查找视图的内容。

EDIT2: If you instance variable your code will look something like this. EDIT2:如果你的实例变量你的代码看起来像这样。 (Possible errors I was typing in a notepad): (我在记事本中输入的可能错误):

public class CanvasWithButtonsActivity extends Activity {
    boolean showCircle = true;
    private CanvasView mCanvasView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout());

        findViewById(R.id.buttonHide).setOnClickListener(
                new OnClickListener() {      
                public void onClick(View v) {
                    //HIDE
                    showCircle = false;
                    //How do i invalidate my canvas from here?
                    if (mCanvasView != null) {
                        mCanvasView.invalidate();
                    }
                }  
        });

        findViewById(R.id.buttonShow).setOnClickListener(
                new OnClickListener() {      
                public void onClick(View v) {
                    //SHOW
                    showCircle = true;
                    //How do i invalidate my canvas from here?
                    if (mCanvasView != null) {
                        mCanvasView.invalidate();
                    }
                }  
        });
    }

    public RelativeLayout layout(){
        RelativeLayout mainLayout = new RelativeLayout(this);
        mainLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT, 1));
        mainLayout.setBackgroundColor(Color.WHITE);

        View buttonLayout = LayoutInflater.from(getBaseContext()).inflate(R.layout.main, null);
        buttonLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT, 1));
        mCanvasView = new CanvasView(getApplicationContext());
        mainLayout.addView(mCanvasView);
        mainLayout.addView(buttonLayout);

        return mainLayout;
    }

    private class CanvasView extends View{

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

        protected void onDraw(Canvas myCanvas){
            Paint myPaint = new Paint();
            myPaint.setColor(Color.BLUE);
            if(showCircle == true)
                myCanvas.drawCircle(myCanvas.getWidth()/2, 100, 20, myPaint);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM