Android ViewPager加载图片效果
程序员文章站
2024-02-16 19:07:58
目前项目中需要用到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, 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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java 安全模型,你了解了吗
下一篇: Android离线缓存的实例代码