仅向Glide Not url提供位图

时间:2018-06-05 08:32:57

标签: android android-glide

我有视频网址列表。我从他们创建缩略图。这些缩略图是bitmap的形式。所以我尝试了

Glide.with(mContext)
                    .load(bitmap)
                    .into(mVideoThumbnail)

我从Here找到了什么。我们可以做这样的事情。

Glide.with(mContext)
                    .load(url).asBitmap()
                    .into(mVideoThumbnail)

但是上面的函数用于将URL加载为位图。它不会将位图作为参数。

我也知道我可以直接将位图设置为图像,如下所述

mVideoThumbnail.setImageBitmap(bitmap);

如果必须为单个视频设置缩略图,则上述方法可以正常工作,但如果是多个视频,则会导致一些性能问题。

我正在分享我的代码,用于将缩略图作为位图获取并设置到我的ImageView中。有没有办法直接将位图传递给Glide或任何其他选项可用于减少性能问题。请帮助

public class TopicInstructionViewHolder implements View.OnClickListener {
@BindView(R.id.iv_thumbnail)
ImageView mVideoThumbnail;
@BindView(R.id.iv_play_video)
ImageView mVideoPlayIcon;
@BindView(R.id.tv_instruction_name)
TextView mInstructionName;
private ITopicVideoPlayListener mTopicVideoPlayListener;
private Context mContext;
private String videoPath;
private int instructionId;
private boolean mHasVideoSeenBL;

public TopicInstructionViewHolder(View itemView,
                                  ITopicVideoPlayListener mTopicVideoPlayListener,
                                  Context mContext) {
    ButterKnife.bind(this, itemView);
    this.mTopicVideoPlayListener = mTopicVideoPlayListener;
    this.mContext = mContext;
}

public void setData(TopicInstructionDetail topicInstructionDetail) {

    String thumbnailPath = null;
    TopicInstructionTranslationDetail topicInstructionTranslationDetails = findTopicInstructionAsPerLang(topicInstructionDetail.getmTopicInstructionTranslationDetails());
    mVideoPlayIcon.setOnClickListener(this);
    videoPath = topicInstructionTranslationDetails.getmInstructionPath();
    mHasVideoSeenBL = topicInstructionDetail.isCompleteSeen();
    instructionId = topicInstructionTranslationDetails.getmInstructionId();
    mInstructionName.setText(topicInstructionTranslationDetails.getmInstructionName());
    thumbnailPath = (NetworkConstants.VIDEO_URL + topicInstructionTranslationDetails.getmThumbnailPath());

    new SampleAsyncTask().execute(NetworkConstants.VIDEO_URL+videoPath);
    if (topicInstructionDetail.isCompleteSeen()) {
        mVideoPlayIcon.setImageResource(R.drawable.check);
    } else {
        mVideoPlayIcon.setImageResource(R.drawable.ic_play);
    }

}

private TopicInstructionTranslationDetail findTopicInstructionAsPerLang(List<TopicInstructionTranslationDetail> topicInstructionTranslationDetails) {

    TopicInstructionTranslationDetail topicInstructionTranslationDetail = null;
    for (TopicInstructionTranslationDetail topicTranslation : topicInstructionTranslationDetails) {
        if (topicTranslation.getmLanguage().equals(AppPreferencesHelper.getInstance(mContext).getCurrentUserLanguage())) {
            topicInstructionTranslationDetail = topicTranslation;
        }
    }

    if (topicInstructionTranslationDetail == null) {
        topicInstructionTranslationDetail = findDefaultTopicInstruction(topicInstructionTranslationDetails);
    }
    return topicInstructionTranslationDetail;
}

private TopicInstructionTranslationDetail findDefaultTopicInstruction(List<TopicInstructionTranslationDetail> topicInstructionTranslationDetails) {

    TopicInstructionTranslationDetail topicInstructionDetail = null;
    for (TopicInstructionTranslationDetail topicTranslation : topicInstructionTranslationDetails) {
        if (topicTranslation.getmLanguage().equals(LanguageCode.getLanguageCode(LanguageCode.LANGUAGE_FIRST))) {
            topicInstructionDetail = topicTranslation;
        }
    }
    return topicInstructionDetail;
}

@Override
public void onClick(View view) {
    mTopicVideoPlayListener.playVideo(videoPath, instructionId, mHasVideoSeenBL);
}

//从视频网址中获取位图     私有类SampleAsyncTask扩展了AsyncTask {

    @Override
    protected Bitmap doInBackground(String... strings) {
        Bitmap bitmap = null;
        MediaMetadataRetriever mediaMetadataRetriever = null;
        try {
            mediaMetadataRetriever = new MediaMetadataRetriever();
            if (Build.VERSION.SDK_INT >= 14) {
                mediaMetadataRetriever.setDataSource(strings[0], new HashMap<String, String>());
            } else {
                mediaMetadataRetriever.setDataSource(strings[0]);
            }
            bitmap = mediaMetadataRetriever.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mediaMetadataRetriever != null) {
                mediaMetadataRetriever.release();
            }
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap s) {
        super.onPostExecute(s);
        try {
            mVideoThumbnail.setImageBitmap(s);
           /* Glide.with(mContext)
                    .load(s).asBitmap()
                    .into(mVideoThumbnail);*/
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

使用Fresco Library代替Glide。

您可以使用facebook fresco库代替滑行,以便在列表中顺利高效地加载。 Fresco库具有pipeline技术,用于在列表中加载图像缩略图。使用Asynctask和fresco管道从视频中获取缩略图并加载它。

检查此answer,为您的问题实施解决方案。

答案 1 :(得分:0)

您正在使用Glide v3。 Glide v4确实添加了将Bitmap加载为Drawable的选项。文档here

更新到v4需要更多配置,请检查here