首页 > 解决方案 > 只为 Glide 提供位图而不是 url

问题描述

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

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

我从这里找到的。我们可以做这样的事情。

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

//从视频 url 获取位图 private class SampleAsyncTask extends 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();
        }
    }
}

}

标签: androidandroid-glide

解决方案


使用 Fresco 库而不是 Glide。

您可以使用 facebook fresco库而不是 glide 来平滑高效地加载列表。Fresco 库具有用于在列表中加载图像缩略图的管道技术。使用 Asynctask 和 fresco 管道从视频中获取缩略图并加载它。

检查这个答案,为您的问题实施解决方案。


推荐阅读