在Android Studio中,我使用LinearGradient有一个自定义视图

private LinearGradient gradient = new LinearGradient(0, getHeight() / 2, getWidth(),
getHeight() / 2, Color.BLUE, Color.rgb(255, 255, 0), Shader.TileMode.MIRROR);

protected void onDraw(Canvas canvas) {
   int halfWidth = this.getMeasuredWidth() / 2;
   int halfHeight = this.getMeasuredHeight() / 2;
   int radius = halfWidth > halfHeight ? halfHeight - 10 : halfWidth - 10;
   circlePaint.setAntiAlias(true);
   circlePaint.setShader(gradient);
   canvas.drawCircle(halfWidth, halfHeight, radius, circlePaint);
}


这是我尝试在GUI设计器中渲染时得到的stacktrace:

Rendering Problems
Exception raised during rendering:
java.lang.ArrayIndexOutOfBoundsException: -2147483648
    at android.graphics.Gradient_Delegate$GradientPaint.getGradientColor(Gradient_Delegate.java:189)
    at android.graphics.LinearGradient_Delegate$LinearGradientPaint.getColor(LinearGradient_Delegate.java:221)
    at android.graphics.LinearGradient_Delegate$LinearGradientPaint.access$100(LinearGradient_Delegate.java:101)
    at android.graphics.LinearGradient_Delegate$LinearGradientPaint$LinearGradientPaintContext.getRaster(LinearGradient_Delegate.java:195)
    at sun.java2d.pipe.AlphaPaintPipe.renderPathTile(AlphaPaintPipe.java:124)
    at sun.java2d.pipe.AAShapePipe.renderTiles(AAShapePipe.java:201)
    at sun.java2d.pipe.AAShapePipe.renderPath(AAShapePipe.java:159)
    at sun.java2d.pipe.AAShapePipe.fill(AAShapePipe.java:68)
    at sun.java2d.pipe.PixelToShapeConverter.fillOval(PixelToShapeConverter.java:84)
    at sun.java2d.pipe.ValidatePipe.fillOval(ValidatePipe.java:108)
    at sun.java2d.SunGraphics2D.fillOval(SunGraphics2D.java:2268)
    at android.graphics.Canvas_Delegate$5.draw(Canvas_Delegate.java:705)
    at com.android.layoutlib.bridge.impl.GcSnapshot.drawInLayer(GcSnapshot.java:613)
    at com.android.layoutlib.bridge.impl.GcSnapshot.draw(GcSnapshot.java:583)
    at android.graphics.Canvas_Delegate.draw(Canvas_Delegate.java:1117)
    at android.graphics.Canvas_Delegate.native_drawOval(Canvas_Delegate.java:696)
    at android.graphics.Canvas_Delegate.native_drawCircle(Canvas_Delegate.java:722)
    at android.graphics.Canvas.native_drawCircle(Canvas.java)
    at android.graphics.Canvas.drawCircle(Canvas.java:1041)
    at MY.APPLICATION.HELP.CUSTOMVIEW.onDraw(CUSTOMVIEW.java:40) <-- HERE
    at android.view.View.draw(View.java:14839)
    at android.view.View.draw(View.java:14741)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3234)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3037)
    at android.view.View.draw(View.java:14739)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3234)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3037)
    at android.view.View.draw(View.java:14739)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3234)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3037)
    at android.view.View.draw(View.java:14739)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3234)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3037)
    at android.view.View.draw(View.java:14739)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3234)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3037)
    at android.view.View.draw(View.java:14842)
    at android.view.View.draw(View.java:14741)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3234)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3037)
    at android.view.View.draw(View.java:14842)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:582)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:331)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:332)
    at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:575)
    at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:564)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
    at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:564)
    at com.android.tools.idea.rendering.RenderService.render(RenderService.java:691)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:586)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1900(AndroidLayoutPreviewToolWindowManager.java:80)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6$1.run(AndroidLayoutPreviewToolWindowManager.java:528)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
    at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6.run(AndroidLayoutPreviewToolWindowManager.java:523)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
    at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
    at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
    at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
    at com.intellij.util.Alarm$Request$1.run(Alarm.java:327)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)


为什么会出现该错误?这很烦人,因为我无法使用GUI设计器,因为它希望我先解决错误。

最佳答案

如果在onCreate期间调用getHeight和getWidth,则这些函数将返回零。也许这就是您遇到问题的原因。

08-17 19:46