Android: programmatically created image vs resource image

删除回忆录丶 提交于 2019-12-25 20:01:14

问题


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


回答1:


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:



来源:https://stackoverflow.com/questions/35241903/android-programmatically-created-image-vs-resource-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!