Open top menu


Exaplain- How to Change Color of an Image/icon in android programmatic.


activity_main.xml.
...................................................................................................................................................................

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
android:layout_gravity="center"
   
android:orientation="vertical">

    <
Button
       
android:id="@+id/red"
       
android:layout_width="300dp"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:text="Change To Red" />

    <
Button
       
android:id="@+id/blue"
       
android:layout_width="300dp"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:text="Change To Blue" />

    <
ImageView
       
android:id="@+id/_icon"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:contentDescription="@null"
       
android:src="@drawable/log" />
</
LinearLayout>

...................................................................................................................................................................


Man_Activity.java
...................................................................................................................................................................
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private static final int STARTING_COLOR = Color.RED;
    ImageView colorView;
    ObjectAnimator colorAnimator;
    ArgbEvaluator colorEvaluator;

    @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        colorView = (ImageView) findViewById(R.id._icon);
        colorView.getDrawable().setColorFilter(STARTING_COLOR, PorterDuff.Mode.MULTIPLY);

        View colorRedButton = findViewById(R.id.red);
        View colorBlueButton = findViewById(R.id.blue);

        colorEvaluator = new ArgbEvaluator();
        colorAnimator = ObjectAnimator.ofObject(colorView, "colorFilter", colorEvaluator,

                0, 0);

        colorRedButton.setOnClickListener(mOnButtonClickListener)
        colorBlueButton.setOnClickListener(mOnButtonClickListener);

    }

    private final View.OnClickListener mOnButtonClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {        

            int currentColor = STARTING_COLOR;
            if (colorAnimator.getAnimatedValue() != null) {

                currentColor = (Integer) colorAnimator.getAnimatedValue();

            }

            switch (v.getId()) {
               case R.id.red:
                    colorAnimator.setObjectValues(currentColor, Color.RED);

                    break;

                case R.id.blue:
                    colorAnimator.setObjectValues(currentColor, Color.BLUE);

                    break;

            }
            colorAnimator.setDuration(2000);
            colorAnimator.start();

        }

    };

}


...................................................................................................................................................................



 try this code...
Tagged

0 comments