提问人:Sambhav Khandelwal 提问时间:4/6/2022 最后编辑:Sambhav Khandelwal 更新时间:4/16/2022 访问量:149
在自定义视图中获取“onDraw”的 xml 上下文
Get context in xml for "onDraw" in a custom view
问:
我想从视图预览方法中的 url 加载图像。我试着用毕加索来做到这一点。但是,我在 xml 中出现错误。这就是方法。onDraw
onDraw
@Override
protected void onDraw(Canvas canvas) {
this.canvas = canvas;
Picasso.get()
.load(currentUrl)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable d = new BitmapDrawable(getResources(), bitmap);
d.draw(canvas);
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawText("Error to load",getWidth() / 2, getHeight() / 2,paint);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
和文字:
java.lang.IllegalStateException: context == null
at com.squareup.picasso.Picasso.get(Picasso.java:681)
at com.sambhav2358.badgegeneratorlibrary.BadgeView.setLogo(BadgeView.java:189)
at com.sambhav2358.badgegeneratorlibrary.BadgeView.init(BadgeView.java:69)
at com.sambhav2358.badgegeneratorlibrary.BadgeView.<init>(BadgeView.java:91)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:352)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:189)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:149)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:301)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:417)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:428)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:332)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1127)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1101)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
at android.view.LayoutInflater.inflate(LayoutInflater.java:505)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:361)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:436)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:736)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:892)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:187)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
我的整个班级链接都在这里
答:
0赞
Primož Ivančič
4/6/2022
#1
AFAIK,如果应用程序未运行,则无法获取引用。Context
0赞
Roberto Muñoz Rodríguez
4/6/2022
#2
您是否尝试过使用 Glide?它是另一个收费图像库。
repositories {
mavenCentral()
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.android.support:support-v4:23.1.1'
}
尝试这样做
private void initLocal() {
// Charge img
Glide.with(this).load(URL).into(imgView);
}
评论
2赞
ADM
4/6/2022
问题不在于图书馆。它是关于视图生命周期的。此外,您不应该只是推荐其他一些库作为答案。
0赞
Sambhav Khandelwal
4/6/2022
谢谢你的回答。但是,这不是我想要的
评论
onDraw
Bitmap
Activity
BadgeView.setLogo