首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >努力创建一个简单的形状

努力创建一个简单的形状
EN

Stack Overflow用户
提问于 2012-02-07 06:19:14
回答 1查看 256关注 0票数 0

我是从android开发人员那里用到的,但我不明白为什么它会强制关闭:

打包com.example.shapedrawable.CustomDrawableView;

代码语言:javascript
复制
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);
    }
    }
EN

回答 1

Stack Overflow用户

发布于 2012-02-07 06:36:05

你没有说它在哪里强制关闭,这总是有用的信息,但我假设它在这一行:

代码语言:javascript
复制
mDrawable.getPaint().setColor(Color.BLUE);

在您调用setPaint()之前,getPaint()将返回null。试试这个:

代码语言:javascript
复制
Paint paint = new Paint();
paint.setColor(Color.BLUE);
mDrawable.setPaint(paint);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9168191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档