Android实现图片点击放大
程序员文章站
2022-06-23 11:30:44
本文实例为大家分享了android实现图片点击放大的具体代码,供大家参考,具体内容如下在我的项目中,有点击图片banner后放大浏览的功能。我的做法就是创建一个专门的图片显示activity,布局里面...
本文实例为大家分享了android实现图片点击放大的具体代码,供大家参考,具体内容如下
在我的项目中,有点击图片banner后放大浏览的功能。我的做法就是创建一个专门的图片显示activity,布局里面用viewpage,这样就能控制图片的左右滑动,并且控制首先显示第几张图片。
功能是ok的,显示也是正常的。但我花费了好几天的时间来实现、完善这个功能。
showmoreimageactivity
/** * 图片放大 */ public class showmoreimageactivity extends baseactivity { @findid(r.id.vp) private viewpager vp; @findid(r.id.ll_point) private linearlayout ll_point; private list<string> imgs; @findid(r.id.btn_save) private imageview btn_save; private int index; public static int type; private activity activity; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_show_more_image); initviews(); activity = this; } private void initviews() { autofindid.findid(context); imgs = (arraylist<string>) getintent().getserializableextra("img"); index = getintent().getintextra("index", 0); type = getintent().getintextra("type", 0); vp.setadapter(new moreimgpageradapter(context, imgs)); vp.addonpagechangelistener(new onpagechangelistener() { @override public void onpageselected(int arg0) { index = arg0; setuppoint(imgs.size(), arg0); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpagescrollstatechanged(int arg0) { } }); setuppoint(imgs.size(), 0); vp.setcurrentitem(index); } protected void download(final string urls) { string[] split = urls.split("\\?"); final string url = split[0]; if (url.startswith("file")) { g.toast(context, "此为本地图片,不用下载,路径为" + url.replace("file://", "")); return; } if (okhttputils.isnetworkavailable(context)) { g.showpd(context); tdutils.execute(new runnable() { @override public void run() { try { file file = new file(c.getdownloadpath()); if (!file.exists()) { file.mkdir(); } file jpg = new file(c.getdownloadpath() + g.urltofilename(url)); // 如果已经存在则不需要下载 if (jpg != null && jpg.exists()) { g.dismissprogressdialoginthread(); g.toastinthread(context, "该文件已被下载到" + jpg.getparent() + context.getresources().getstring(r.string.xia)); return; } // 先从缓存中查找 file tmpfile = netaide.getbitmaputils().getbitmapfilefromdiskcache(url); if (tmpfile != null && tmpfile.exists()) { g.look("---从缓存中查找到图片----"); bitmap bm = bitmapfactory.decodefile(tmpfile.getabsolutepath()); fileoutputstream fos = new fileoutputstream(jpg); bm.compress(compressformat.jpeg, 100, fos); fos.close(); g.dismissprogressdialoginthread(); // 通知图库更新 c.noticeimagerefresh(context, jpg); g.toastinthread(context, context.getresources().getstring(r.string.downloadurl) + jpg.getparent() + context.getresources().getstring(r.string.xia)); return; } // 从网络上下载保存 bitmap bm = bitmapfactory.decodestream(new url(url).openstream()); fileoutputstream fos = new fileoutputstream(jpg); bm.compress(compressformat.jpeg, 100, fos); fos.close(); g.dismissprogressdialoginthread(); // 通知图库更新 c.noticeimagerefresh(context, jpg); g.toastinthread(context, "你现在可以在图库中查看该图片了"); } catch (exception e) { e.printstacktrace(); g.dismissprogressdialoginthread(); g.toastinthread(context, context.getresources().getstring(r.string.downloadfail)); file jpg = new file(c.getdownloadpath() + g.urltofilename(url)); if (jpg != null && jpg.exists()) { jpg.delete(); } } } }); } } private void setuppoint(int size, int choose) { ll_point.removeallviews(); if (size <= 1) { return; } for (int i = 0; i < size; i++) { imageview point = new imageview(context); point.setlayoutparams(new linearlayout.layoutparams(densityutil.dip2px(context, 15), -2)); point.setscaletype(scaletype.fit_center); if (i == choose) { point.setimageresource(r.drawable.white_choosed); } else { point.setimageresource(r.drawable.white_no_choosed); } ll_point.addview(point); } } public void doclcik(view view) { switch (view.getid()){ case r.id.btn_save: permissionutils permissionutils = new permissionutils(); permissionutils.setpermission(this, "存储", "保存图片", new permissionutils.afterpermission() { @override public void donext() { download(imgs.get(index)); } },manifest.permission.write_external_storage); break; } } }
对应布局:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:orientation="vertical"> <androidx.viewpager.widget.viewpager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" /> <linearlayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_margintop="25dp"> <linearlayout android:layout_width="50dp" android:layout_height="match_parent" android:onclick="onfinish"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginleft="10dp" android:background="@drawable/nav_back" /> </linearlayout> <view android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <imageview android:id="@+id/btn_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginright="10dp" android:onclick="doclcik" android:src="@drawable/download_img" /> </linearlayout> <linearlayout android:id="@+id/ll_point" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_marginbottom="40dp" android:gravity="center" android:orientation="horizontal"/> </framelayout>
moreimgpageradapter
public class moreimgpageradapter extends pageradapter { private context context; private list<string> images; private sparsearray<softreference<view>> ivs; public moreimgpageradapter(context context, list<string> images) { this.context = context; this.images = images; ivs = new sparsearray<softreference<view>>(); } @override public int getcount() { return images.size(); } @override public void destroyitem(viewgroup arg0, int arg1, object arg2) { softreference<view> reference = ivs.get(arg1); if (reference != null && reference.get() != null) { arg0.removeview(reference.get()); } } @override public object instantiateitem(viewgroup arg0, final int arg1) { softreference<view> reference = ivs.get(arg1); if (reference == null || reference.get() == null) { view v = layoutinflater.from(context).inflate(r.layout.item_show_more_image, null); reference = new softreference<view>(v); ivs.put(arg1, reference); } view v = reference.get(); final viewholder holder = new viewholder(v); glide.with(context).asbitmap().load(images.get(arg1)).into(holder.image); arg0.addview(v); return v; } @override public boolean isviewfromobject(view arg0, object arg1) { return arg0.equals(arg1); } class viewholder { @findid(r.id.image) private imageview image; @findid(r.id.rl_percent) private relativelayout rl_percent; @findid(r.id.tv_percent) private textview tv_percent; @findid(r.id.iv_top) private imageview iv_top; public viewholder(view v) { autofindid.findidbyview(this, v); } } }
对应布局:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:orientation="vertical" > <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginbottom="50dp" android:layout_margintop="70dp" > <imageview android:layout_gravity="center" android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" /> <imageview android:id="@+id/iv_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|right" android:visibility="gone" android:background="@drawable/shuiyin" /> </framelayout> <relativelayout android:visibility="gone" android:id="@+id/rl_percent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" > <progressbar android:layout_width="40dp" android:layout_height="40dp" /> <textview android:id="@+id/tv_percent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:textcolor="@android:color/white" android:textsize="12sp" /> </relativelayout> </relativelayout>
上面都是次要的,因为我发现了一个更为简便的*。
在我的项目中,我只需要两步就完成了此功能。
第一步:
// 查看大图 implementation 'com.github.sherlockgougou:bigimageviewpager:v4_6.1.1'
第二步:
在点击图片事件那里调用:
imagepreview .getinstance() // 上下文,必须是activity,不需要担心内存泄漏,本框架已经处理好; .setcontext(context) // 设置从第几张开始看(索引从0开始) .setindex(position) // 有三种设置数据集合的方式,根据自己的需求进行三选一: // 1:第一步生成的imageinfo list //.setimageinfolist(imageinfolist) // 2:直接传url list .setimagelist(imagelist) // 3:只有一张图片的情况,可以直接传入这张图片的url //.setimage(string image) // 开启预览 .start();
就这样完成了图片放大浏览、下载的功能,在这里记录下。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。