Drawing a rectangle behind a custom non-animated ImageView

I need a custom Image View that first draws some shapes behind the image before it draws the image itself, using the same β€œScaling” that applies to the actual bitmap itself. Everything works fine, except that I have AlphaAnimation to fade out a custom ImageView. It reduces part of BitmapDrawable, but not my form. The shape is always drawn in full opacity. I tried setting the alpha of the paint that I use for the getAlpha view, but no luck. Here is my onDraw function inside my own ImageView class:

@Override
protected void onDraw(Canvas canvas) {
    float[] f = new float[9];
    getImageMatrix().getValues(f);

    final float scaleX = f[Matrix.MSCALE_X];
    final float scaleY = f[Matrix.MSCALE_Y];

    final Drawable d = getDrawable();
    final int origW = d.getIntrinsicWidth();
    final int origH = d.getIntrinsicHeight();

    final int actW = Math.round(origW * scaleX);
    final int actH = Math.round(origH * scaleY);

    canvas.drawRect(Math.max((getWidth() - actW) / 2, 0) + 1, Math.max((getHeight() - actH) / 2, 0) + 1, Math.max((getWidth() - actW) / 2, 0) + actW - 1, Math.max((getHeight() - actH) / 2, 0) + actH - 1, paint);
    super.onDraw(canvas);
}
+4
2

, - , .

+1

, : 5.0.0_r1, onDraw imageView

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mDrawable == null) {
        return; // couldn't resolve the URI
    }

    if (mDrawableWidth == 0 || mDrawableHeight == 0) {
        return;     // nothing to draw (empty bounds)
    }

    if (mDrawMatrix == null && mPaddingTop == 0 && mPaddingLeft == 0) {
        mDrawable.draw(canvas);
    } else {
        int saveCount = canvas.getSaveCount();
        canvas.save();

        if (mCropToPadding) {
            final int scrollX = mScrollX;
            final int scrollY = mScrollY;
            canvas.clipRect(scrollX + mPaddingLeft, scrollY + mPaddingTop,
                    scrollX + mRight - mLeft - mPaddingRight,
                    scrollY + mBottom - mTop - mPaddingBottom);
        }

        canvas.translate(mPaddingLeft, mPaddingTop);

        if (mDrawMatrix != null) {
            canvas.concat(mDrawMatrix);
        }
        mDrawable.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
}

, Paint, , . Paint Drawable.getAlpha()

Drawable.getAlpha() API 19, Drawable :

class MyDrawable extends Drawable {

    final Drawable originalDrawable;
    private int mAlpha = 1;

    @Override
    public int getAlpha() {
        return mAlpha;
    }

    public MyDrawable(Drawable drawable){
        originalDrawable = drawable;
    }

    @Override
    public void draw(Canvas canvas) {
        originalDrawable.draw(canvas);
    }

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

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

    @Override
    public int getOpacity() {
        return originalDrawable.getOpacity();
    }
}
0

Source: https://habr.com/ru/post/1611578/


All Articles