import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.ScaleAnimation; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTv; private ArrayList<String> nameList; private Random r; private String rname = null; int i = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTv = (TextView) this.findViewById(R.id.tv_showname); nameList = new ArrayList<String>(); readData(); } private void readData() { // 直接从文件里拿到全部人名字,放到集合里 // 随机为集合长度,那个随机所以人名字显示到mTv try { InputStream is = this.getAssets().open("name.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8")); String line = null; while ((line = br.readLine()) != null) { nameList.add(line);// 遍历每一行的名字放入List集合 } } catch (Exception e) { e.printStackTrace(); } } public void rName(View view) { final int size = nameList.size(); r = new Random(); new Thread() { public void run() { // 得到一个随机索引 for (i =0; i < 4; i++) { try { Thread.sleep(300); int index = r.nextInt(size); rname = nameList.get(index); runOnUiThread(new Runnable() { @Override public void run() { // 显示 mTv.setText(rname); if (i == 4) { showAnim(); } } }); } catch (InterruptedException e) { e.printStackTrace(); } } }; }.start(); } /** * 显示缩放动画 */ private void showAnim(){ AnimationSet set = new AnimationSet(false); ScaleAnimation anim = new ScaleAnimation(0, 4, 0,4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); anim.setDuration(1000); ScaleAnimation anim1 = new ScaleAnimation(4, 0, 4, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); anim1.setDuration(1000); set.addAnimation(anim); set.addAnimation(anim1); mTv.startAnimation(set); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="rName" android:text="抽奖活动" /> <TextView android:id="@+id/tv_showname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="恭喜你!" android:textColor="#FF0000" android:textSize="33dp" /> </RelativeLayout>
在assets文件下创建name.text
效果: