Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

1) Is it possible to create drawable, example

static Bitmap.Config conf = Bitmap.Config.ARGB_8888;
static Bitmap bmp = Bitmap.createBitmap(100, 100, conf);
// something draw
// eventually convert to DrawableBitmap

and then convert it / asiggn as / put in resource, to use in function with resource id param, like:

public void setWidgetLayoutResource (int widgetLayoutResId)

or
2) is it possible to dynamically draw to change image in R.drawable.something.bmp?

All this for change color of widget in setWidgetLayoutResource() to any color, not fixed as color of concrete resource

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
168 views
Welcome To Ask or Share your Answers For Others

1 Answer

My own answer

This question is relative to my other: Android PreferenceScreen and I was do it in this way:

ColorLinesView.java

public class ColorLinesView extends View
{
    private static GradientDrawable gdDefault = new GradientDrawable();
    public static int fillColor;

    public ColorLinesView(Context context, AttributeSet attrs)
    {   super(context, attrs);
    }

    @Override protected void onDraw(Canvas cv) 
    {   
       gdDefault.setColor(fillColor);
       gdDefault.setCornerRadius(4);
       gdDefault.setStroke(2, 0xffbbbbbb);
       this.setBackgroundDrawable(gdDefault);
    }
}

color_lines_accesory.xml

<?xml version="1.0" encoding="utf-8"?>
<android.swp.ColorLinesView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/colorBoxLines"
 android:layout_width="52dp"
 android:layout_height="26dp"
 android:gravity="right"
 android:layout_marginRight="6dp" />

and finally while programmatically create PreferenceScreen, after add category preference "somePref":

ColorLinesView.fillColor = 0xff00ff00; // examply green
somePref.setWidgetLayoutResource(R.layout.color_lines_accesory);

and the same two lines (with new color) in OnPreferenceClickListener() for "somePref" category, after using color picker to change color.

result:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...