首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓:如何改变ShapeDrawable的背景

安卓:如何改变ShapeDrawable的背景
EN

Stack Overflow用户
提问于 2014-01-24 15:15:30
回答 2查看 365关注 0票数 0

我在下面的代码中遇到了问题。

我想创建一个戏剧性的层级列表。如果我用setColor(int color)设置每一层的背景,它会给我一个很好的结果。但是现在我想用drawable来设置背景。但是结果并不是我所期望的。

代码语言:javascript
复制
public LayerDrawable testCreate() {

    ShapeDrawable background = new ShapeDrawable();

    background.getPaint().setStyle(Style.FILL);
    background.getPaint().setColor(R.drawable.progress_background);
            // if using setColor(
    //   getResources().getColor(android.R.color.background_dark)); result will        //be ok



    ShapeDrawable shapeD = new ShapeDrawable();
    shapeD.getPaint().setStyle(Style.FILL);
    shapeD.getPaint().setColor(R.drawable.progress_progress);
    ClipDrawable clipDrawable = new ClipDrawable(shapeD, Gravity.LEFT,
        ClipDrawable.HORIZONTAL);

    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] {
        clipDrawable, background });
    return layerDrawable;

}
EN

回答 2

Stack Overflow用户

发布于 2014-01-24 15:30:43

使用以下函数创建BitmapDrawable

代码语言:javascript
复制
private BitmapDrawable getDrawable(Resources r, int rId) {
    Bitmap image = BitmapFactory.decodeResource(r, rId);
            //to set height and width call this
    //Bitmap.createScaledBitmap(image, height, width, false);
    return new BitmapDrawable(r, image);
}

并将您的函数更改为:

代码语言:javascript
复制
public LayerDrawable testCreate() {

    Drawable background = getDrawable(getResources(),R.drawable.progress_background);

            // if using setColor(android.R.color.background_dark), result will be ok



    Drawable shapeD = getDrawable(getResources(),R.drawable.progress_progress);

    ClipDrawable clipDrawable = new ClipDrawable(shapeD, Gravity.LEFT,
        ClipDrawable.HORIZONTAL);

    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] {
        clipDrawable, background });
    return layerDrawable;

}
票数 1
EN

Stack Overflow用户

发布于 2014-01-24 15:20:42

使用可绘制的数字资源id调用setColor(__int color)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21326650

复制
相关文章

相似问题

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