1. 程式人生 > >簡單實現一個圖片輪播

簡單實現一個圖片輪播

(注意要將圖片替換為你自己的圖片)
public class MainActivity extends Activity {

private ViewPager mViewPaper;
private List<ImageView> images;
private List<View> dots;
private int currentItem;
//記錄上一次點的位置
private int oldPosition = 0;
//存放圖片的id
private int[] imageIds = new int[]{
        R.drawable.a,
        R.drawable.b,
        R.drawable.c,
        R.drawable.d,
        R.drawable.e,
        R.drawable.f,
        R.drawable.g,
};
//存放圖片的標題
private String[]  titles = new String[]{
        "上班的時候",
        "澳門旅遊",
        "閨蜜/同事",
        "家?",
        "美人",
        "嫵媚",
        "晒弟狂魔",
};
private TextView title;
private ViewPagerAdapter adapter;
private ScheduledExecutorService scheduledExecutorService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mViewPaper = (ViewPager) findViewById(R.id.vp);

    //顯示的圖片
    images = new ArrayList<ImageView>();
    for(int i = 0; i < imageIds.length; i++){
        ImageView imageView = new ImageView(this);
        imageView.setBackgroundResource(imageIds[i]);
        images.add(imageView);
    }
    //顯示的小點
    dots = new ArrayList<View>();
    dots.add(findViewById(R.id.dot_0));
    dots.add(findViewById(R.id.dot_1));
    dots.add(findViewById(R.id.dot_2));
    dots.add(findViewById(R.id.dot_3));

    dots.add(findViewById(R.id.dot_4));
    dots.add(findViewById(R.id.dot_5));
    dots.add(findViewById(R.id.dot_6));
    dots.add(findViewById(R.id.dot_7));
    title = (TextView) findViewById(R.id.title);
    title.setText(titles[0]);

    adapter = new ViewPagerAdapter();
    mViewPaper.setAdapter(adapter);

    mViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {


        @Override
        public void onPageSelected(int position) {
            title.setText(titles[position]);
            dots.get(position).setBackgroundResource(R.drawable.dot_focused);
            dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);

            oldPosition = position;
            currentItem = position;
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    });
}

/**
 * 自定義Adapter
 * @author liuyazhuang
 *
 */
private class ViewPagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {
        // TODO Auto-generated method stub

// super.destroyItem(container, position, object);
// view.removeView(view.getChildAt(position));
// view.removeViewAt(position);
view.removeView(images.get(position));
}

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        // TODO Auto-generated method stub
        view.addView(images.get(position));
        return images.get(position);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/**
 * 利用執行緒池定時執行動畫輪播
 */
@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    scheduledExecutorService.scheduleWithFixedDelay(
            new ViewPageTask(),
            2,
            2,
            TimeUnit.SECONDS);
}


private class ViewPageTask implements Runnable{

    @Override
    public void run() {
        currentItem = (currentItem + 1) % imageIds.length;
        mHandler.sendEmptyMessage(0);
    }
}

/**
 * 接收子執行緒傳遞過來的資料
 */
private Handler mHandler = new Handler(){
    public void handleMessage(android.os.Message msg) {
        mViewPaper.setCurrentItem(currentItem);
    };
};
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
}

}

佈局(可以自定義)

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="35dip"
        android:layout_gravity="bottom"
        android:background="#33000000"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="圖片標題"
            android:textColor="@android:color/white" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dip"
            android:orientation="horizontal" >

            <View
                android:id="@+id/dot_0"
                android:layout_width="5dip"
                android:layout_height="5dip"
                android:layout_marginLeft="2dip"
                android:layout_marginRight="2dip"
                android:background="@drawable/dot_focused"/>

            <View
                android:id="@+id/dot_1"
                android:layout_width="5dip"
                android:layout_height="5dip"
                android:layout_marginLeft="2dip"
                android:layout_marginRight="2dip"
                android:background="@drawable/dot_normal"/>
            <View
                android:id="@+id/dot_2"
                android:layout_width="5dip"
                android:layout_height="5dip"
                android:layout_marginLeft="2dip"
                android:layout_marginRight="2dip"
                android:background="@drawable/dot_normal"/>
            <View
                android:id="@+id/dot_3"
                android:layout_width="5dip"
                android:layout_height="5dip"
                android:layout_marginLeft="2dip"
                android:layout_marginRight="2dip"
                android:background="@drawable/dot_normal"/>
            <View
                android:id="@+id/dot_4"
                android:layout_width="5dip"
                android:layout_height="5dip"
                android:layout_marginLeft="2dip"
                android:layout_marginRight="2dip"
                android:background="@drawable/dot_normal"/>
            <View
                android:id="@+id/dot_5"
                android:layout_width="5dip"
                android:layout_height="5dip"
                android:layout_marginLeft="2dip"
                android:layout_marginRight="2dip"
                android:background="@drawable/dot_normal"/>
            <View
                android:id="@+id/dot_6"
                android:layout_width="5dip"
                android:layout_height="5dip"
                android:layout_marginLeft="2dip"
                android:layout_marginRight="2dip"
                android:background="@drawable/dot_normal"/>
            <View
                android:id="@+id/dot_7"
                android:layout_width="5dip"
                android:layout_height="5dip"
                android:layout_marginLeft="2dip"
                android:layout_marginRight="2dip"
                android:background="@drawable/dot_normal"/>

        </LinearLayout>
    </LinearLayout>
</FrameLayout>