05
2014
04

Android开发学习:App启动时-带自动加载效果

Android开发学习

App启动有很多效果,初体验自动加载效果

新建xml布局文件:activity_main.xml,代码内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/index"
    android:gravity="bottom|center"
    android:orientation="vertical" >

    <RatingBar
        android:id="@+id/ratingBar1"
        style="@style/roomRatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="55dip"
        android:max="5"
        android:numStars="6"
        android:rating="1" >
    </RatingBar>

</LinearLayout>

使用线性布局LinearLayout,资源文件中的图片index.jpg

效果图片:

实现MainActivity.java代码:

package com.zhengdecai.startloading;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Message;
import android.view.Menu;
import android.widget.RatingBar;

/**
 * App启动时-带自动加载效果
 *
 * @author 郑德才
 *
 */
public class MainActivity extends Activity {

 class TestAsyn extends AsyncTask<String, String, String> {
  public float num = 1;
  protected String doInBackground(String... params) {

   RatingBar rb = (RatingBar) findViewById(R.id.ratingBar1);
   for (int i = 0; i < 100; i++) {
    rb.setRating(num);
    num++;
    if (num == 7) {
     num = 0;
    }
    try {
     Thread.sleep(500);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
   return null;
  }

  @Override
  protected void onPostExecute(String result) {
  }

  @Override
  protected void onPreExecute() {
  }
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  new Thread(new Runnable() {
   Message msg = new Message();

   @Override
   public void run() {
    // TODO Auto-generated method stub
    try {
     Thread.sleep(3500);
     // 结束后可以使用Intent实现跳转到下一个Activity
    } catch (Exception e) {
     // TODO: handle exception
    }
   }
  }).start();

  TestAsyn ts = new TestAsyn();
  ts.execute();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
}

模拟器上的运行效果:

 

 

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。