gpt4 book ai didi

java - 单击按钮时 Canvas 无效?

转载 作者:行者123 更新时间:2023-12-02 07:50:06 24 4
gpt4 key购买 nike

当我点击按钮时,我试图使我的 Canvas 无效。我有一个带有按钮的布局,下面有一个 Canvas View 。当我单击按钮时,圆圈应该隐藏或显示。在现在的代码中,我只能使 Canvas 无效一次。当我第一次按下按钮时它起作用了。但是当我按第二次时就不起作用了。示例:如果我按隐藏,则显示它有效。但当我再次按下隐藏按钮时,它不起作用。当我单击按钮时,我希望 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);
}
}
}

如果有人能解决这个问题,那么我的许多问题就会得到解决。

最佳答案

您似乎正在按钮处理程序中创建一个新的 Canvas View 实例

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

您可能想保留对它的引用,不是吗?

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

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

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

此外,我建议阅读有关使用 XML 创建布局和使用 findViewById 方法定位 View 的更多信息。

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

关于java - 单击按钮时 Canvas 无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10332761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com