我是从android开发人员那里用到的,但我不明白为什么它会强制关闭:
打包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);
}
}发布于 2012-02-07 06:36:05
你没有说它在哪里强制关闭,这总是有用的信息,但我假设它在这一行:
mDrawable.getPaint().setColor(Color.BLUE);在您调用setPaint()之前,getPaint()将返回null。试试这个:
Paint paint = new Paint();
paint.setColor(Color.BLUE);
mDrawable.setPaint(paint);https://stackoverflow.com/questions/9168191
复制相似问题