【发布时间】:2018-07-25 16:03:26
【问题描述】:
所以我试图在我的应用程序中不断更新 Box 的位置,但我检索数据的方式是来自静态方法。 我试图做以下但没有运气.. 希望有人能指导我如何正确地做到这一点。
Public class Foo extends service{
private ImageView box;
static int dataX;
static int dataY;
public void onCreate() {
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
box = new ImageView(this);
box.setImageResource(R.drawable.imageX);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.x = 0
params.y = 0
windowManager.addView(box, params);
// The Box position should be changing constantly from the data I retrieve
box.setX(dataX);
box..setY(dataY);
windowManager.updateViewLayout(box, params);
..
..
// and the code above is not working, since im not passing the data correctly
}
public static void Data(int x, int y) {
// the data is being passed to this method
// x and y contains dynamic data which changes constantly
dataX = x;
dataY = y;
}
}
【问题讨论】:
-
为什么使用静态方法?是必须的吗?
-
是的,在这种情况下它对我来说是必须的 :-)
-
好的,我认为您不应该更改 onCreate() 方法中的位置,因为它只调用一次尝试查找方法会频繁调用或尝试删除并再次重新绘制以强制 onCreate () 工作
-
@Sundar 说:“它应该可以工作。由于您的代码很好,能否请您显示您尝试设置为 Box 的代码。由于它是静态变体,它包含的数据是什么我们在 Data 方法中设置”,我同意:如果 Box 与 Point 类似,那么我看不出您发布的代码有任何问题
-
但它没有:-)。我提供了更多代码
标签: java android static-methods