2012年3月27日 星期二

Image Transition

從一張圖,經過5秒轉換成另一張圖

res/layout/main.xml:


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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center"
        android:src="@drawable/yih950909_01_300" />

</LinearLayout>

**************
res/drawable/kids_photos.xml:


<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/yih950909_01_300"/>
<item android:drawable="@drawable/usa_03y_01_300"/>
</transition>

****************************

main.java:


public class Cq_ImgTransitionActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
//設定轉換圖像來自 res/drawable/kids_photos.xml
        Resources res = getResources();
        TransitionDrawable tranisiton = (TransitionDrawable)res.getDrawable(R.drawable.kids_photos);
        ImageView mImageView = (ImageView)findViewById(R.id.imageView1);
        mImageView.setImageDrawable(tranisiton);
     
      //開始轉換到第2張圖像
        tranisiton.startTransition(5000);
    }
}


沒有留言:

張貼留言