2012-12-05 16:08:29.0|分类: android|浏览量: 1704
本人博客开始迁移,博客整个架构自己搭建及编码 http://www.cookqq.com/listBlog.action 例如登陆页面中需要显示等待动画, Animation Drawable可以让你把一系列的图片像幻灯片一样一张一张地显示在屏幕上。虽然Animation Drawable的效果看起来像动画一样,但在实际的代码处理中应该将其视为Drawable,而事实上它继承于Drawable,其XML配置文件也是放置在res/drawable目录下。 代码:login_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="100"> <layer-list> <item android:drawable="@drawable/login_loading_00" /> <item android:drawable="@drawable/login_loading_10" /> </layer-list> </item> <item android:duration="100"> <layer-list> <item android:drawable="@drawable/login_loading_01" /> <item android:drawable="@drawable/login_loading_11" /> </layer-list> </item> <item android:duration="100"> <layer-list> <item android:drawable="@drawable/login_loading_02" /> <item android:drawable="@drawable/login_loading_12" /> </layer-list> </item> <item android:duration="100"> <layer-list> <item android:drawable="@drawable/login_loading_03" /> <item android:drawable="@drawable/login_loading_13" /> </layer-list> </item> <item android:duration="100"> <layer-list> <item android:drawable="@drawable/login_loading_04" /> <item android:drawable="@drawable/login_loading_14" /> </layer-list> </item> <item android:duration="100"> <layer-list> <item android:drawable="@drawable/login_loading_05" /> <item android:drawable="@drawable/login_loading_15" /> </layer-list> </item> <item android:duration="100"> <layer-list> <item android:drawable="@drawable/login_loading_06" /> <item android:drawable="@drawable/login_loading_16" /> </layer-list> </item> <item android:duration="100"> <layer-list> <item android:drawable="@drawable/login_loading_07" /> <item android:drawable="@drawable/login_loading_17" /> </layer-list> </item> <item android:duration="100"> <layer-list> <item android:drawable="@drawable/login_loading_08" /> <item android:drawable="@drawable/login_loading_18" /> </layer-list> </item> <item android:duration="100"> <layer-list> <item android:drawable="@drawable/login_loading_09" /> <item android:drawable="@drawable/login_loading_19" /> </layer-list> </item> </animation-list> 注释:(1)android:duration="100" 每个帧都会延时100ms (2)如果android:oneshot属性设置为true,那么该Animation Drawable只会运行一次,并最后会定格在最后一帧上,要是android:oneshot属性设为false,那么便会无限循环地显示上面的这几个帧 MainActivity.java package com.tbx.android.waiting; import com.tbx.android.R; public class MainActivity extends Activity { private Button button ; private View loginLoading; private AnimationDrawable loadingAnimation; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button)findViewById(R.id.button1); loginLoading = (View)findViewById(R.id.login_loading); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(loginLoading.getVisibility() == View.GONE){ loginLoading.setVisibility(View.VISIBLE); loadingAnimation = (AnimationDrawable)loginLoading.getBackground(); loadingAnimation.start(); }else{ if(loadingAnimation!=null){ loadingAnimation.stop(); loginLoading.setVisibility(View.GONE); } } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } } 注释: 开始播放动画 loadingAnimation.start() mian.xml
<LinearLayout 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" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" tools:context=".MainActivity" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginRight="20dp" android:layout_marginTop="48dp" android:layout_toLeftOf="@+id/textView1" android:text="Button" /> <View android:id="@+id/login_loading" android:layout_width="135.0dip" android:layout_height="135.0dip" android:layout_gravity="center" android:visibility="gone" android:background="@anim/login_loading"/> </LinearLayout> ok,运行后可以看到效果了。
为什么顺序是反的呢 引用来自“qiaoliyun”的评论为什么顺序是反的呢 啊发射点发射点发生发射点 啊手动阀手动阀手动阀 看看效果 看下效果 查看效果 |