如何从活动中调用自定义视图对象的更改
我有一个绘制到画布上的自定义视图。当用户从该视图所绑定的活动中按下按钮时,我试图调用对画布的更改。目前,我尝试通过调用我在视图内创建的名为 setNewDrawable 的公共方法来执行此操作。
当我发布 Invalidate 时,调试器为我提供不同的视图 ID 值,具体取决于我是在 setNewDrawable 内部还是在 Overridden onDraw 方法内部。
例如,调试器变量分别在 setNewDrawable 和 onDraw 中显示: (This - MyView id=830067720176) 或 (This - MyView id=830067712344) 。这让我觉得我基本上拥有该对象的两个副本,而我基本上正在与错误的副本进行交互。
如何向我的自定义视图获取信息以确定它绘制的内容?
这是我正在运行的代码。
public class Main extends Activity {
MyView mView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.main, null);
mView= (MyView) v.findViewById(R.id.my_view);
//A button to modify what's drawn on the canvas
Button switchLeft = (Button) findViewById(R.id.switch_left);
switchLeft.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mView.setNewDrawable();
mView.postInvalidate();
}
});
}
}
谢谢大家!
I have a custom view that draws to a canvas. I am trying to invoke changes to the canvas when the user pushes a button from the activity that this view is tied to. Currently, I am attempting to do this by calling a public method, called setNewDrawable, that I created inside the view.
The debugger is giving me different values for the view ID depending on whether I'm inside setNewDrawable or inside the Overridden onDraw method when I post an Invalidate.
For example the debugger variables show: (This - MyView id=830067720176) or (This - MyView id=830067712344) in setNewDrawable and onDraw respectively. This makes me think I basically have two copies of the object and I am essentially interacting with the wrong one.
How can I get information to my custom View in order to determine what it draws?
Here is the code I am running.
public class Main extends Activity {
MyView mView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.main, null);
mView= (MyView) v.findViewById(R.id.my_view);
//A button to modify what's drawn on the canvas
Button switchLeft = (Button) findViewById(R.id.switch_left);
switchLeft.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mView.setNewDrawable();
mView.postInvalidate();
}
});
}
}
Thanks all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在充气机的布局上。我把它去掉了,效果很好。
The problem was the layout inflater. I removed it and it worked fine.