android - 只为 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();
}
}
}
}
解决方案
推荐阅读
- c# - C# 单元测试时连接被拒绝
- unix - unix:用 .gz 文件剪切
- java - Spark:将 DataFrame 写入 CSV 时出错
- c - 对于 TCP 套接字上大于 64KB 的数据,如何处理带有 MSG_DONTWAIT 标志的 recv()?
- c++ - 通过类本身检测“this”的变化
- arrays - android中的字符串Jsonarray
- xcode - Xcode 10 中的对象库去了哪里?
- c# - System.InvalidOperationException: '在未连接的套接字上不允许该操作。'
- python - PostgreSQL:“索引行需要 1460216 字节,最大大小为 8191”
- azure-devops - VSTS-Agent 不能 chmod