天天看點

android圖檔輪播

第一步,先寫布局檔案

[html] view

plaincopyprint?

<com.main.util.slideshowview   

                android:id="@+id/slideshowview"  

                android:layout_width="fill_parent"  

                android:layout_height="300dp"  

                android:layout_centerhorizontal="true"  

                />  

第二步,java代碼

[java] view

package com.main.util;  

import java.util.arraylist;  

import java.util.list;  

import java.util.concurrent.executors;  

import java.util.concurrent.scheduledexecutorservice;  

import java.util.concurrent.timeunit;  

import com.main.r;  

import android.content.context;  

import android.graphics.drawable.drawable;  

import android.os.handler;  

import android.os.message;  

import android.os.parcelable;  

import android.support.v4.view.pageradapter;  

import android.support.v4.view.viewpager;  

import android.support.v4.view.viewpager.onpagechangelistener;  

import android.util.attributeset;  

import android.view.layoutinflater;  

import android.view.view;  

import android.widget.framelayout;  

import android.widget.imageview;  

import android.widget.imageview.scaletype;  

public class slideshowview extends framelayout {  

    //輪播圖圖檔數量  

    private final static int image_count = 5;  

    //自動輪播的時間間隔  

    private final static int time_interval = 5;  

    //自動輪播啟用開關  

    private final static boolean isautoplay = true;   

    //自定義輪播圖的資源id  

    private int[] imagesresids;  

    //放輪播圖檔的imageview 的list  

    private list<imageview> imageviewslist;  

    //放圓點的view的list  

    private list<view> dotviewslist;  

    private viewpager viewpager;  

    //目前輪播頁  

    private int currentitem  = 0;  

    //定時任務  

    private scheduledexecutorservice scheduledexecutorservice;  

    //handler  

    private handler handler = new handler(){  

        @override  

        public void handlemessage(message msg) {  

            // todo auto-generated method stub  

            super.handlemessage(msg);  

            viewpager.setcurrentitem(currentitem);  

        }  

    };  

    public slideshowview(context context) {  

        this(context,null);  

        // todo auto-generated constructor stub  

    }  

    public slideshowview(context context, attributeset attrs) {  

        this(context, attrs, 0);  

    public slideshowview(context context, attributeset attrs, int defstyle) {  

        super(context, attrs, defstyle);  

        initdata();  

        initui(context);  

        if(isautoplay){  

            startplay();  

    /** 

     * 開始輪播圖切換 

     */  

    private void startplay(){  

        scheduledexecutorservice = executors.newsinglethreadscheduledexecutor();  

        scheduledexecutorservice.scheduleatfixedrate(new slideshowtask(), 1, 4, timeunit.seconds);  

     * 停止輪播圖切換 

    private void stopplay(){  

        scheduledexecutorservice.shutdown();  

     * 初始化相關data 

    private void initdata(){  

        imagesresids = new int[]{//圖檔資料  

                r.drawable.p1,    

                r.drawable.p2,  

        };  

        imageviewslist = new arraylist<imageview>();  

        dotviewslist = new arraylist<view>();  

     * 初始化views等ui 

    private void initui(context context){  

        layoutinflater.from(context).inflate(r.layout.imageviewpager, this, true);  

        for(int imageid : imagesresids){  

            imageview view =  new imageview(context);  

            view.setimageresource(imageid);  

            view.setscaletype(scaletype.fit_xy);  

            imageviewslist.add(view);  

        dotviewslist.add(findviewbyid(r.id.v_dot1));  

        dotviewslist.add(findviewbyid(r.id.v_dot2));  

        viewpager = (viewpager) findviewbyid(r.id.viewpager);  

        viewpager.setfocusable(true);  

        viewpager.setadapter(new mypageradapter());  

        viewpager.setonpagechangelistener(new mypagechangelistener());  

     * 填充viewpager的頁面擴充卡 

     * @author caizhiming 

    private class mypageradapter  extends pageradapter{  

        public void destroyitem(view container, int position, object object) {  

            //((viewpag.er)container).removeview((view)object);  

            ((viewpager)container).removeview(imageviewslist.get(position));  

        public object instantiateitem(view container, int position) {  

            ((viewpager)container).addview(imageviewslist.get(position));  

            return imageviewslist.get(position);  

        public int getcount() {  

            return imageviewslist.size();  

        public boolean isviewfromobject(view arg0, object arg1) {  

            return arg0 == arg1;  

        public void restorestate(parcelable arg0, classloader arg1) {  

        public parcelable savestate() {  

            return null;  

        public void startupdate(view arg0) {  

        public void finishupdate(view arg0) {  

     * viewpager的監聽器 

     * 當viewpager中頁面的狀态發生改變時調用 

    private class mypagechangelistener implements onpagechangelistener{  

        boolean isautoplay = false;  

        public void onpagescrollstatechanged(int arg0) {  

            switch (arg0) {  

            case 1:// 手勢滑動,空閑中  

                isautoplay = false;  

                break;  

            case 2:// 界面切換中  

                isautoplay = true;  

            case 0:// 滑動結束,即切換完畢或者加載完畢  

                // 目前為最後一張,此時從右向左滑,則切換到第一張  

                if (viewpager.getcurrentitem() == viewpager.getadapter().getcount() - 1 && !isautoplay) {  

                    viewpager.setcurrentitem(0);  

                }  

                // 目前為第一張,此時從左向右滑,則切換到最後一張  

                else if (viewpager.getcurrentitem() == 0 && !isautoplay) {  

                    viewpager.setcurrentitem(viewpager.getadapter().getcount() - 1);  

        public void onpagescrolled(int arg0, float arg1, int arg2) {  

        public void onpageselected(int pos) {  

            currentitem = pos;  

            for(int i=0;i < dotviewslist.size();i++){  

                if(i == pos){  

                    ((view)dotviewslist.get(pos)).setbackgroundresource(r.drawable.dot_black);  

                }else {  

                    ((view)dotviewslist.get(i)).setbackgroundresource(r.drawable.dot_white);  

            }  

     *執行輪播圖切換任務 

     *@author caizhiming 

    private class slideshowtask implements runnable{  

        public void run() {  

            synchronized (viewpager) {  

                currentitem = (currentitem+1)%imageviewslist.size();  

                handler.obtainmessage().sendtotarget();  

     * 銷毀imageview資源,回收記憶體 

    private void destorybitmaps() {  

        for (int i = 0; i < image_count; i++) {  

            imageview imageview = imageviewslist.get(i);  

            drawable drawable = imageview.getdrawable();  

            if (drawable != null) {  

                //解除drawable對view的引用  

                drawable.setcallback(null);  

}  

繼續閱讀