Android从输入位图

时间:2017-07-10 10:20:28

标签: android imageview android-imageview android-canvas

我需要使用线性渐变透明扩展自定义ImageView。如下图像。我输入的是来自fresco的位图数据。 botton 0%不透明度和前100%。你能帮我怎么样?

public class MyImageView extends ImageView (or View) {
    ....
}

myImageView.doIt(myBitmapImageWithoutGradient);

感谢。

my image

更新

public class GradientImageAlpha extends View {

    private Bitmap bitmap;
    private Paint paint;

    public GradientImageAlpha(Context context) {
        super(context);
        init();
    }

    public GradientImageAlpha(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GradientImageAlpha(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
    }

    public void inputBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;

        Shader shaderA = new LinearGradient(0, 0, bitmap.getWidth(), 0, 0xffffffff, 0x00ffffff, Shader.TileMode.CLAMP);
        Shader shaderB = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setShader(new ComposeShader(shaderA, shaderB, PorterDuff.Mode.SRC_IN));

        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), paint);
    }
}

0 个答案:

没有答案