按钮单击时无效的画布?

时间:2012-04-26 11:47:09

标签: java android button invalidation

我点击按钮时试图使画布无效。我有一个带按钮的布局,下面有一个画布视图。当我点击按钮时,圆圈应隐藏或显示。在代码现在我只能使我的画布无效一次。当我第一次按下按钮时。但是当我第二次按它时它不起作用。示例:如果我按下隐藏,则显示它有效。但是,当我再次按下隐藏按钮时,它不起作用。当我点击一个按钮时,我希望CanvasView每次都无效。不仅是第一次。

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);
        }
    }
}

如果某人有解决方案可以解决我的许多问题。

1 个答案:

答案 0 :(得分:2)

您似乎在按钮处理程序中创建了一个新的画布视图实例

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

你可能想继续引用它,不是吗?

编辑: 在layout()方法中创建一个视图实例。 cv这里是对它的引用。它是您的方法的本地:

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

你想让它成为全球性的(即类字段)。基本上是一个Java实例变量(就像你的布尔“showCircle”标志)。您可以从侦听器代码中使用它,就像更改布尔“showCircle”标志一样。

另外,我建议您阅读有关使用XML创建布局和使用findViewById方法查找视图的更多信息。

EDIT2:如果您是实例变量,您的代码将看起来像这样。 (我在记事本中输入的可能错误):

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);
        }
    }
}