其他分享
首页 > 其他分享> > Android 加载动画逐帧动画透明弹窗

Android 加载动画逐帧动画透明弹窗

作者:互联网

1. 效果截图

2. LoadingDialog代码

public class LoadingDialog extends Dialog {

    private AnimationDrawable mAnimation;
    private final String mLoadingTitle;
    private final int layout;

    public LoadingDialog(Context context, String content, int layout) {
        super(context);
        this.mLoadingTitle = content;
        this.layout = layout;
        //点击外围取消
        setCanceledOnTouchOutside(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置窗口布局
        setContentView(R.layout.progress_dialog);

        ImageView mImageView = findViewById(R.id.loadingIv);
        //设置动画资源
        mImageView.setImageResource(layout);
        mAnimation = (AnimationDrawable) mImageView.getDrawable();
        // 启动动态图话
        mImageView.postDelayed(mAnimation::start, 100);
        TextView mLoadingTv = findViewById(R.id.loadingTv);
        //设置加载文字
        mLoadingTv.setText(mLoadingTitle);
        // <color name="transparent">#00FFFFFF</color>
        //设置透明
        getWindow().setBackgroundDrawableResource(R.color.transparent);
    }

}

3. 调用窗口代码


public class MainActivity extends AppCompatActivity {
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        findViewById(R.id.show).setOnClickListener(v -> {
            if (loadingDialog == null) {
                //创建
                loadingDialog = new LoadingDialog(MainActivity.this, "正在加载", R.drawable.anim_loading);
            }
            //调用
            loadingDialog.show();
        });
    }
}

4. LoadingDialog布局——progress_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/loadingIv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/loading" />

    <TextView
        android:id="@+id/loadingTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="20sp" />
</LinearLayout>

5. 调用窗口布局——activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <Button
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@color/black"
        android:text="@string/show"
        android:textColor="#ffffff" />

</LinearLayout>

6. 动画文件——anim_loading.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/loading_01"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_02"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_03"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_04"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_05"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_06"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_07"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_08"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_09"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_10"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_11"
        android:duration="200"/>
    <item
        android:drawable="@drawable/loading_12"
        android:duration="200"/>

</animation-list>

7. 透明色彩

<color name="transparent">#00FFFFFF</color>

参考链接:https://www.runoob.com/w3cnote/android-tutorial-progressbar.html
源码及素材地址:https://github.com/WindSnowLi/android-tools
源码及素材地址:https://gitee.com/windsnowli/android-tools

标签:动画,layout,loadingDialog,private,逐帧,LoadingDialog,Android,onCreate,mImageView
来源: https://www.cnblogs.com/WindSnowLi/p/14778513.html