我在下面的代码中遇到了问题。
我想创建一个戏剧性的层级列表。如果我用setColor(int color)设置每一层的背景,它会给我一个很好的结果。但是现在我想用drawable来设置背景。但是结果并不是我所期望的。
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;
}发布于 2014-01-24 15:30:43
使用以下函数创建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);
}并将您的函数更改为:
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;
}发布于 2014-01-24 15:20:42
使用可绘制的数字资源id调用setColor(__int color)。
https://stackoverflow.com/questions/21326650
复制相似问题