调试器为视图ID提供了不同的值,这取决于我是否在setNewDrawable中,或者在我发布Invalidate时是否在重写的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();
}
});
}
}