gpt4 book ai didi

android - 空指针异常 : While getting the bitmap from canvas

转载 作者:行者123 更新时间:2023-12-05 07:35:06 26 4
gpt4 key购买 nike

我想要实现的目标:我想创建一个自定义 View ,即圆圈,现在我希望该圆圈在我的 Activity 中可见,这对我来说很酷,我在 Activity 中提到过这门课。当我点击膨胀的圆圈然后触摸设备屏幕中的任何位置然后绘制圆圈时我想要什么。

想法是:在这个圆形 View 的触摸上我应该生成位图,当用户触摸时我应该使位图适合父布局。

 public class CircleIcon extends View {
private int centerX,centerY;
private Paint outerCircularPaint,innerCircularPaint;
private static final int STROKE_WIDTH = 5;
Bitmap bitmap;
Canvas mCanvas;

public CircleIcon(Context context) {
super(context);
init();
}

public CircleIcon(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

public CircleIcon(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CircleIcon(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);
init();
}

private void init(){
bitmap = Bitmap.createBitmap(100,100, Bitmap.Config.ARGB_8888);
outerCircularPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
outerCircularPaint.setStyle(Paint.Style.STROKE);
outerCircularPaint.setStrokeWidth(STROKE_WIDTH);

outerCircularPaint.setAntiAlias(true);
outerCircularPaint.setStrokeCap(Paint.Cap.BUTT);
outerCircularPaint.setColor(ContextCompat.getColor(getContext(), R.color.color_black));

innerCircularPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
innerCircularPaint.setStyle(Paint.Style.FILL);
innerCircularPaint.setStrokeWidth(STROKE_WIDTH);
innerCircularPaint.setColor(ContextCompat.getColor(getContext(), R.color.white));

}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
centerX = getMeasuredWidth()/ 2;
centerY = getMeasuredHeight()/ 2;
int radius = Math.min(centerX,centerY);
canvas.drawCircle(centerX, centerY, radius, outerCircularPaint);
canvas.drawCircle(centerX, centerY, radius-1, innerCircularPaint);
canvas.drawBitmap(bitmap, 0, 0, outerCircularPaint);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.e("called---",""+w);
bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(bitmap);
}



public Bitmap getBitmap()
{
this.setDrawingCacheEnabled(true);
this.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(this.getDrawingCache());// here throwing nullpointer exception
this.setDrawingCacheEnabled(false);
return bmp;
}
}

在 Activity 中:

private void initCustomMarkerBitmap() {
Bitmap well = circleView.getBitmap();
Bitmap save = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
Canvas now = new Canvas(save);
now.drawRect(new Rect(0,0,320,480), paint);
now.drawBitmap(well, new Rect(0,0,well.getWidth(),well.getHeight()), new Rect(0,0,320,480), null);
this.mCustomDrawableBitmap=save;
this.mCustomDrawableMarker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(this.mCustomDrawableBitmap));
}

Logcat 错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at android.graphics.Bitmap.createBitmap(Bitmap.java:659)
at com.example.amit8.comapanydemo.customicon.CircleIcon.getBitmap(CircleIcon.java:103)
at com.example.amit8.comapanydemo.ui.base.main.MainActivity.initCustomMarkerBitmap(MainActivity.java:218)
at com.example.amit8.comapanydemo.ui.base.main.MainActivity.onCreate(MainActivity.java:84)
at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)

最佳答案

将此行放在您的 init() 方法中,然后将其从 getBitmap() 方法中删除。

setDrawingCacheEnabled(true);

检查它是否正常工作。

关于android - 空指针异常 : While getting the bitmap from canvas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49718804/

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