Monday, November 9, 2015

android - Color filter and different PorterDuff modes

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    >
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Color filter by : RED"
        android:textStyle="bold"
        android:textColor="#8f9e92"
        android:fontFamily="sans-serif-condensed"
        />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:src="@drawable/butterfly"
        android:layout_below="@id/tv"
        android:background="#c5d1c5"
        android:padding="2dp"
        android:layout_margin="2dp"
        />
    <ImageView
        android:id="@+id/iv_filter"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:src="@drawable/butterfly_filter"
        android:layout_below="@id/tv"
        android:layout_toRightOf="@id/iv"
        android:background="#c5d1c5"
        android:padding="2dp"
        android:layout_margin="2dp"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.graphics.PorterDuff.Mode;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private TextView tv;
    private ImageView iv;
    private Spinner spinner;

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

        // Get the application context
        mContext = getApplicationContext();

        // Get the widgets reference from XML layout
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        tv = (TextView) findViewById(R.id.tv);
        iv = (ImageView) findViewById(R.id.iv_filter);
        spinner = (Spinner) findViewById(R.id.spinner);

        // Initializing a new String array
        // Populate the array with PorterDuff different modes
        String[] modes = new String[]{
                "ADD",
                "CLEAR",
                "DARKEN",
                "DST",
                "DST_ATOP",
                "DST_IN",
                "DST_OUT",
                "DST_OVER",
                "LIGHTEN",
                "MULTIPLY",
                "OVERLAY",
                "SCREEN",
                "SRC",
                "SRC_ATOP",
                "SRC_IN",
                "SRC_OUT",
                "SRC_OVER",
                "XOR"
        };

        // Initialize a new ArrayAdapter and populate it with String array
        ArrayAdapter<String> adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,modes);

        // Define the ArrayAdapter items layout
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // Set the Spinner Adapter
        spinner.setAdapter(adapter);

        // Set an item selected listener to Spinner object
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                // Get the Spinner selected item (PorterDuff Mode)
                String mode = adapterView.getItemAtPosition(i).toString();

                /*
                    public final void setColorFilter (int color, PorterDuff.Mode mode)
                        Set a tinting option for the image.

                        Related XML Attributes
                            android:tint
                        Parameters
                            color : Color tint to apply.
                            mode : How to apply the color. The standard mode is SRC_ATOP

                    public static PorterDuff.Mode valueOf (String name)
                */
                // Apply the color filter to ImageView image
                iv.setColorFilter(Color.RED,Mode.valueOf(mode));

                // Another way to apply color filter
                /*
                    PorterDuffColorFilter
                        A color filter that can be used to tint the source pixels using a single
                        color and a specific Porter-Duff composite mode.

                    public PorterDuffColorFilter (int color, PorterDuff.Mode mode)
                        Create a color filter that uses the specified color and Porter-Duff mode.

                    Parameters
                        color : The ARGB source color used with the specified Porter-Duff mode
                        mode : The porter-duff mode that is applied
                */
                /*
                    public void setColorFilter (ColorFilter cf)

                        Apply an arbitrary colorfilter to the image.

                    Parameters
                        cf : the colorfilter to apply (may be null)
                */
                /*
                    public abstract void setColorFilter (ColorFilter colorFilter)

                        Specify an optional color filter for the drawable.
                        If a Drawable has a ColorFilter, each output pixel of the Drawable's drawing
                        contents will be modified by the color filter before it is blended onto the
                         render target of a Canvas.

                        Pass null to remove any existing color filter.

                    Note : Setting a non-null color filter disables tint.

                    Parameters
                        colorFilter The color filter to apply, or null to remove the existing color filter
                */
                /*
                    ColorFilter
                        Known Direct Subclasses
                            ColorMatrixColorFilter, LightingColorFilter, PorterDuffColorFilter

                        A color filter can be used with a Paint to modify the color of each pixel
                        drawn with that paint. This is an abstract class that should never be used directly.
                */
                //PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED,Mode.valueOf(mode));
                //iv.setColorFilter(filter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // Do something
            }
        });
    }
}
More android examples