在android中添加视图时添加延迟

时间:2012-02-14 06:58:19

标签: android handler postdelayed

我有一个简单的动画附加到我正在创建的动态textview但我想要的是添加它们时添加延迟。请指导我如何做到这一点。

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

    Animation a1 = new AlphaAnimation(0.00f, 1.00f);
    a1.setDuration(350);
    a1.setFillAfter(true);  

    for(int k =0; k < 5; k++){
        // may be some handler here but how ?
        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, k);
    }

    hsv.addView(lhsv);

    ll.addView(hsv);

由于

根据建议,我已经尝试过它可以工作,但是所有视图都在一起,我想要的是一个视图输入然后一点延迟然后另一个视图输入等等......这是代码。

   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...
                    final 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);

3 个答案:

答案 0 :(得分:6)

使用此

 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            //write your code here...
        }
    }, delay_time);

答案 1 :(得分:0)

试试AysncTask。这是出于同样的原因。

答案 2 :(得分:0)

试试这个......

 for(int k =0; k < 5; k++){
   //write your code here...
                        final 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++;
             new Handler().postDelayed(new Runnable() {
                    public void run() {

                    }
                }, 2000);
}