添加延迟视图

时间:2012-02-14 08:25:32

标签: android view handler

我试图在向主视图添加视图时添加延迟,但同时查看矛。请帮忙。

 final Handler handler = new Handler();
    LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
    final HorizontalScrollView hsv = new HorizontalScrollView(TestViewActivity.this);
    final LinearLayout lhsv = new LinearLayout(TestViewActivity.this);

    final Animation a1 = new AlphaAnimation(0.00f, 1.00f);
    a1.setDuration(350);
    a1.setFillAfter(true);  
    for(int k =0; k < 5; k++){
         new Handler().postDelayed(new Runnable() {
                public void run() {
                    //write your code here...
                    TextView tv = new TextView(TestViewActivity.this);  
                    tv.setText("Text");
                    tv.setTextSize(42);
                    tv.setPadding(10, 0, 10, 0);
                    tv.setVisibility(View.INVISIBLE);
                    tv.clearAnimation();   
                    tv.startAnimation(a1);
                    lhsv.addView(tv, temp);
                    temp++;
                }
            }, 2000);
        }

    hsv.addView(lhsv);
    ll.addView(hsv);

temp是静态int。

1 个答案:

答案 0 :(得分:2)

根据你的代码,结果可能是在2秒后同时添加所有视图,对吧? 延迟时间必须由下面的int k改变。

for(int k =0; k < 5; k++){
     handler.postDelayed(new Runnable() {
            public void run() {
                //write your code here...
                TextView tv = new TextView(TestViewActivity.this);  
                tv.setText("Text");
                tv.setTextSize(42);
                tv.setPadding(10, 0, 10, 0);
                tv.setVisibility(View.INVISIBLE);
                tv.clearAnimation();   
                tv.startAnimation(a1);
                lhsv.addView(tv, temp);
                temp++;
            }
        }, 2000 + 2000 * k);
    }

我建议不要制作不必要的处理程序。只需将runnables发布到一个处理程序。