網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android實現(xiàn)多張圖片合成加載動畫的具體代碼,供大家參考,具體內(nèi)容如下
1、自定義ProgressDialog
public class MyProgressDialog extends ProgressDialog {
? ? private int procressLoadRes;
? ? private ImageView mImageView;
? ? private AnimationDrawable animationDrawable;
? ? public MyProgressDialog(Context context,int procressLoadRes) {
? ? ? ? super(context);
? ? ? ? this.procressLoadRes = procressLoadRes;
? ? }
? ? public MyProgressDialog(Context context, int theme, int procressLoadRes) {
? ? ? ? super(context, theme);
? ? ? ? this.procressLoadRes = procressLoadRes;
? ? }
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.layout_progress);
? ? ? ? mImageView = (ImageView) findViewById(R.id.image);
? ? ? ? //設置動畫背景
? ? ? ? mImageView.setBackgroundResource(procressLoadRes);
? ? ? ? //獲取動畫對象,必須在上一步之后
? ? ? ? animationDrawable = (AnimationDrawable) mImageView.getBackground();
? ? ? ? mImageView.post(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? //啟動動畫
? ? ? ? ? ? ? ? animationDrawable.start();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
2、MyProgressDialog對應的布局layout_progress.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:orientation="vertical" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:gravity="center" ? ? android:background="@null"> ? ? <ImageView ? ? ? ? android:id="@+id/image" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" /> </LinearLayout>
3、使用自定義的MyProgressDialog
public class MainActivity extends AppCompatActivity {
? ? private Button button;
? ? private MyProgressDialog progressDialog;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? button = (Button) findViewById(R.id.button);
? ? ? ? //傳入style和anim文件
? ? ? ? progressDialog = new MyProgressDialog(this,R.style.dialog,R.anim.loading);
? ? ? ? button.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View view) {
? ? ? ? ? ? ? ? progressDialog.show();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
4、使用時需要一個style文件和一個anim文件
style.xml
<style name="dialog" parent="@android:style/Theme.Dialog"> ? ? ? ? <!--邊框--> ? ? ? ? <item name="android:windowFrame">@null</item> ? ? ? ? <!--是否浮現(xiàn)在activity之上--> ? ? ? ? <item name="android:windowIsFloating">true</item> ? ? ? ? <!--半透明--> ? ? ? ? <item name="android:windowIsTranslucent">false</item> ? ? ? ? <!--無標題--> ? ? ? ? <item name="android:windowNoTitle">true</item> ? ? ? ? <!--背景透明去掉背景色邊框也就去掉了 --> ? ? ? ? <item name="android:windowBackground">@color/transparent</item>? ? ? ? ? <!--模糊--> ? ? ? ? <item name="android:backgroundDimEnabled">false</item> ? ? </style>
anim文件夾下的loading.xml文件
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <item android:drawable="@mipmap/loading1" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading2" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading3" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading4" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading5" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading6" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading7" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading8" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading9" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading10" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading11" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading12" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading13" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading14" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading15" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading16" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading17" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading18" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading19" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading20" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading21" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading22" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading23" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading24" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading25" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading26" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading27" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading30" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading31" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading32" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading33" android:duration="100"/> ? ? <item android:drawable="@mipmap/loading34" android:duration="100"/> </animation-list>
在color.xml文件中添加
<color name="transparent">#00ffffff</color>
原文鏈接:https://blog.csdn.net/hello_1s/article/details/51889596
相關推薦
- 2022-08-22 Git配置別名簡化操作命令方式詳解_相關技巧
- 2022-03-23 C語言可變長的參數(shù)列表詳解_C 語言
- 2022-05-01 jquery實現(xiàn)移動端按鈕組左右滑動_jquery
- 2022-04-12 Python實現(xiàn)數(shù)據(jù)地址實體抽取_python
- 2022-04-16 Android中RecyclerView實現(xiàn)簡單購物車功能_Android
- 2022-11-26 docker?prune命令定時清理不常用數(shù)據(jù)的操作方法_docker
- 2022-10-08 React中useState的使用方法及注意事項_React
- 2024-03-10 Spring 非自定義Bean注解
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支