Frame布局FrameLayout & Frame动画AnimationDrawable 应用实例
1)FrameLayout常常与 merge 相关,关于他们各自的介绍,请参阅相关的文档。在这里,
用来合并两个透明的png图片,就像photoshop里图层合并一样。
2)Frame动画animation-list,常常用于制作短片动画或用于进程进度的一个指示标识。
先附图如下:
下面是部分代码:
res/anim/scrolling.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false"><item android:drawable="@drawable/ball1" android:duration="80" /> <item android:drawable="@drawable/ball2" android:duration="80" /> <item android:drawable="@drawable/ball3" android:duration="80" /> <item android:drawable="@drawable/ball4" android:duration="80" /> <item android:drawable="@drawable/ball5" android:duration="80" /> <item android:drawable="@drawable/ball6" android:duration="80" /> </animation-list>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_centerHorizontal="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/frame01"><ImageView android:src="@drawable/line"android:layout_width="300sp" android:layout_height="200sp"></ImageView><ImageView android:src="@drawable/bar"android:layout_width="300sp" android:layout_height="200sp"></ImageView></FrameLayout> <ImageView android:id="@+id/scrolling_anim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_centerHorizontal="true" android:layout_below="@id/frame01" android:layout_marginTop="15sp"/> </RelativeLayout>
package com.sunflower;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.widget.ImageView;public class FrameActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); ImageView img = (ImageView)findViewById(R.id.scrolling_anim); img.setBackgroundResource(R.anim.scrolling); //设置动画的载体(画布) StopAnim mar2 = new StopAnim(); Timer t2 = new Timer(false); t2.schedule(mar2, 10000); //10秒后停止动画 } class StartUpAnim extends TimerTask { public void run() { ImageView img = (ImageView)findViewById(R.id.scrolling_anim); // Get the background, which has been compiled to an AnimationDrawable object. AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); // Start the animation (looped playback by default). frameAnimation.start(); } } class StopAnim extends TimerTask { public void run() { ImageView img = (ImageView)findViewById(R.id.scrolling_anim); // Get the background, which has been compiled to an AnimationDrawable object. AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); // stop the animation (looped playback by default). frameAnimation.stop(); } }@Overrideprotected void onResume() {super.onResume(); StartUpAnim mar = new StartUpAnim(); //放在这里执行是让frameAnimation有足够的时间取得画布(Images's background) Timer t = new Timer(false); t.schedule(mar, 1000); //延迟1秒后才开始动画}}