- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
感谢@Budius 所做的一切努力。
我的应用程序的大部分图像工作可以由 Picasso/Glide 处理,但是,某些图像显示在 TextView
中通过Html.fromHtml
。以及 TextView
中的图像也经常使用。
但是,我不知道如何实现getDrawable()
使用 Picasso/Glide 的方法 ImageGetter
传递至Html.fromHtml
。是否可以为TextView
中的这些图片共享Picasso/Glide相同的缓存和其他位图?
或者我应该使用自定义 LruCache
相反,以 ImageGetter
形式缓存这些图片分别地?这种方式会增加 OOM 错误的风险吗?而且我认为使用两个不同的系统来处理图像会产生不必要的工作量。
更新:我尝试使用.get()
picasso ,但医生说
/**
* The result of this operation is not cached in memory because the underlying
* {@link Cache} implementation is not guaranteed to be thread-safe.
*/
所以在这种情况下没有使用缓存。
更新:@Budius 的答案是正确的,但是设置 Drawable
边界的代码缺失,剩下 Drawable
未显示在TextView
中。于是我修改了DrawableWrapper
中的代码类:
public void setWrappedDrawable(Drawable drawable) {
if (mDrawable != null) {
mDrawable.setCallback(null);
}
mDrawable = drawable;
if (drawable != null) {
mDrawable.setBounds(0,0,mDrawable.getIntrinsicWidth(),mDrawable.getIntrinsicHeight());
drawable.setCallback(this);
}
}
更新:,问题仍然没有解决。如果您实现上述解决方案,TextView
中的图像会出现一些奇怪的行为。 。有时,除非切换到另一个应用程序再切换回来,否则图像无法刷新,并且图像位置严重不正确。
更新:我已在下面发布了所有用于测试的代码。仍然存在一些错误。如果没有占位符,它仍然会抛出 NPE。使用占位符时,行为非常奇怪。第一次进入TestActivity
,它显示占位符,但不会更改为下载的图片。但是当我切换到另一个应用程序或按后退按钮并输入 TestActivity
后再次显示图片(也许是因为它在缓存中?)。
而且图片的大小是正确的,但仍然没有为图像留出位置。如果我打电话mDrawable.setBounds(getBounds());
而不是mDrawable.setBounds(0,0,getIntrinsicWidth(),getIntrinsicHeight());
,则不会显示。
public class DrawableWrapper extends Drawable implements Drawable.Callback {
private Drawable mDrawable;
public DrawableWrapper(Drawable drawable) {
setWrappedDrawable(drawable);
}
@Override
public void draw(Canvas canvas) {
mDrawable.draw(canvas);
}
@Override
public int getIntrinsicWidth() {
return 384;
}
@Override
public int getIntrinsicHeight() {
return 216;
}
//... other delegation methods are omitted
public void setWrappedDrawable(Drawable drawable) {
if (mDrawable != null) {
mDrawable.setCallback(null);
}
mDrawable = drawable;
if (drawable != null) {
mDrawable.setBounds(0,0,getIntrinsicWidth(),getIntrinsicHeight());
drawable.setCallback(this);
}
}
}
public class PicassoTargetDrawable extends DrawableWrapper
implements Target {
private Context context;
public PicassoTargetDrawable(Context context) {
super(new ColorDrawable(0));
// use application context to not leak activity
this.context = context.getApplicationContext();
}
public void onBitmapFailed(Drawable errorDrawable) {
setWrappedDrawable(errorDrawable);
invalidateSelf();
}
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
setWrappedDrawable(new BitmapDrawable(context.getResources(), bitmap));
context = null;
invalidateSelf();
}
public void onPrepareLoad(Drawable placeHolderDrawable) {
setWrappedDrawable(placeHolderDrawable);
invalidateSelf();
}
}
public class TestActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
setContentView(textView);
String html = "<div>test<br/>" +
"<img src=\"http://i2.cdn.turner.com/money/dam/assets/150910165544-elon-evo-open-still-384x216.png\"></img>" +
"<br/>/test</div>";
textView.setText(Html.fromHtml(html, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
PicassoTargetDrawable d = new PicassoTargetDrawable(TestActivity.this);
Picasso.with(TestActivity.this)
.load(source)
//add placeholder here
.into(d);
return d;
}
}, null));
}
}
最佳答案
我的建议是返回一个可绘制的包装。并继续使用Picasso下载图像。
在下面的链接中你可以找到一个 DrawableWrapper,它来自 Google 的支持库,但它不是公共(public)文档的一部分,所以我只需将整个代码复制到你的项目中 https://android.googlesource.com/platform/frameworks/support/+/master/v7/appcompat/src/android/support/v7/graphics/drawable/DrawableWrapper.java
然后您可以从中创建一个 PicassoTargetDrawable
。
public class PicassoTargetDrawable extends DrawableWrapper
implements Picasso.Target {
private Context context;
public PicassoTargetDrawable(Context context) {
super(new ColorDrawable(0));
// use application context to not leak activity
this.context = context.getApplicationContext();
}
public void onBitmapFailed(Drawable errorDrawable) {
setWrappedDrawable(errorDrawable);
invalidateSelf();
}
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
setWrappedDrawable(new BitmapDrawable(context.getResources(), bitmap));
context = null;
invalidateSelf();
}
public void onPrepareLoad(Drawable placeHolderDrawable) {
setWrappedDrawable(placeHolderDrawable);
invalidateSelf();
}
}
然后只需加载即可
public void Drawable getDrawable(String source) {
PicassoTargetDrawable d = new PicassoTargetDrawable(context);
Picasso.with(context)
.load(source)
..... add here onError and placeholder drawables
.into(d);
return d;
}
附注:我写的这些都没有查太多资料,可能会有一些错别字和一些需要整理的问题,但是对于你理解这个概念来说肯定足够了。
更新:只是更正您的代码。
TextView 已经告诉 WrapDrawable 它应该使用的边界。如果您告诉新的 mDrawable 它可以使用它想要的任何尺寸,它就会使用它想要的任何尺寸。因此,您应该传递赋予 WrapDrawable 的大小,而不是传递其自身的固有宽度/高度
public void setWrappedDrawable(Drawable drawable) {
if (mDrawable != null) {
mDrawable.setCallback(null);
}
mDrawable = drawable;
if (drawable != null) {
mDrawable.setBounds(getBounds());
drawable.setCallback(this);
}
}
关于android - 如何使 Picasso/Glide 与 Html.ImageGetter 一起缓存图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33040002/
从 Gilde 3.7.0 迁移到 Glide 4.9.0。 如下定义我的 MyAppGlideModule。 @GlideModule public class MyAppGlideModule
我将 Glide JS 用于我使用 ACF 和 Gutenberg Blocks 构建的幻灯片。 但是下一个按钮有问题。 ">"无法正确呈现 - 它会破坏整个 DOM。 “data-glide-dir
我到处搜索,但因为 Glide 已经发布了 4.4 版。我再也找不到应用 RoundedCornersTransformation 的方法了。我正在使用 glide-transformations甚至
我想调整 gif 文件的大小并保存它。我尝试使用一些建议的方法,但这些方法会出错,后来我才知道 Glide v4 中不推荐使用某些方法 byte[] bytes = Glide.
我正在尝试使用 firebase 在 android studio 中开发一个消息应用程序。根本没有语法错误。但是当我使用实现 'com.github.bumptech.glide:glide:4.8
这是我的代码: d.setBounds(15, 8, d.getIntrinsicWidth(), d.getIntrinsicHeight()); ImageSpan span = new Imag
我很难从 firebase 存储中获取图像 URL,我从数据库中获取了照片 URL,但据我所知,Glide 无法从这样的链接获取图片:com.google.firebase .storage.Uplo
我使用 Glide我的应用程序中的库内部自定义适配器 View 。但我有错误: "You must not call setTag() on a view Glide is targeting" 我的
我试图添加编译 应用程式 bulid.gradle compile 'com.github.bumptech.glide:glide:3.7.0' 当我尝试运行该应用程序时,它给了我错误 NoCla
我尝试了所有可能的方法,但无法弄清楚如何在我的 json 更新时更新图像以滑动 我的 android 应用程序最初使用 glide 从 json 加载图像,之后它只从缓存加载图像。它不会加载在服务器上
首先我集成了 Room 数据库并将一些信息存储到 db,其中包含一些图像 URL,现在我需要在 ImageViews 中显示这些图像。 现在尝试使用 glide,添加依赖项和注释处理器, 实现'com
我的房车有问题。我正在将 mp3 的封面艺术加载到字节数组中,然后使用 glide 将字节数组加载到图像中 (vh.coverArt)。但是,当我使用 glide 而不是仅使用 .SetImageBa
简单的 Glide.with(context).load(url).into(image) 在将项目与 同步后无法正常工作 compile 'com.github.bumptech.glide:gli
我真的为 Rails 的所有新 JS 更改而苦苦挣扎。我正在尝试实现非常简单的 slider 脚本 Glide.js 这是我的 app/javascripts/packs/front.js 文件: /
Build.gradle(模块:App) apply plugin: 'com.android.application' android { compileSdkVersion 23
我正在创建一个应用程序,让用户通过按 ImageView 来更改他/她的图像,该图像将来自他/她的图库并将上传到我的 FirebaseStorage 中。用户的个人详细信息单独上传到 Firebase
我可以通过滑动将图像成功加载到 ImageView 中,并使用 .circularCrop() 方法对其进行裁剪。 Glide.with(this) .load("https://i.img
使用 glide 在 recyclerview imageview 项目上加载图像。受阻 Glide 听众我意识到图像资源已准备好,但图像未显示在 imageview 中。当我在 Glide 监听器
我在我的应用程序中使用 glide 加载图像。它在我的情况下工作正常,比如 if(image_enabled==1){ Glide.with(getContext()).load(constant.S
是否可以使用 缓存图像? Glide 未在 imageView 中显示?.如果是那怎么办? 现在我正在做这个代码: Glide .with(getApplicationContext()) .
我是一名优秀的程序员,十分优秀!