Reputation: 11
I have a problem in the following code.
I want to create a layerlist dramatically. And if I set background of each layer by setColor(int color), it gives me a good result. But now I want to set background by image using drawable. But the result is not what I expected.
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;
}
Upvotes: 0
Views: 386
Reputation: 17401
use following function to create a BitmapDrawable
:
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);
}
and change your function to:
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;
}
Upvotes: 1
Reputation: 16872
you call setColor(int
color) with the numeric resource id of the drawable.
Upvotes: 0