ImageView上的StateListDrawable无法正常工作

时间:2014-07-07 15:08:27

标签: java android imageview drawable android-source

从AOSP Launcher3尝试这段代码时:

void generatePressedFocusedStates(ImageView v) {
    if (!mStatesUpdated && v != null) {
        mStatesUpdated = true;
        Bitmap original = createOriginalImage(v, mTempCanvas);
        Bitmap outline = createImageWithOverlay(v, mTempCanvas, mHighlightColor);
        Bitmap hotword = createImageWithOverlay(v, mTempCanvas, mHotwordColor);
        FastBitmapDrawable originalD = new FastBitmapDrawable(original);
        FastBitmapDrawable outlineD = new FastBitmapDrawable(outline);
        FastBitmapDrawable hotwordD = new FastBitmapDrawable(hotword);

        StateListDrawable states = new StateListDrawable();

        states.addState(new int[] {android.R.attr.state_pressed}, outlineD);
        states.addState(new int[] {android.R.attr.state_focused}, outlineD);
        states.addState(new int[] {R.attr.stateHotwordOn}, hotwordD);
        states.addState(new int[] {}, originalD);
        v.setImageDrawable(states);
    }
}

2 ImageView我应用它突然失去了他们的Drawables:他们是空的。

如果我用

替换最后一行
 v.setImageDrawable(originalD);
 // or
 v.setImageDrawable(outlineD);
 // or
 v.setImageDrawable(hotwordD);

然后ImageViews显示所选的drawable。所以我认为这证明了ImageView或Drawables没有问题......只有StateListDrawable。


完成后,以下是使用的方法:

private Bitmap createOriginalImage(ImageView v, Canvas canvas) {
    final Drawable d = v.getDrawable();
    final Bitmap b = Bitmap.createBitmap(
            d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

    canvas.setBitmap(b);
    canvas.save();
    d.draw(canvas);
    canvas.restore();
    canvas.setBitmap(null);

    return b;
}

-

private Bitmap createImageWithOverlay(ImageView v, Canvas canvas, int color) {
    final Drawable d = v.getDrawable();
    final Bitmap b = Bitmap.createBitmap(
            d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

    canvas.setBitmap(b);
    canvas.save();
    d.draw(canvas);
    canvas.restore();
    canvas.drawColor(color, PorterDuff.Mode.SRC_IN);
    canvas.setBitmap(null);

    return b;
}

-

class FastBitmapDrawable extends Drawable {
private Bitmap mBitmap;
private int mAlpha;
private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);

FastBitmapDrawable(Bitmap b) {
    mAlpha = 255;
    mBitmap = b;
    setBounds(0, 0, b.getWidth(), b.getHeight());
}

@Override
public void draw(Canvas canvas) {
    final Rect r = getBounds();
    // Draw the bitmap into the bounding rect
    canvas.drawBitmap(mBitmap, null, r, mPaint);
}

@Override
public void setColorFilter(ColorFilter cf) {
    mPaint.setColorFilter(cf);
}

@Override
public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
}

@Override
public void setAlpha(int alpha) {
    mAlpha = alpha;
    mPaint.setAlpha(alpha);
}

public void setFilterBitmap(boolean filterBitmap) {
    mPaint.setFilterBitmap(filterBitmap);
    mPaint.setAntiAlias(filterBitmap);
}

public int getAlpha() {
    return mAlpha;
}

@Override
public int getIntrinsicWidth() {
    return getBounds().width();
}

@Override
public int getIntrinsicHeight() {
    return getBounds().height();
}

@Override
public int getMinimumWidth() {
    return getBounds().width();
}

@Override
public int getMinimumHeight() {
    return getBounds().height();
}

public Bitmap getBitmap() {
    return mBitmap;
}
}

0 个答案:

没有答案