1. 程式人生 > >android網路請求的圖片輪播效果

android網路請求的圖片輪播效果

我們首先畫小圓點我們在res-drawable 資料夾下首先我們建立 別如果是當前的圖片是紅色

shape_select.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <corners android:radius="2dp"/>
    <solid android:color="@color/colorAccent"/>

</shape>

第二個畫小圓點我們在res-drawable 資料夾下首先我們建立 別如果是當前的圖片是藍色

shape_notxml.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <corners android:radius="2dp"/>

    <solid android:color="@color/colorPrimary"/>

</shape>

接下來我們就在activity-main 佈局檔案中寫入ViewPager 控和小圓點的佈局空間
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="test.bwie.com.wzq_lunbo.MainActivity">

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:id="@+id/vp"></android.support.v4.view.ViewPager>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/linear"
            android:layout_centerHorizontal="true"
            android:layout_alignBottom="@id/vp"
            android:layout_marginBottom="25dp"></LinearLayout>
        <com.limxing.xlistview.view.XListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/xlistview"
            android:layout_below="@id/vp"></com.limxing.xlistview.view.XListView>

</RelativeLayout>



主要的我們還有對圖片的快取 使用的是Imagerloader對網路圖片快取 點選藍色字型進行檢視。

好了以上操作完成之後我們就要對MainActivity類進行操作了

package test.bwie.com.wuzeqiang1507d20170825;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.limxing.xlistview.view.XListView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity{

    private ViewPager vp;
    private LinearLayout linear;
    private List<String> imagelist;
    private int currentindext=0;
    private int oldindext=0;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            vp.setCurrentItem(currentindext);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitData();
        //做適配
        vp.setAdapter(new myPager());
        //呼叫執行緒中做無限迴圈
        autoplay();
        init();
		getdata();

    }

    /**
     * vViewpager 新增小圓點
     * 做監聽事件
     */
    private void init() {
        //做對應相同圖片的小圓點
        for ( int i =0;i<imagelist.size();i++){
            //自己定義的檢視空間
            View img = new View(MainActivity.this);
            //檢視的長和寬
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30,30);
            //左間距
            params.leftMargin=10;
            //右間距
            params.rightMargin=10;
            img.setLayoutParams(params);
            if(i==0){
                img.setBackgroundResource(R.drawable.shape_select);
            }else{
                img.setBackgroundResource(R.drawable.shape_notxml);
            }
            //填充給Linearlayout 控制元件中
            linear.addView(img);
        }
        //對viewpager 做滑動監聽
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //當前的介面是成功的 紅色的小圓點
                linear.getChildAt(position % imagelist.size()).setBackgroundResource(R.drawable.shape_select);
                //當前介面不是的顯示的圖片的時候就 藍色的小圓點
                linear.getChildAt(oldindext %imagelist.size()).setBackgroundResource(R.drawable.shape_notxml);
                oldindext=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    //初始化控制元件的iD
    private void InitData() {
        vp = (ViewPager) findViewById(R.id.vp);
linear = (LinearLayout) findViewById(R.id.linear);
		//網路快取的圖片地址
        imagelist = Arrays.asList("http://p2.so.qhmsg.com/bdr/_240_/t01e8378dd608d74de7.jpg",
                "http://p0.so.qhmsg.com/bdr/_240_/t0137613c27d5b716a5.jpg",
                "http://p4.so.qhimgs1.com/bdr/_240_/t01b03cca59569f456a.jpg",
                "http://p2.so.qhimgs1.com/bdr/_240_/t01ebd4aba75daf1cee.jpg"
                );
	}

    /**
     * 線上程中做無限迴圈
     * 通過handler 更新主執行緒UI
     */
    private void autoplay(){
        new Thread(){
            @Override
            public void run() {
                while (true){
                    try {
                        currentindext++;
                        Thread.sleep(3000);
                        handler.sendEmptyMessage(0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

    }

    //viewpager做適配
    class myPager extends PagerAdapter{

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView image = new ImageView(MainActivity.this);
            ImageLoader.getInstance().displayImage(imagelist.get(position %imagelist.size()),image,Myapplication.GetOptios());
            container.addView(image);
            return image;
        }
    }


}
其實不是很多的,我們就是在oncreate 方法中呼叫發放就可以了。每一個方法也就是實現輪播的一個功能,上面都有註釋,仔細觀看