How to Change image color dynamically in android?

前端 未结 6 767
死守一世寂寞
死守一世寂寞 2020-12-13 12:58

I am doing such type of project ,In my project change Image color dynamically.

I have a one black shape color image ,when user click on this ima

相关标签:
6条回答
  • 2020-12-13 13:21

    In XML use src not background tag in ImageView. In java code-

    import android.graphics.PorterDuff.Mode;
    final Context context=this;
    
            home1=(ImageView) findViewById(R.id.home1);
     Resources res = context1.getResources();
            final int newColor = res.getColor(android.R.color.black);
            home1.setColorFilter(newColor, Mode.SRC_ATOP);
    
    0 讨论(0)
  • 2020-12-13 13:29

    Set android:tint attribute of image/image button to the color you need.

    android:tint="@android:color/black"
    

    Optionally you can set android:tintMode attribute.

    0 讨论(0)
  • 2020-12-13 13:35
    imageView.setImageResource(R.drawable.ic_person_black_48dp);
    
    imageView.setColorFilter(imageView.getContext().getResources().getColor(R.color.desired_color), PorterDuff.Mode.SRC_ATOP);
    
    0 讨论(0)
  • 2020-12-13 13:35

    Put this in your OnDraw, just before you draw your square.

    if (userclicked){
    paint.setColor(Color.GREEN);
    } else {
    paint.setColor(Color.BLACK);
    }
    

    Of course that is if you are drawing it with canvas.drawRect(x0,y0,x1,y1,paint) which you would if you were drawing a simple shape like that.

    0 讨论(0)
  • 2020-12-13 13:39

    Here's how I do this: It's pulling the color from a resource xml file.

    <resources>
    <color name="new_color">#FFAAAAAA</color>
    </resources>
    

    In your activity .java file:

    import android.graphics.PorterDuff.Mode;
    
    Resources res = context.getResources();
    final ImageView image = (ImageView) findViewById(R.id.imageId);
    final int newColor = res.getColor(R.color.new_color);
    image.setColorFilter(newColor, Mode.SRC_ATOP);
    

    To clear it call:

    image.setColorFilter(null);
    
    0 讨论(0)
  • 2020-12-13 13:42

    Create resource in drawable folder like

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <bitmap
                android:src="@drawable/rect"
                android:tint="@color/red"/>
        </item>
    </layer-list>
    

    android:tint="@color/red" does it.

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