AdapterViewFlipper的功能和用法
AdapterViewFlipper繼承了AdapterAnimator,它會顯示多個組件,但每次只能顯示一個組件,通過showprevious和shownext方法實現(xiàn)顯示上一個和下一個組件
并且可以通過調(diào)用startFlipping實現(xiàn)自動播放
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? >
? ? <!--? android:flipInterval="5000"設(shè)置自動播放時間間隔-->
? ? <AdapterViewFlipper
? ? ? ? android:id="@+id/mia"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:flipInterval="5000"
? ? ? ? android:layout_alignParentTop="true">
? ? </AdapterViewFlipper>
? ? <Button
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:onClick="prev"
? ? ? ? android:layout_alignParentLeft="true"
? ? ? ? android:text="上一個"/>
? ? <Button
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:layout_centerHorizontal="true"
? ? ? ? android:onClick="next"
? ? ? ? android:text="下一個"/>
? ? <Button
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_alignParentRight="true"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:onClick="auto"
? ? ? ? android:text="自動播放"/>
</RelativeLayout>
//love mia
public class MainActivity extends Activity {
? ? ? ? int[] love_mia = new int[] { R.drawable.mia, R.drawable.love,
? ? ? ? ? ? ? ? ? ? ? ? R.drawable.mia3, R.drawable.mia4, R.drawable.mia5, R.drawable.mia6,
? ? ? ? ? ? ? ? ? ? ? ? R.drawable.mia7, R.drawable.mia8, R.drawable.mia9,
? ? ? ? ? ? ? ? ? ? ? ? R.drawable.mia10, R.drawable.mia11 };
? ? ? ? private AdapterViewFlipper avf;
? ? ? ? @Override
? ? ? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? ? ? ? ? setContentView(R.layout.image);
? ? ? ? ? ? ? ? avf = (AdapterViewFlipper) findViewById(R.id.mia);
? ? ? ? ? ? ? ? // 創(chuàng)建一個baseAdapter對象,該對象負責(zé)提供Gallery所顯示的列表項
? ? ? ? ? ? ? ? BaseAdapter ba = new BaseAdapter() {
? ? ? ? ? ? ? ? ? ? ? ? // 該方法返回的view代表了每個列表項
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public View getView(int arg0, View arg1, ViewGroup arg2) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 創(chuàng)建一個imageview
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ImageView iv = new ImageView(MainActivity.this);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? iv.setImageResource(love_mia[arg0]);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 設(shè)置imageview的縮放類型
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? iv.setScaleType(ScaleType.FIT_XY);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 為imageview設(shè)置布局參數(shù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? iv.setLayoutParams(new ViewGroup.LayoutParams(
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return iv;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public long getItemId(int arg0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return arg0;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public Object getItem(int arg0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return arg0;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public int getCount() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return love_mia.length;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? avf.setAdapter(ba);
? ? ? ? }
? ? ? ? public void prev(View source){
? ? ? ? ? ? ? ? //顯示上一個組件
? ? ? ? ? ? ? ? avf.showPrevious();
? ? ? ? ? ? ? ? //停止自動播放
? ? ? ? ? ? ? ? avf.stopFlipping();
? ? ? ? }
? ? ? ? public void next(View source){
? ? ? ? ? ? ? ? //顯示下一個組件
? ? ? ? ? ? ? ? avf.showNext();
? ? ? ? ? ? ? ? //停止自動播放
? ? ? ? ? ? ? ? avf.stopFlipping();
? ? ? ? }
? ? ? ? public void auto(View source){
? ? ? ? ? ? ? ? //開始自動播放
? ? ? ? ? ? ? ? avf.startFlipping();
? ? ? ? }
}
