Skip to main content

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

Popular posts from this blog

How to use NumberPicker in Android

Table of contentsNumberPicker ExamplesetOnValueChangedListeneronValueChange()setWrapSelectorWheel()setMinValue()setMinValue()setMaxValue()NumberPicker with String ArraysetDisplayedValues()String Array1. NumberPicker Example activity_main.xml <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" android:background="#ffffff" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="Select a number..." /> <NumberPicker android:id="@+id/np" …

TextView new line (multiline) in android

TextView new line (multiline) TextView display text on android app. by default, TextView show text on a single line, and if it is long then TextView take more lines to display its text. android developers can generate a new line on TextView both programmatically and syntactically. android developers can make a multiline TextView without splitting text to multiline by android:minLines attribute.

the following android example code demonstrate us how can we syntactically create a new line on TextView widget by xml layout file and string resource file.

the simplest way to create a new line on TextView is android:text attribute. android:text attribute allow us to display text on android app. we can add a simple '\n' to TextView text where we want to start a new line. in this way we can create a multiline TextView widget in android app. we can assign android:text attribute value by this way android:text="Line1 \n Line2 \n Line3" for a TextView widget. this value will…

How to change TextView font size in android

TextView font size TextView widget display text on android application. we can set or change TextView font size statically by declarative syntax in xml layout file or programmatically at run time in java file. even we can use an xml file source to define font size.

the following example code demonstrate us how can we define TextView font size in xml layout file and how can we uses dimens.xml to reference font size. in this example we did not changes any coding in java file, so here we only include the layout xml file and dimens.xml file. activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="25dp" tools:context=".MainActivity" > <TextView android:id=&…