android启动界面
时间:2014-05-02 05:06:02
收藏:0
阅读:371
启动界面的效果图:
实现的原理:在一个线程中让几幅图片进行循环显示,实现了动画的效果。
主要的实现类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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); main_imageview = (LoadingView) findViewById(R.id.main_imageview); initLoadingImages(); new Thread() { @Override public 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); } @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; } }
布局文件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
评论(0)