Xamarin.Android 实现正在加载动画效果

本文详细介绍如何在Android应用中实现自定义加载动画。包括资源准备、XML动画配置、对话框样式设置及界面布局创建等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过添加不同帧数的图片,来实现自己想要的动画效果正在加载。。。


第一步: 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> 


第三步:实现加载页半透明样式 values下新建界面style.xml

<?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);             
            }
        }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值