在自定义视图中获取 xml 中 "onDraw" 的上下文
Get context in xml for "onDraw" in a custom view
我想在 onDraw
方法中从 url 加载图像以进行视图预览。我试着用毕加索来做这个。但是,我在 xml 中遇到错误。这是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) {
}
});
}
但是,Picasso 抱怨没有上下文可以从中加载文件。这是错误:
和正文:
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(RenderTask.java:892)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.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)
我整个classlink是here
AFAIK,如果申请不是 运行,则无法获得 Context
参考。
您尝试使用 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);
}
我想在 onDraw
方法中从 url 加载图像以进行视图预览。我试着用毕加索来做这个。但是,我在 xml 中遇到错误。这是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) {
}
});
}
但是,Picasso 抱怨没有上下文可以从中加载文件。这是错误:
和正文:
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(RenderTask.java:892)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.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)
我整个classlink是here
AFAIK,如果申请不是 运行,则无法获得 Context
参考。
您尝试使用 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);
}