gpt4 book ai didi

android - CircularImageView 和 API 16 : NullPointerException error

转载 作者:行者123 更新时间:2023-12-02 10:11:11 25 4
gpt4 key购买 nike

我在不同的 API 上测试我的应用程序,它似乎可以在 API 17 到 23 上工作,但我发现在 API 16 中,我无法加载我的自定义 View 。我只想在一个圆圈中显示一张照片,并且我已经从此线程中获得了自定义 View 的代码:How to create a circular ImageView in Android?

我的代码粘贴在这里:

public class CircularImageView extends ImageView {

public CircularImageView(Context context) {
super(context);
}

public CircularImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {

Drawable drawable = getDrawable();

int w = getWidth();

if (drawable == null) {
return;
}

if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();

Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);

}

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;

if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
float factor = smallest / radius;
sbmp = Bitmap.createScaledBitmap(bmp, (int) (bmp.getWidth() / factor), (int) (bmp.getHeight() / factor), false);
} else {
sbmp = bmp;
}

Bitmap output = Bitmap.createBitmap(radius, radius,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, radius, radius);

paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(radius / 2 + 0.7f,
radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);

return output;
}
}

错误发生在这一行:

    if (bmp.getWidth() != radius || bmp.getHeight() != radius) 

这是日志猫:

01-09 15:09:51.185 2119-2119/? E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.example.simon.customshapes.CircularImageView.getCroppedBitmap(CircularImageView.java:59) at com.example.simon.customshapes.CircularImageView.onDraw(CircularImageView.java:51) at android.view.View.draw(View.java:13458) at android.view.View.getDisplayList(View.java:12409) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) at android.view.View.draw(View.java:13461) at android.view.View.getDisplayList(View.java:12409) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) at android.view.View.getDisplayList(View.java:12407) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:3588) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) at android.view.View.draw(View.java:13461) at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3097) at android.view.View.getDisplayList(View.java:12409) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2806) at android.view.View.draw(View.java:13461) at android.view.View.getDisplayList(View.java:12409) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) at android.view.View.getDisplayList(View.java:12407) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) at android.view.View.draw(View.java:13461) at android.support.v4.view.ViewPager.draw(ViewPager.java:2262) at android.view.View.getDisplayList(View.java:12409) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1077) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) at android.view.View.getDisplayList(View.java:12407) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) at android.view.View.getDisplayList(View.java:12407) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) at android.view.View.getDisplayList(View.java:12407) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) at android.view.View.draw(View.java:13461) at android.widget.FrameLayout.draw(FrameLayout.java:467) at android.view.View.getDisplayList(View.java:12409) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) at android.view.View.getDisplayList(View.java:12407) at android.view.View.getDisplayList(View.java:12453) at android.view.View.draw(View.java:13182) at android.view.ViewGroup.drawChild(ViewGroup.java:2929) at a

我无法在 Android API 16 中绘制圆形 ImageView 是否有原因?

最佳答案

好吧,因为您的位图为空,并且可绘制对象并不是唯一可能出现错误的地方,即在 copy(Bitmap, boolean) 内.

如果你查看文档

If the conversion is not supported, or the allocator fails, then this returns NULL.

尝试使用较小的图像,因为这可能是内存不足的迹象。您还应该针对可能返回的空值添加一些防范措施,例如在这种情况下只需绘制普通位图。

关于android - CircularImageView 和 API 16 : NullPointerException error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34695181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com