哔哩哔哩地址:
依赖
implementation 'com.ycjiang:ImagePreview:2.3.5'
创建图片初始化类
TestImageLoader
package com.anguomob.market.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.previewlibrary.loader.IZoomMediaLoader;
import com.previewlibrary.loader.MySimpleTarget;
public class TestImageLoader implements IZoomMediaLoader {
@Override
public void displayImage(@NonNull Fragment context, @NonNull String path, final ImageView imageView, @NonNull final MySimpleTarget simpleTarget) {
Glide.with(context)
.asBitmap()
.load(path)
.apply(new RequestOptions().fitCenter())
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
simpleTarget.onResourceReady();
imageView.setImageBitmap(resource);
}
});
}
@Override
public void displayGifImage(@NonNull Fragment context, @NonNull String path, ImageView imageView, @NonNull final MySimpleTarget simpleTarget) {
Glide.with(context)
.asGif()
.load(path)
//可以解决gif比较几种时 ,加载过慢 //DiskCacheStrategy.NONE
.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE).dontAnimate())
//去掉显示动画
.listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
simpleTarget.onResourceReady();
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
simpleTarget.onLoadFailed(null);
return false;
}
})
.into(imageView);
}
@Override
public void onStop(@NonNull Fragment context) {
Glide.with(context).onStop();
}
@Override
public void clearMemory(@NonNull Context c) {
Glide.get(c).clearMemory();
}
}
这个初始化 如果你的app有自己的
MyApplication类 可以写到里面初始化
否则 需要写在main或者使用到的页面上面的onCreate方法中来
ZoomMediaLoader.getInstance().init(TestImageLoader())
下一步写一个用户自定义的数据bean
MarketPropaganda
内容也非常的简单 实现IThumbViewInfo 接口并覆盖要写的字段
import android.graphics.Rect
import android.os.Parcel
import android.os.Parcelable
import androidx.annotation.Nullable
import com.previewlibrary.enitity.IThumbViewInfo
/**
*
* @author ludoven
* @date 2020/4/22
* Deprecated: 图片预览实体类
*/
class MarketPropaganda : IThumbViewInfo {
//图片地址
private var url: String
// 记录坐标
private var mBounds: Rect? = null
private var videoUrl: String? = null
constructor(url: String) {
this.url = url
}
constructor(videoUrl: String?, url: String) {
this.url = url
this.videoUrl = videoUrl
}
override fun getUrl(): String { //将你的图片地址字段返回
return url
}
fun setUrl(url: String) {
this.url = url
}
override fun getBounds(): Rect? { //将你的图片显示坐标字段返回
return mBounds
}
@Nullable
override fun getVideoUrl(): String? {
return videoUrl
}
fun setBounds(bounds: Rect?) {
mBounds = bounds
}
fun setVideoUrl(videoUrl: String?) {
this.videoUrl = videoUrl
}
override fun describeContents(): Int {
return 0
}
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeString(url)
dest.writeParcelable(mBounds, flags)
dest.writeString(videoUrl)
}
protected constructor(`in`: Parcel) {
url = `in`.readString()!!
mBounds = `in`.readParcelable(Rect::class.java.classLoader)
videoUrl = `in`.readString()
}
companion object CREATOR : Parcelable.Creator<MarketPropaganda> {
override fun createFromParcel(parcel: Parcel): MarketPropaganda {
return MarketPropaganda(parcel)
}
override fun newArray(size: Int): Array<MarketPropaganda?> {
return arrayOfNulls(size)
}
}
}
到了使用部分
marketPropagandas.forEachIndexed { index, data ->
//取出ImageView
val iv = arrayImageView.get(index)
//取出数据bean
val marketPropaganda = marketPropagandas.get(index);
iv.setOnClickListener {
val location = IntArray(2)
iv.getLocationOnScreen(location)
marketPropaganda.bounds =
Rect(
location[0],
location[1] ,
location[0] + iv.width,
location[1] + +iv.height
)
GPreviewBuilder.from(this)
.setData(marketPropagandas)
.setCurrentIndex(index)
.setSingleFling(true)//是否在黑屏区域点击返回
.setDrag(true)//是否禁用图片拖拽返回
.setType(GPreviewBuilder.IndicatorType.Dot)
.start()
}
}
计算位置并使用。更加详细的教程烦请参考哔哩哔哩!