Android 图片预览功能

哔哩哔哩地址:

视频去哪了呢?_哔哩哔哩_bilibili

依赖

    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()
                }
            }

计算位置并使用。更加详细的教程烦请参考哔哩哔哩!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值