ANDROID笔记:AdapterViewFlipper和ViewFlipper的简单使用

 1 package com.example.android_flipper;

 2 

 3 import android.app.Activity;

 4 import android.os.Bundle;

 5 import android.view.View;

 6 import android.view.View.OnClickListener;

 7 import android.widget.AdapterView;

 8 import android.widget.AdapterView.OnItemClickListener;

 9 import android.widget.AdapterView.OnItemSelectedListener;

10 import android.widget.AdapterViewFlipper;

11 import android.widget.Toast;

12 

13 public class MainActivity extends Activity {

14     int counter = 0;

15     int[] images = { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4 };

16     // 监听AdapterViewFlipper

17     private View.OnClickListener imageViewListener = new OnClickListener() {

18 

19         @Override

20         public void onClick(View v) {

21             Toast.makeText(getApplicationContext(), "image", 200).show();

22 

23         }

24     };

25 

26     @Override

27     protected void onCreate(Bundle savedInstanceState) {

28         super.onCreate(savedInstanceState);

29         setContentView(R.layout.filppertext);

30         AdapterViewFlipper flipper = (AdapterViewFlipper) findViewById(R.id.filpper);

31         flipper.setAdapter(new FlipperAdapter(getApplicationContext(), images,

32                 imageViewListener));

33         // 开始滑动

34         flipper.startFlipping();

35 

36     }

37 }
 1 package com.example.android_flipper;

 2 

 3 import android.content.Context;

 4 import android.view.View;

 5 import android.view.View.OnClickListener;

 6 import android.view.ViewGroup;

 7 import android.view.animation.Animation;

 8 import android.view.animation.AnimationUtils;

 9 import android.widget.AdapterViewFlipper;

10 import android.widget.BaseAdapter;

11 import android.widget.ImageView;

12 import android.widget.ImageView.ScaleType;

13 import android.widget.Toast;

14 

15 public class FlipperAdapter extends BaseAdapter {

16     private int[] images;

17     private Context context;

18     private OnClickListener listener;

19     Animation animation;

20 

21     public FlipperAdapter(Context context, int[] images,

22             OnClickListener listener) {

23         this.images = images;

24         this.context = context;

25         this.listener = listener;

26 

27     }

28 

29     public void setListener(OnClickListener listener) {

30         this.listener = listener;

31     }

32 

33     @Override

34     public int getCount() {

35         // TODO Auto-generated method stub

36         return images.length;

37     }

38 

39     @Override

40     public Object getItem(int position) {

41         // TODO Auto-generated method stub

42         return images[position];

43     }

44 

45     @Override

46     public long getItemId(int position) {

47         // TODO Auto-generated method stub

48         return position;

49     }

50 

51     @Override

52     public View getView(int position, View convertView, ViewGroup parent) {

53         // TODO Auto-generated method stub

54         final ImageView imageView = new ImageView(context);

55         imageView.setScaleType(ScaleType.FIT_XY);

56         imageView

57                 .setLayoutParams(new AdapterViewFlipper.LayoutParams(480, 800));

58         imageView.setImageResource(images[position]);

59         // 设置显示的动画效果

60         animation = AnimationUtils.loadAnimation(context, R.anim.alpha1);

61         imageView.setAnimation(animation);

62 

63         imageView.setOnClickListener(listener);

64         return imageView;

65     }

66 }
1 <?xml version="1.0" encoding="utf-8"?>

2 <AdapterViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"

3     android:id="@+id/filpper"

4     android:layout_width="match_parent"

5     android:layout_height="match_parent"

6     android:flipInterval="4000"

7     android:orientation="vertical" >

8 

9 </AdapterViewFlipper>

注:

AdapterViewFlipper 不能直接添加监听


ViewFlipper的使用
package com.example.android_viewflipper;



import android.app.Activity;

import android.os.Bundle;

import android.view.GestureDetector;

import android.view.GestureDetector.OnGestureListener;

import android.view.MotionEvent;

import android.widget.ImageView;

import android.widget.ViewFlipper;



public class MainActivity extends Activity implements OnGestureListener {

    ViewFlipper flipper;

    GestureDetector mGestureDetector;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        flipper = (ViewFlipper) findViewById(R.id.flipper);

        ImageView imageView = new ImageView(getApplicationContext());

        imageView.setBackgroundResource(R.drawable.ic_launcher);

        flipper.addView(imageView);

        // 手势

        mGestureDetector = new GestureDetector(this);

    }



    @Override

    public boolean onTouchEvent(MotionEvent event) {

        return mGestureDetector.onTouchEvent(event);

    }



    @Override

    public boolean onDown(MotionEvent e) {

        // TODO Auto-generated method stub

        return false;

    }



    @Override

    public void onShowPress(MotionEvent e) {

        // TODO Auto-generated method stub



    }



    @Override

    public boolean onSingleTapUp(MotionEvent e) {

        // TODO Auto-generated method stub

        return false;

    }



    @Override

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

            float distanceY) {

        // TODO Auto-generated method stub

        return false;

    }



    @Override

    public void onLongPress(MotionEvent e) {

        // TODO Auto-generated method stub



    }



    /**

     * 滑动变换图片

     */

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

            float velocityY) {

        if (e2.getX() - e1.getX() > 100) {

            flipper.showPrevious();

        } else if (e2.getX() - e1.getX() < 100) {

            flipper.showNext();

        }

        return false;

    }



}
 
  

 

 
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/flipper"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:flipInterval="1000" >



    <ImageView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:scaleType="centerCrop"

        android:src="@drawable/guide" />



    <ImageView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:scaleType="centerCrop"

        android:src="@drawable/guide0" />



    <ImageView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:scaleType="centerCrop"

        android:src="@drawable/guide1" />



    <ImageView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:scaleType="centerCrop"

        android:src="@drawable/guide2" />



    <RelativeLayout

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:background="@drawable/guide3"

        android:scaleType="centerCrop" >



        <Button

            android:id="@+id/btn"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentBottom="true"

            android:layout_centerHorizontal="true"

            android:layout_marginBottom="46dp"

            android:text="开始" />

    </RelativeLayout>



</ViewFlipper>

ANDROID笔记:AdapterViewFlipper和ViewFlipper的简单使用



你可能感兴趣的:(viewflipper)