我从Android开发人员那里使用它,但不明白为什么它会强行关闭:
package com.example.shapedrawable.CustomDrawableView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;
public class CustomDrawableViewActivity extends View {
private ShapeDrawable mDrawable;
public CustomDrawableViewActivity(Context context) {
super(context);
int x = 10;
int y = 10;
int width = 300;
int height = 50;
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(Color.BLUE);
mDrawable.setBounds(x, y, x + width, y + height);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}
答案 0 :(得分:1)
你没有说哪里它强制关闭哪些总是有用的信息,但我认为它就在这一行:
mDrawable.getPaint().setColor(Color.BLUE);
在您致电getPaint()
之前, setPaint()
将返回null。试试这个:
Paint paint = new Paint();
paint.setColor(Color.BLUE);
mDrawable.setPaint(paint);