通过添加不同帧数的图片,来实现自己想要的动画效果正在加载。。。
第一步: Resources/drawable 目录下添加每一帧的图片,我设定了18帧
第二步:实现动画效果,drawable下新建页面 loading_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" >
<item android:drawable="@drawable/load_img1" android:duration="150"/>
<item android:drawable="@drawable/load_img2" android:duration="150"/>
<item android:drawable="@drawable/load_img3" android:duration="150"/>
<item android:drawable="@drawable/load_img4" android:duration="150"/>
<item android:drawable="@drawable/load_img5" android:duration="150"/>
<item android:drawable="@drawable/load_img6" android:duration="150"/>
<item android:drawable="@drawable/load_img7" android:duration="150"/>
<item android:drawable="@drawable/load_img8" android:duration="150"/>
<item android:drawable="@drawable/load_img9" android:duration="150"/>
<item android:drawable="@drawable/load_img10" android:duration="150"/>
<item android:drawable="@drawable/load_img11" android:duration="150"/>
<item android:drawable="@drawable/load_img12" android:duration="150"/>
<item android:drawable="@drawable/load_img13" android:duration="150"/>
<item android:drawable="@drawable/load_img14" android:duration="150"/>
<item android:drawable="@drawable/load_img15" android:duration="150"/>
<item android:drawable="@drawable/load_img16" android:duration="150"/>
<item android:drawable="@drawable/load_img17" android:duration="150"/>
<item android:drawable="@drawable/load_img18" android:duration="150"/>
</animation-list>
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyDialogStyle">
<item name="android:windowBackground">@drawable/filled_box</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowContentOverlay">@null</item>
<!--<item name="android:windowAnimationStyle">@style/dialog_animation</item>-->
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:backgroundDimAmount">0.6</item> <!--灰度-->
</style>
</resources>
第四步:layout下新建加载界面loading.axml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:gravity="center">
<RelativeLayout
android:layout_width="600dp"
android:layout_height="500dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:background="@drawable/loading_anim" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载..."
android:textColor="#fff"
android:textSize="28sp"
android:layout_marginLeft="10dp"
android:id="@+id/txt_loading" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
第五步:点击事件下,弹出加载界面,完成
public void OnClick(View v)
{
int id = v.Id;
if (id == Resource.Id.btn_buy)
{
Dialog dialog = new Dialog(this, Resource.Style.MyDialogStyle);
dialog.SetContentView(Resource.Layout.loading);
dialog.Show();
mImageView = dialog.FindViewById<ImageView>(Resource.Id.imageView1);
TextView tv=dialog.FindViewById<TextView>(Resource.Id.txt_loading);
tv.Text = "正在加载,请稍后...";
mAnimation = (AnimationDrawable)mImageView.Background;
mImageView.Post(new Runnable(() =>
{
mAnimation.Start();
}));
new Handler().PostDelayed(new Runnable(() =>
{
//等待10000毫秒后销毁此页面,并提示登陆成功
dialog.Dismiss();//取消加载
Toast.MakeText(this, "加载完成", ToastLength.Short).Show();
}), 10000);
}
}