我的应用程序的底层视图在画布上绘制了位图、线条等。我想在这上面显示图像(图标),这将响应触摸事件。这些图像的数量和位置将根据SQLite数据库中的数据而变化。
我认为实现这一点的方法是拥有一个ViewGroup,首先为每个图标添加基本视图,然后为每个图标添加一个带有OnTouch侦听器的ImageButton对象,并通过在RelativeLayout.LayoutParams对象中设置边距来定义位置,这样的想法正确吗?
我为每个按钮编写的代码如下:`
public class MyButton {
ImageButton mButton;
private Bitmap Star;
public MyButton(int intX, int intY) {
mButton = new ImageButton(Global.thisApp);
mButton.setImageBitmap(BitmapFactory.decodeResource(Global.thisApp.getResources(), R.drawable.star)r);
mButton.setPadding(0, 0, 0, 0);
mButton.setOnTouchListener(new ImageButton.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// ToDo
return false;
}
});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(intX, intY, 0, 0);
Global.thisApp.mLayout.addView(mButton, params);
}}
`创建按钮的代码如下:
poiCount = 0;
mCursor=Global.mDBAdapter.fetchItems(...);
if (mCursor.getCount()>=0) {
poi = new MyButton[mCursor.getCount()];
mCursor.moveToFirst();
do {
int X = ... ;
int Y = ... ;
poi[poiCount] = new MyButton("Type", X, Y);
poiCount++;
} while (mCursor.moveToNext());
}这段代码是从底层视图的onDraw()方法调用的。
当最初绘制屏幕时,这似乎是有效的,当屏幕随后重新绘制(具有新数据)时,添加了新的按钮;但是,以前的按钮没有删除。
我尝试使用每个按钮作为参数来调用Global.ThisApp.MyLayout.removeView,但是MyButton对象不能作为removeView的参数。
如何在每次基础视图无效时删除现有按钮?
发布于 2010-01-15 05:46:50
MyButton应该扩展视图(在您的情况下是ImageButton),以便在ViewGroup中添加或删除。
https://stackoverflow.com/questions/2065692
复制相似问题