我试图让三个单独的条在屏幕上“浮动”,但由于某种原因,只有一个可见。这是我的代码:
// initialized the bar
ShapeDrawable barShape = new ShapeDrawable(new RectShape());
barShape.getPaint().setColor(Color.CYAN);
barShape.setIntrinsicHeight(20);
barShape.setIntrinsicWidth(mDisplayWidth);
// defines an imageview with the bar in it
mBarView1 = new ImageView(getApplicationContext());
mBarView1.setImageDrawable(barShape);
mBarView1.setTranslationX(0);
mBarView1.setTranslationY(0);
// Copies the bars
mBarView2 = new ImageView(getApplicationContext());
mBarView2.setImageDrawable(barShape);
mBarView3 = new ImageView(getApplicationContext());
mBarView3.setImageDrawable(barShape);
// Adds the bars to the grid
((LinearLayout) findViewById(R.id.linear_layout)).addView(mBarView1);
((LinearLayout) findViewById(R.id.linear_layout)).addView(mBarView2);
((LinearLayout) findViewById(R.id.linear_layout)).addView(mBarView3);
mBarView2.setTranslationX(0);
mBarView2.setTranslationY(50);
mBarView3.setTranslationX(0);
mBarView3.setTranslationY(150);
// updates the gates every 10 milliseconds
final Handler h = new Handler();
final int delay = 10;//milli seconds
h.postDelayed(new Runnable() {
public void run() {
moveBars();
h.postDelayed(this, delay);
}
}, delay);
这是我的moveBars()
方法:
private void moveBars() {
mBarView1.setTranslationY(mBarView1.getY() + 1);
mBarView2.setTranslationY(mBarView2.getY() + 10);
mBarView3.setTranslationY(mBarView3.getY() + 100);
}
我认为这可能是因为它们指的是同一个ShapeDrawable
对象,但如果这是真的,我该如何解决?