【问题标题】:how to add imageview to layer with delay in java如何在java中延迟将imageview添加到图层
【发布时间】:2017-08-08 01:41:06
【问题描述】:

线程和睡眠和处理程序没有帮助。 这是我的代码。

while(true){
        ImageView image = new ImageView(context);
        image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
        image.setMaxHeight(80);
        image.setMaxWidth(50);
        image.setImageResource(R.drawable.missile);


        TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 10.0f, -800.0f);          //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
        animation.setDuration(700);   
        animation.setFillAfter(true); 
        image.startAnimation(animation);
        LinearLayout missile_layout = (LinearLayout)findViewById(R.id.missile_layout);
        missile_layout.addView(image,0);
    } 

【问题讨论】:

  • 如果延迟发布不是您所需要的 - 尝试解释您需要什么样的延迟以及为什么需要延迟?
  • 我想延迟一个一个地添加imageviews

标签: java android eclipse android-studio


【解决方案1】:

试试这个

    // flag that should be set true if handler should stop
 boolean mStopHandler = false;
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // load the layout
  setContentView(R.layout.filters);
  Runnable runnable = new Runnable() {
   @Override
   public void run() {
    addView();
    if (!mStopHandler) {
     mHandler.postDelayed(this, 1000);
    }
   }
  };

  // start it with:
  mHandler.post(runnable);
 }






 public void addView() {
  ImageView image = new ImageView(context);
  image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80, 60));
  image.setMaxHeight(80);
  image.setMaxWidth(50);
  image.setImageResource(R.drawable.missile);


  TranslateAnimation animation = new TranslateAnimation(0.0 f, 0.0 f, 10.0 f, -800.0 f); //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
  animation.setDuration(700);
  animation.setFillAfter(true);
  image.startAnimation(animation);
  LinearLayout missile_layout = (LinearLayout) findViewById(R.id.missile_layout);
  missile_layout.addView(image, 0);
 }

【讨论】:

  • 谢谢。但不行。所有的图像视图加在一起。
  • 要一个一个添加吗?
  • 请问。mHandler是什么?
  • 只是Handler的一个对象
  • 很好,thanks.solved.but first Y_location of imageviews 逐渐减少!换句话说,imageviews 将起点移动到屏幕顶部
【解决方案2】:

这应该可行:

while(true){
        ImageView image = new ImageView(context);
        image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
        image.setMaxHeight(80);
        image.setMaxWidth(50);
        image.setImageResource(R.drawable.missile);


        TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 10.0f, -800.0f);          //  new TranslateAnimation(xFrom,xTo, yFrom,yTo)
        animation.setDuration(700);   
        animation.setFillAfter(true); 
        image.startAnimation(animation);
        LinearLayout missile_layout = (LinearLayout)findViewById(R.id.missile_layout);

    missile_layout.postDelayed(new Runnable() {
        public void run() {
            missile_layout.addView(image,0);
        }
    }, 5000
);
}

【讨论】:

  • 谢谢。但不行。所有的图像视图加在一起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
相关资源
最近更新 更多