Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android ViewPager加載圖片效果

Android ViewPager加載圖片效果

編輯:關於Android編程

目前項目中需要用到ViewPager加載圖片,現在在此記錄一下。

首先先看布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.fly.viewpagerbanner.MainActivity">

 <android.support.v4.view.ViewPager
  android:id="@+id/viewpager"
  android:layout_above="@+id/ll"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />
 <LinearLayout
  android:id="@+id/ll"
  android:layout_marginTop="10.0dip"
  android:orientation="horizontal"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginBottom="24.0dip"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"/>

</RelativeLayout>

MainActivity

package com.example.fly.viewpagerbanner;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 類名:
 * 類描述:ViewPager展示圖片
 * 創建人:fly
 * 創建日期: 2017/1/16.
 * 版本:V1.0
 */
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener,View.OnClickListener {

 private ViewPager vp;
 private ViewPagerAdapter vpAdapter;
 private List<Map<String, Object>> data;
 private int lastValue = -1; // 是不是最後一張圖片
 private ImageView[] dots; //底部小點圖片
 private int currentIndex; //記錄當前選中位置

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //  Intent intent = new Intent();
//  intent.putStringArrayListExtra("GalleryPlusActivity",ArrayList<String>); // 怎麼傳遞集合
  ArrayList<String> imageLists = getIntent().getStringArrayListExtra("GalleryPlusActivity"); // 動態獲取展示圖片的集合
  data = getData(imageLists);
  vp = (ViewPager) findViewById(R.id.viewpager);
  vpAdapter = new ViewPagerAdapter(data,this);
  vp.setAdapter(vpAdapter);
  vp.addOnPageChangeListener(this);
  initDots();//初始化底部小點
 }

 //這個方法長度是動態的,可以改成你從服務器獲取的圖片,這樣數量就不確定啦
 public List<Map<String, Object>> getData(ArrayList<String> imageLists) {
  List<Map<String, Object>> mData = new ArrayList<>();

//  if (imageLists != null && imageLists.size() > 0) {
//   for (int i = 0; i < imageLists.size(); i++) {
//    Map<String, Object> map = new HashMap<>();
//    map.put("url", imageLists.get(i));
//    map.put("view", new ImageView(this));
//    mData.add(map);
//   }
//  }

  Map<String, Object> map = new HashMap<>();
  map.put("url", "http://img2.duitang.com/uploads/item/201207/19/20120719132725_UkzCN.jpeg");
  map.put("view", new ImageView(this));
  mData.add(map);

  Map<String, Object> map1 = new HashMap<>();
  map1.put("url", "http://img4.duitang.com/uploads/item/201404/24/20140424195028_vtvZu.jpeg");
  map1.put("view", new ImageView(this));
  mData.add(map1);

  Map<String, Object> map2 = new HashMap<>();
  map2.put("url", "http://download.pchome.net/wallpaper/pic-5041-8-240x320.jpg");
  map2.put("view", new ImageView(this));
  mData.add(map2);

  Map<String, Object> map3 = new HashMap<>();
  map3.put("url", "http://www.mangowed.com/uploads/allimg/130425/572-130425105311304.jpg");
  map3.put("view", new ImageView(this));
  mData.add(map3);
  return mData;
 }

 private void initDots() {
  LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  mLayoutParams.setMargins(3, 0, 3, 0);
  LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
  dots = new ImageView[data.size()];
  //循環取得小點圖片
  for (int i = 0; i < data.size(); i++) {
   dots[i] = new ImageView(this);
   dots[i].setLayoutParams(mLayoutParams);
   dots[i].setBackgroundResource(R.drawable.dot);
   dots[i].setEnabled(true);//都設為灰色
   dots[i].setOnClickListener(this);
   dots[i].setTag(i);//設置位置tag,方便取出與當前位置對應
   ll.addView(dots[i]);
  }
  currentIndex = 0;
  dots[currentIndex].setEnabled(false);//設置為白色,即選中狀態
 }

 private void setCurView(int position) { /**設置當前的引導頁*/
  if (position < 0 || position >= data.size()) {return;}
  vp.setCurrentItem(position);
 }

 private void setCurDot(int position) { /**設置當前引導小點的選中*/
  if (position < 0 || position > data.size() - 1 || currentIndex == position) {return;}
  dots[position].setEnabled(false);
  dots[currentIndex].setEnabled(true);
  currentIndex = position;
 }

 @Override //當前頁面被滑動時調用
 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  lastValue = position;
 }

 @Override
 public void onPageSelected(int position) {     //當新的頁面被選中時調用
  setCurDot(position); //設置底部小點選中狀態
 }

 @Override
 public void onPageScrollStateChanged(int state) {   //當滑動狀態改變時調用
  if(state == 0){
   if(lastValue == data.size()-1){
    Toast.makeText(this, "已經是最後一張了", Toast.LENGTH_SHORT).show();
   }
  }
 }

 @Override
 public void onClick(View v) {
  int position = (Integer)v.getTag();
  setCurView(position);
  setCurDot(position);
 }
}

ViewPagerAdapter

package com.example.fly.viewpagerbanner;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;
import java.util.Map;

public class ViewPagerAdapter extends PagerAdapter {
 Context context;
 List<Map<String, Object>> viewLists;

 public ViewPagerAdapter(List<Map<String, Object>> lists, Context context) {
  this.viewLists = lists;
  this.context = context;
 }

 @Override
 public int getCount() {  //獲得size
  return viewLists.size();
 }

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

 @Override
 public void destroyItem(ViewGroup view, int position, Object object) { //銷毀Item
  ImageView x = (ImageView) viewLists.get(position).get("view");
  x.setScaleType(ImageView.ScaleType.FIT_CENTER);
  view.removeView(x);
 }

 @Override
 public Object instantiateItem(ViewGroup view, int position){ //實例化Item
  ImageView imageView = (ImageView) viewLists.get(position).get("view");
  imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
  Glide.with(context)
    .load(viewLists.get(position).get("url").toString())
//    .placeholder(R.mipmap.new_default)
    .error(R.mipmap.ic_launcher)
    .diskCacheStrategy(DiskCacheStrategy.RESULT)
    .into(imageView);
  view.addView(imageView, 0);

  return viewLists.get(position).get("view");
 }
}

是不是很簡單。
最後附上demo:http://xiazai.jb51.net/201701/yuanma/ViewPager(jb51.net).rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved