標籤:android 介面
啟動介面的:
實現的原理:在一個線程中讓幾幅圖片進行迴圈顯示,實現了動畫的效果。
主要的實作類別LoadingView是自訂控制項,繼承了ImageView,實現了Runnable介面 :
package wht.android.loading;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.widget.ImageView;public class LoadingView extends ImageView implements Runnable{ private boolean isStop = false; private int[] imageIds; private int index = 0; private int length = 1; public LoadingView(Context context) { this(context, null); } public LoadingView(Context context, AttributeSet attrs) { super(context, attrs); } public void setImageIds(int[] imageId) { this.imageIds = imageId; if(imageIds != null && imageIds.length > 0) { length = imageIds.length; } } @Override protected void onDetachedFromWindow() { // TODO Auto-generated method stub super.onDetachedFromWindow(); isStop = true; } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); if(imageIds != null && imageIds.length > 0) { this.setImageResource(imageIds[index]); } } @Override public void run() { while(!isStop) { //計算要顯示圖片的位置 index = ++index % length; postInvalidate(); try { Thread.sleep(400); } catch (InterruptedException e) { e.printStackTrace(); } } } //啟動圖片輪換線程 public void startAnim() { new Thread(this).start(); }}
MainActivity:當中的LoadingView 就是我們自訂的控制項,在布局中使用時跟其他控制項一樣。
package wht.android.loading;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {private LoadingView main_imageview;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);main_imageview = (LoadingView) findViewById(R.id.main_imageview);initLoadingImages();new Thread() {@Overridepublic void run() {main_imageview.startAnim();}}.start();}private void initLoadingImages() {int[] imageIds = new int[6];imageIds[0] = R.drawable.loader_frame_1;imageIds[1] = R.drawable.loader_frame_2;imageIds[2] = R.drawable.loader_frame_3;imageIds[3] = R.drawable.loader_frame_4;imageIds[4] = R.drawable.loader_frame_5;imageIds[5] = R.drawable.loader_frame_6;main_imageview.setImageIds(imageIds);}@Overridepublic 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;}}
布局檔案main.xml 當中使用的wht.android.loading.LoadingView就是前面定義的控制項,完整的包名加上類名。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#e1e1e1" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="65dip" android:orientation="vertical" > <wht.android.loading.LoadingView android:id="@+id/main_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@drawable/loader_frame_1" > </wht.android.loading.LoadingView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dip" android:text="啟動中..." android:textColor="#666666" android:textSize="20sp" /> </LinearLayout></LinearLayout>
:http://download.csdn.net/detail/l1976135784/7281483