android通过线程实现逐行显示信息

    技术2025-11-27  9

    今天调查怎么实现在android 手机上逐行显示内容的效果。这是其中的一种方法,通过AlphaAnimation来和设置线程的延时做的,效果可以实现,但开销比较大。开销小的方法将在下一篇文章中介绍。

     

    package com.display; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.widget.Button; import android.widget.TextView; public class Display extends Activity {     private TextView[] tvs = new TextView[7];     private Handler handler = new Handler();     private Button btnReload;     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         tvs[0] = (TextView) findViewById(R.id.TextView01);         tvs[1] = (TextView) findViewById(R.id.TextView02);         tvs[2] = (TextView) findViewById(R.id.TextView03);         tvs[3] = (TextView) findViewById(R.id.TextView04);         tvs[4] = (TextView) findViewById(R.id.TextView05);         tvs[5] = (TextView) findViewById(R.id.TextView06);         tvs[6] = (TextView) findViewById(R.id.TextView07);         loadContent();         btnReload = (Button) findViewById(R.id.Button01);         btnReload.setOnClickListener(new OnClickListener() {             @Override             public void onClick(View v) {                 loadContent();             }         });     }     private void loadContent() {         setInvisible();         for (int j = 0; j < tvs.length; j++) {             final TextView tv = tvs[j];             Runnable r = new Runnable() {                 @Override                 public void run() {                     setAnimation(tv);                 }             };

     

    // 设置动画延时

                handler.postDelayed(r, j * 1000);         }     }

     

    // 设置动画

        private void setAnimation(final TextView tv) {         AlphaAnimation aa = new AlphaAnimation(0, 1.0f);         aa.setDuration(1000);         aa.setAnimationListener(new AnimationListener() {             @Override             public void onAnimationEnd(Animation animation) {                 tv.setVisibility(View.VISIBLE);             }             @Override             public void onAnimationRepeat(Animation animation) {             }             @Override             public void onAnimationStart(Animation animation) {             }         });         tv.startAnimation(aa);     }     private void setInvisible() {         for (int i = 0; i < tvs.length; i++) {             tvs[i].setVisibility(View.INVISIBLE);         }     } }

    最新回复(0)