我在image
为ImageView
:
Drawable d = getResources().getDrawable(R.drawable.somejpeg);
//only one of the lines below
image.setImageDrawable(d); //works
image.setImageDrawable(new DrawableDecorator(d)); //does not work - blank image
这是我的可绘装饰者:
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
public final class DrawableDecorator extends Drawable {
private final Drawable drawable;
public DrawableDecorator(Drawable drawable) {
this.drawable = drawable;
}
@Override
public void draw(Canvas canvas) {
drawable.draw(canvas);
}
@Override
public void setAlpha(int alpha) {
drawable.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
drawable.setColorFilter(cf);
}
@Override
public int getOpacity() {
return drawable.getOpacity();
}
}
我已经覆盖了我所拥有的东西,只是传递给装饰物品,但我错过了什么导致它失败?
请注意,调用draw方法。
答案 0 :(得分:1)
我应该通过一些更重要的覆盖。
@Override
public boolean setVisible(boolean visible, boolean restart) {
return drawable.setVisible(visible, restart);
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
drawable.setBounds(left, top, right, bottom);
}
@Override
public void setBounds(Rect bounds) {
drawable.setBounds(bounds);
}
@Override
public int getIntrinsicHeight() {
return drawable.getIntrinsicHeight();
}
@Override
public int getIntrinsicWidth() {
return drawable.getIntrinsicWidth();
}