Applying ColorFilter to ImageView with ShapedDrawable

前端 未结 4 1725
北海茫月
北海茫月 2020-11-27 16:21

I have an ImageView with android:src set to a ShapedDrawable, namely a white circle. What I want is to colorize this ImageView

4条回答
  •  清酒与你
    2020-11-27 16:50

    Alright, I had a quick play with this and noticed your issue of the circles disappearing. Without you describing what exactly you tried, I assume you haven't tried setting the color filter to the Drawable itself yet? (as opposed to the ImageView, which only seems to work with BitmapDrawables).

    The following statements work perfectly fine for an xml-declared ShapeDrawable with white as initial color:

    ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
    ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
    ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);
    
    // we can create the color values in different ways:
    redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
    greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
    blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );
    

    The ShapeDrawable for completeness: (I set the size on the ImageView, see below)

    
        
    
    

    And one of the ImageViews as example:

    
    

    Visual result:

    Screenshot of colored circles

提交回复
热议问题