先来看主页面布局
main_activity.xml里面只有一个button(添加点击事件,弹出加载框)
再看MainActivity
package com.example.loadingdialog; import android.app.Activity; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LoadingDialog loadingDialog = new LoadingDialog(MainActivity.this); loadingDialog.setCancelable(false); loadingDialog.show(); } }); } }
看加载框的布局文件
activity_custom_loding_dialog_layout.xml
<?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:gravity="center" > <LinearLayout android:layout_width="220dp" android:layout_height="220dp" android:layout_gravity="center" android:background="@drawable/dialog_bocop_loaing_bg" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="2" android:gravity="center" > <ImageView android:id="@+id/iv_route" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/dialog_bocop_loading_rotate_anim_img" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_weight="1" android:gravity="center_horizontal" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/tv_point" android:ellipsize="marquee" android:gravity="center" android:singleLine="true" android:text="正在加载" android:textColor="#6F6868" android:textSize="20sp" /> <TextView android:id="@+id/tv_point" android:layout_width="20dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="..." android:textColor="#6F6868" android:textSize="20sp" /> </RelativeLayout> </LinearLayout> </LinearLayout>
LoadingDialog(里面有详细的注释)
package com.example.loadingdialog; import android.app.Dialog; import android.content.Context; import android.os.Handler; import android.view.View; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.ImageView; import android.widget.TextView; public class LoadingDialog extends Dialog { private static final int CHANGE_TITLE_WHAT = 1; private static final int CHNAGE_TITLE_DELAYMILLIS = 300; private static final int MAX_SUFFIX_NUMBER = 3; private static final char SUFFIX = '.'; private ImageView iv_route; private TextView tv; private TextView tv_point; private RotateAnimation mAnim; private boolean cancelable = true; /** * 定义一个handler,加载就发送一个即时消息,让原点+1,继而在每隔300毫秒发送一个延迟消息,来增加+1 */ private Handler handler = new Handler(){ //正在加载的原点数量 private int num = 0; public void handleMessage(android.os.Message msg) { if (msg.what == CHANGE_TITLE_WHAT) { StringBuilder builder = new StringBuilder(); if (num >= MAX_SUFFIX_NUMBER) { num = 0; } num ++; for (int i = 0;i < num;i++) { builder.append(SUFFIX); } tv_point.setText(builder.toString()); if (isShowing()) { handler.sendEmptyMessageDelayed(CHANGE_TITLE_WHAT, CHNAGE_TITLE_DELAYMILLIS); } else { num = 0; } } }; }; public LoadingDialog(Context context) { super(context, R.style.Dialog_bocop); init(); } private void init() { View contentView = View.inflate(getContext(), R.layout.activity_custom_loding_dialog_layout, null); setContentView(contentView); contentView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cancelable) { dismiss(); } } }); iv_route = (ImageView) findViewById(R.id.iv_route); tv = (TextView) findViewById(R.id.tv); tv_point = (TextView) findViewById(R.id.tv_point); /**动画初始化*/ initAnim(); //背景暗色 getWindow().setWindowAnimations(R.anim.alpha_in); } private void initAnim() { mAnim = new RotateAnimation(360, 0,Animation.RESTART, 0.5f, Animation.RESTART,0.5f); mAnim.setDuration(2000); // 设置动画重复次数 mAnim.setRepeatCount(Animation.INFINITE); //动画重复的模式--执行完第一次动画之后,回到动画开始然后执行第二次动画 mAnim.setRepeatMode(Animation.RESTART); mAnim.setStartTime(Animation.START_ON_FIRST_FRAME); } @Override public void show() { iv_route.startAnimation(mAnim); handler.sendEmptyMessage(CHANGE_TITLE_WHAT); super.show(); } @Override public void dismiss() { mAnim.cancel(); super.dismiss(); } @Override public void setCancelable(boolean flag) { cancelable = flag; super.setCancelable(flag); } }
再看super(context, R.style.Dialog_bocop);
背景色,无标题属性
<style name="Dialog_bocop"> <item name="android:windowBackground">@color/bocop_dialog_bg</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> </style>
在color.xml文件中
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="bocop_dialog_bg">#77000000</color> </resources> <!-- window背景色 -->
接下来看这个getWindow().setWindowAnimations(R.anim.alpha_in);
alpha_in.xml(加载框之外是暗色)
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromAlpha="1.0" android:toAlpha="0.0" > </alpha>
时间: 2024-10-13 08:06:47