Android ViewPager加载图片效果

Android ViewPager加载图片效果,第1张

概述目前项目中需要用到ViewPager加载图片,现在在此记录一下。首先先看布局文件:activity_main.xml

目前项目中需要用到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,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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android ViewPager加载图片效果全部内容,希望文章能够帮你解决Android ViewPager加载图片效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147235.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存