Android change color of ImageView / Bitmap

后端 未结 5 1269
-上瘾入骨i
-上瘾入骨i 2021-02-15 11:19

I need to find a way to change the color of bitmap in Android. I need to replace/change colors of oval image smoothly in my application depending on int value. I ne

相关标签:
5条回答
  • 2021-02-15 11:59
    getResources().getDrawable( R.drawable.button );
    

    is now deprecated. Can also do it this way:

    ((ImageView) findViewById(R.id.my_icon))
      .setColorFilter(new LightingColorFilter(Color.BLUE, Color.BLUE));
    
    0 讨论(0)
  • 2021-02-15 12:00

    This is how I solved this issue :

    1. Declare an ImageView with src="@drawable/button"
    2. Create a Drawable and set ColorFilter to it and after that use it as src to your declared ImageView like this :

    >

    Drawable myIcon = getResources().getDrawable( R.drawable.button );
    ColorFilter filter = new LightingColorFilter( Color.BLUE, Color.BLUE );
    myIcon.setColorFilter(filter);
    color.setImageDrawable(myIcon);
    
    0 讨论(0)
  • 2021-02-15 12:04

    Should you this.

    Drawable myIcon = getResources().getDrawable( R.drawable.button ); 
    ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK);
    myIcon.setColorFilter(filter);
    
    0 讨论(0)
  • 2021-02-15 12:05

    This solution doesn't work very well for me. In some images the final color was wrong. I use this solution instead:

    Drawable myIcon = getResources().getDrawable(R.drawable.your_image); 
    myIcon.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP); 
    ((ImageView)findViewById(R.id.view_to_change)).setImageDrawable(myIcon);
    
    0 讨论(0)
  • 2021-02-15 12:06

    You can use a TransitionDrawable to achieve this - http://developer.android.com/reference/android/graphics/drawable/TransitionDrawable.html

    0 讨论(0)
提交回复
热议问题