Android 实现加载动画
程序员文章站
2024-03-24 15:34:52
...
首先在drawable下建一个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:duration="130" android:drawable="@drawable/image_loading_01"></item>
<item android:duration="130" android:drawable="@drawable/image_loading_02"></item>
<item android:duration="130" android:drawable="@drawable/image_loading_03"></item>
<item android:duration="130" android:drawable="@drawable/image_loading_04"></item>
<item android:duration="130" android:drawable="@drawable/image_loading_05"></item>
<item android:duration="130" android:drawable="@drawable/image_loading_06"></item>
</animation-list>
在layout文件下建一个layout_dialog.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/aa_dialog_bg"
android:gravity="center"
android:padding="10dp"
android:orientation="vertical">
<ProgressBar
android:id="@+id/loading_progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5.0dip"
android:indeterminateDrawable="@drawable/animation_loading" />
<TextView
android:id="@+id/tv_reminder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="加载中..."
android:textColor="@color/colorTextGray" />
</LinearLayout>
实现dialog的调用:
package youhua.com.mytestapp;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* description:弹窗浮动加载进度条
*/
public class LoadingDialog {
/** 加载数据对话框 */
private static Dialog mLoadingDialog;
/**
* 显示加载对话框
* @param context 上下文
* @param msg 对话框显示内容
* @param cancelable 对话框是否可以取消
*/
public static Dialog showDialogForLoading(Activity context, String msg, boolean cancelable) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_dialog, null);
TextView loadingText = (TextView)view.findViewById(R.id.tv_reminder);
loadingText.setText(msg);
mLoadingDialog = new Dialog(context, R.style.CustomProgressDialog);
mLoadingDialog.setCancelable(cancelable);
mLoadingDialog.setCanceledOnTouchOutside(false);
mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
mLoadingDialog.show();
return mLoadingDialog;
}
public static Dialog showDialogForLoading(Activity context) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_dialog, null);
TextView loadingText = (TextView)view.findViewById(R.id.tv_reminder);
loadingText.setText("加载中...");
mLoadingDialog = new Dialog(context, R.style.CustomProgressDialog);
mLoadingDialog.setCancelable(true);
mLoadingDialog.setCanceledOnTouchOutside(false);
mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
mLoadingDialog.show();
return mLoadingDialog;
}
/**
* 关闭加载对话框
*/
public static void cancelDialogForLoading() {
if(mLoadingDialog != null) {
mLoadingDialog.cancel();
}
}
}
最后在Activity中调用:
package youhua.com.mytestapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
/**
* developer: $(wei)
* date:created by 2018/9/4 17:32
* email:[email protected]
*/
public class MainActivity extends AppCompatActivity {
private TextView btn_loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_loading = findViewById(R.id.btn_loading);
btn_loading.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoadingDialog.showDialogForLoading(MainActivity.this);
}
});
}
}
备注说明:图片换成美工切的幁图就行了。
上一篇: 《CSS世界》第三章学习心得
推荐阅读
-
Android 实现加载动画
-
基于Android搭建tensorflow lite,实现官网的Demo以及运行自定义tensorflow模型(二)
-
关于JQ在ajax请求数据前添加加载动画
-
Android动画总结系列(1)——帧动画
-
Android-AnimationDrawable自定义加载动画dialog
-
android回收AnimationDrawable动画的每一帧的图片资源,而释放内存
-
【Android】Property Animation(属性动画)ObjectAnimator 动画类
-
Android AnimationDrawable动画实现icon闪烁
-
Android UI绘制流程(一)----布局的加载
-
android在service中使用AsyncHttpClient加载网络资源