Showing posts with label Android code sample: ImageView. Show all posts
Showing posts with label Android code sample: ImageView. Show all posts

Load WebP from Internet and display in ListView


WebP is a modern image format that provides superior lossless and lossy compression for images on the web. Details refer Google Developers - WebP.

WebP is supported starting from Android 4.0+ (reference: Android Developers - Supported Media Formats). This example modify from the post "Async load image from internet to ListView" to load WebP from internet and display in ListView. Once item clicked, use "Simplest way to open browser using CustomTabsIntent.Builder".


To use CustomTabsIntent.Builder in our app, To use CustomTabsIntent.Builder, edit app/build.gradle to add dependencies of compile 'com.android.support:customtabs:23.0.0'.

The WebP images load from the page Google Developers - WebP Image Galleries.

MainActivity.java
package com.blogspot.android_er.androidimage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.customtabs.CustomTabsIntent;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

final static String src[] = {
"https://www.gstatic.com/webp/gallery3/1_webp_ll.webp",
"https://www.gstatic.com/webp/gallery3/1_webp_a.webp",
"https://www.gstatic.com/webp/gallery3/2_webp_ll.webp",
"https://www.gstatic.com/webp/gallery3/2_webp_a.webp",
"https://www.gstatic.com/webp/gallery3/3_webp_ll.webp",
"https://www.gstatic.com/webp/gallery3/3_webp_a.webp",
"https://www.gstatic.com/webp/gallery3/4_webp_ll.webp",
"https://www.gstatic.com/webp/gallery3/4_webp_a.webp",
"https://www.gstatic.com/webp/gallery3/5_webp_ll.webp",
"https://www.gstatic.com/webp/gallery3/5_webp_a.webp" };

ListView imageList;

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

imageList = (ListView) findViewById(R.id.imagelist);
ArrayList<String> srcList = new ArrayList<String>(Arrays.asList(src));
imageList.setAdapter(new CustomListAdapter(this, srcList));

imageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String imageSrc = src[position];
Toast.makeText(MainActivity.this,
imageSrc,
Toast.LENGTH_LONG).show();

Uri imageUri = Uri.parse(imageSrc);
new CustomTabsIntent.Builder()
.build()
.launchUrl(MainActivity.this, imageUri);
}
});
}

// ----------------------------------------------------

public class CustomListAdapter extends BaseAdapter {
private ArrayList<String> listData;
private LayoutInflater layoutInflater;

public CustomListAdapter(Context context, ArrayList<String> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return listData.size();
}

@Override
public Object getItem(int position) {
return listData.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.icon = (ImageView)convertView.findViewById(R.id.icon);
holder.text = (TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.text.setText(
String.valueOf(position) + "\n" + src[position]);

if (holder.icon != null) {
new BitmapWorkerTask(holder.icon).execute(listData.get(position));
}
return convertView;
}

class ViewHolder {
ImageView icon;
TextView text;
}
}

// ----------------------------------------------------
// Load bitmap in AsyncTask
// ref:
// http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private String imageUrl;

public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage
// collected
imageViewReference = new WeakReference<ImageView>(imageView);
}

// Decode image in background.
@Override
protected Bitmap doInBackground(String... params) {
imageUrl = params[0];
return LoadImage(imageUrl);
}

// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}

private Bitmap LoadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
}
return bitmap;
}

private InputStream OpenHttpConnection(String strURL)
throws IOException {
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();

try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("GET");
httpConn.connect();

if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
} catch (Exception ex) {
}
return inputStream;
}
}
}


layout/row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidimage.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<ListView
android:id="@+id/imagelist"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>


uses-permission of "android.permission.INTERNET" is needed in AndroidManifest.xml

download filesDownload the files .

Sharpen and blur bitmap, convolution using ScriptIntrinsicConvolve3x3

This example show how to create sharpen and blur bitmap, by convolution using ScriptIntrinsicConvolve3x3.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicConvolve3x3;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

ImageView imageA1, imageA2, imageB1, imageB2, imageC1, imageC2;

Bitmap bitmapOriginal, bitmapBlur, bitmapSharpen;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageA1 = (ImageView) findViewById(R.id.imagea1);
imageA2 = (ImageView) findViewById(R.id.imagea2);
imageB1 = (ImageView) findViewById(R.id.imageb1);
imageB2 = (ImageView) findViewById(R.id.imageb2);
imageC1 = (ImageView) findViewById(R.id.imagec1);
imageC2 = (ImageView) findViewById(R.id.imagec2);

bitmapOriginal = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

imageA1.setImageBitmap(bitmapOriginal);
imageA2.setImageBitmap(bitmapOriginal);

// create sharpen bitmap from blur bitmap
bitmapSharpen = createBitmap_convolve(bitmapOriginal, matrix_sharpen);
imageB1.setImageBitmap(bitmapSharpen);
imageB2.setImageBitmap(bitmapSharpen);

// create blur bitmap from original bitmap
bitmapBlur = createBitmap_convolve(bitmapOriginal, matrix_blur);
imageC1.setImageBitmap(bitmapBlur);
imageC2.setImageBitmap(bitmapBlur);

}

float[] matrix_blur =
{ 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f,
1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f,
1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f};

float[] matrix_sharpen =
{ 0, -1, 0,
-1, 5, -1,
0, -1, 0};

private Bitmap createBitmap_convolve(Bitmap src, float[] coefficients) {

Bitmap result = Bitmap.createBitmap(src.getWidth(),
src.getHeight(), src.getConfig());

RenderScript renderScript = RenderScript.create(this);

Allocation input = Allocation.createFromBitmap(renderScript, src);
Allocation output = Allocation.createFromBitmap(renderScript, result);

ScriptIntrinsicConvolve3x3 convolution = ScriptIntrinsicConvolve3x3
.create(renderScript, Element.U8_4(renderScript));
convolution.setInput(input);
convolution.setCoefficients(coefficients);
convolution.forEach(output);

output.copyTo(result);
renderScript.destroy();
return result;
}

}

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<ImageView
android:id="@+id/imagea1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/imagea2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/imageb2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >

<ImageView
android:id="@+id/imagec1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/imagec2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

Next:
interactive exercise of ScriptIntrinsicConvolve3x3

Create blur bitmap using RenderScript and ScriptIntrinsicBlur

ScriptIntrinsicBlur, added in API Level 17, is a Intrinsic Gausian blur filter. Applies a gaussian blur of the specified radius to all elements of an allocation.

Here is a example show how to create blur bitmap with RenderScript and ScriptIntrinsicBlur.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

TextView textTitle;
ImageView image1, image2, image3;
SeekBar seekbarBlurRadius;

Bitmap bitmapOriginal;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTitle = (TextView) findViewById(R.id.title);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);

bitmapOriginal = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

image1.setImageBitmap(bitmapOriginal);

// create blur bitmaps
image2.setImageBitmap(createBitmap_ScriptIntrinsicBlur(bitmapOriginal, 25.0f));
image3.setImageBitmap(createBitmap_ScriptIntrinsicBlur(bitmapOriginal, 25.0f));

seekbarBlurRadius = (SeekBar)findViewById(R.id.blurradius);

seekbarBlurRadius.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
float radius = (float)seekbarBlurRadius.getProgress();
image2.setImageBitmap(createBitmap_ScriptIntrinsicBlur(bitmapOriginal, radius));
image3.setImageBitmap(createBitmap_ScriptIntrinsicBlur(bitmapOriginal, radius));
}});

}

private Bitmap createBitmap_ScriptIntrinsicBlur(Bitmap src, float r) {

//Radius range (0 < r <= 25)
if(r <= 0){
r = 0.1f;
}else if(r > 25){
r = 25.0f;
}

Bitmap bitmap = Bitmap.createBitmap(
src.getWidth(), src.getHeight(),
Bitmap.Config.ARGB_8888);

RenderScript renderScript = RenderScript.create(this);

Allocation blurInput = Allocation.createFromBitmap(renderScript, src);
Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap);

ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
Element.U8_4(renderScript));
blur.setInput(blurInput);
blur.setRadius(r);
blur.forEach(blurOutput);

blurOutput.copyTo(bitmap);
renderScript.destroy();
return bitmap;
}

}

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<SeekBar
android:id="@+id/blurradius"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="25"
android:progress="25" />

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />


</LinearLayout>


Invert bitmap using ColorMatrix

Example to create inverted bitmap using ColorMatrix.


package com.example.androidimageview;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

TextView textTitle;
ImageView image1, image2, image3, image4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTitle = (TextView) findViewById(R.id.title);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);
image4 = (ImageView) findViewById(R.id.image4);

Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

image1.setImageBitmap(bm);

//invert
image2.setImageBitmap(createInvertedBitmap(bm));
image3.setImageBitmap(
createInvertedBitmap(bm));

//invert and invert
image4.setImageBitmap(
createInvertedBitmap(createInvertedBitmap(bm)));

}

private Bitmap createInvertedBitmap(Bitmap src) {
ColorMatrix colorMatrix_Inverted =
new ColorMatrix(new float[] {
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0});

ColorFilter ColorFilter_Sepia = new ColorMatrixColorFilter(
colorMatrix_Inverted);

Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

Paint paint = new Paint();

paint.setColorFilter(ColorFilter_Sepia);
canvas.drawBitmap(src, 0, 0, paint);

return bitmap;
}

}

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<ImageView
android:id="@+id/image4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

</LinearLayout>

Create Sepia bitmap using ColorMatrix

Example to create spedia bitmap uwing ColorMatrix.


package com.example.androidimageview;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

TextView textTitle;
ImageView image1, image2, image3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTitle = (TextView) findViewById(R.id.title);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);

Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

image1.setImageBitmap(bm);
image2.setImageBitmap(createSepia(bm));
image3.setImageBitmap(createSepia(bm));

}

private Bitmap createSepia(Bitmap src) {
ColorMatrix colorMatrix_Sepia = new ColorMatrix();
colorMatrix_Sepia.setSaturation(0);

ColorMatrix colorScale = new ColorMatrix();
colorScale.setScale(1, 1, 0.8f, 1);

colorMatrix_Sepia.postConcat(colorScale);

ColorFilter ColorFilter_Sepia = new ColorMatrixColorFilter(
colorMatrix_Sepia);

Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

Paint paint = new Paint();

paint.setColorFilter(ColorFilter_Sepia);
canvas.drawBitmap(src, 0, 0, paint);

return bitmap;
}

}

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image3"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

Create grayscale bitmap using ColorMatrix

Example to create grayscale bitmap uwing ColorMatrix.


package com.example.androidimageview;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

TextView textTitle;
ImageView image1, image2, image3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTitle = (TextView) findViewById(R.id.title);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);

Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

image1.setImageBitmap(bm);
image2.setImageBitmap(createGrayscale(bm));
image3.setImageBitmap(createGrayscale(bm));

}

private Bitmap createGrayscale(Bitmap src) {
ColorMatrix colorMatrix_Sat0 = new ColorMatrix();
colorMatrix_Sat0.setSaturation(0);
ColorFilter ColorFilter_Grayscale = new ColorMatrixColorFilter(
colorMatrix_Sat0);

Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

Paint paint = new Paint();

paint.setColorFilter(ColorFilter_Grayscale);
canvas.drawBitmap(src, 0, 0, paint);

return bitmap;
}

}

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ImageView
android:id="@+id/image3"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

Various effect of PorterDuff.Mode when using setColorFilter() on ImageView

Last post show a simple example to "Colorize ImageView, using setColorFilter() with Mode.MULTIPLY".  Actually, we have a number of other PorterDuff.Mode. This example show the effects of various PorterDuff.Mode.


package com.example.androidimageview;

import android.support.v7.app.ActionBarActivity;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

TextView textTitle;
ImageView image1;
SeekBar barR, barG, barB, barAlpha;
Spinner spinnerMode;

Mode[] modeValues;
String[] modeName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTitle = (TextView)findViewById(R.id.title);
image1 = (ImageView)findViewById(R.id.image1);
barR = (SeekBar)findViewById(R.id.r);
barG = (SeekBar)findViewById(R.id.g);
barB = (SeekBar)findViewById(R.id.b);
barAlpha = (SeekBar)findViewById(R.id.alpha);

barR.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
barG.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
barB.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
barAlpha.setOnSeekBarChangeListener(myOnSeekBarChangeListener);

//default color
int defaultColor = barAlpha.getProgress() * 0x1000000
+ barR.getProgress() * 0x10000
+ barG.getProgress() * 0x100
+ barB.getProgress();
image1.setColorFilter(defaultColor, Mode.MULTIPLY);

prepareMode();
spinnerMode = (Spinner) findViewById(R.id.selmode);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, modeName);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerMode.setAdapter(adapter);
spinnerMode.setOnItemSelectedListener(myOnItemSelectedListener);
}

OnItemSelectedListener myOnItemSelectedListener = new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
updateMode();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}};

OnSeekBarChangeListener myOnSeekBarChangeListener = new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
updateMode();
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
};

private void updateMode(){
int selectedModePos = spinnerMode.getSelectedItemPosition();
Mode selectedMode = modeValues[selectedModePos];
//Colorize ImageView
int newColor = barAlpha.getProgress() * 0x1000000
+ barR.getProgress() * 0x10000
+ barG.getProgress() * 0x100
+ barB.getProgress();
image1.setColorFilter(newColor, selectedMode);
}

private void prepareMode(){

modeValues = PorterDuff.Mode.values();

modeName = new String[modeValues.length];

for(int i=0; i<modeValues.length; i++){
modeName[i] = modeValues[i].name();
}
}
}

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<Spinner
android:id="@+id/selmode"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<SeekBar
android:id="@+id/r"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<SeekBar
android:id="@+id/g"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<SeekBar
android:id="@+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<SeekBar
android:id="@+id/alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />

</LinearLayout>


download filesDownload the files.

Colorize ImageView, using setColorFilter()

This example show how to colorize ImageView, using setColorFilter() and android.graphics.PorterDuff.Mode.MULTIPLY.



package com.example.androidimageview;

import android.support.v7.app.ActionBarActivity;
import android.graphics.PorterDuff.Mode;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

TextView textTitle;
ImageView image1;
SeekBar barR, barG, barB, barAlpha;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTitle = (TextView)findViewById(R.id.title);
image1 = (ImageView)findViewById(R.id.image1);
barR = (SeekBar)findViewById(R.id.r);
barG = (SeekBar)findViewById(R.id.g);
barB = (SeekBar)findViewById(R.id.b);
barAlpha = (SeekBar)findViewById(R.id.alpha);

barR.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
barG.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
barB.setOnSeekBarChangeListener(myOnSeekBarChangeListener);
barAlpha.setOnSeekBarChangeListener(myOnSeekBarChangeListener);

//default color
int defaultColor = barAlpha.getProgress() * 0x1000000
+ barR.getProgress() * 0x10000
+ barG.getProgress() * 0x100
+ barB.getProgress();
image1.setColorFilter(defaultColor, Mode.MULTIPLY);
}

OnSeekBarChangeListener myOnSeekBarChangeListener = new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {

//Colorize ImageView
int newColor = barAlpha.getProgress() * 0x1000000
+ barR.getProgress() * 0x10000
+ barG.getProgress() * 0x100
+ barB.getProgress();
image1.setColorFilter(newColor, Mode.MULTIPLY);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
};
}


<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<SeekBar
android:id="@+id/r"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<SeekBar
android:id="@+id/g"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<SeekBar
android:id="@+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<SeekBar
android:id="@+id/alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />

</LinearLayout>


Next:
Various effect of PorterDuff.Mode when using setColorFilter() on ImageView

Set opacity (Alpha) of ImageView and background

This example show how to change opacity (Alpha) of ImageView and its background.


package com.example.androidimageview;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

TextView textTitle;
ImageView image1, image2, image3;
SeekBar opacityBar, backgroundOpacityBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTitle = (TextView)findViewById(R.id.title);
image1 = (ImageView)findViewById(R.id.image1);
image2 = (ImageView)findViewById(R.id.image2);
image3 = (ImageView)findViewById(R.id.image3);
opacityBar = (SeekBar)findViewById(R.id.opacity);
backgroundOpacityBar = (SeekBar)findViewById(R.id.backgroundopacity);

image1.setBackgroundColor(0xFFff0000);
image2.setBackgroundColor(0xFFff0000);
image3.setBackgroundColor(0xFFff0000);

opacityBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
//setAlpha (float alpha)
//alpha between 0 and 1
textTitle.setAlpha((float)progress/255);

image1.setAlpha((float)progress/255);

//setAlpha (int alpha) deprecated in API level 16.
image2.setAlpha(progress);

//require API level 16
image3.setImageAlpha(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}});

backgroundOpacityBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {

int backgroundOpacity = progress * 0x01000000;

image1.setBackgroundColor(backgroundOpacity + 0xff0000);
image2.setBackgroundColor(backgroundOpacity + 0xff0000);
image3.setBackgroundColor(backgroundOpacity + 0xff0000);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}});
}

}

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<SeekBar
android:id="@+id/opacity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<SeekBar
android:id="@+id/backgroundopacity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

</LinearLayout>


Next:
Colorize ImageView, using setColorFilter()

Change opacity of ImageView programmatically

This example show how to change opacity of ImageView, using setAlpha(float), deprecated setAlpha(int) and API 16's setImageAlpha(int).


package com.example.androidimageview;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

TextView textTitle;
ImageView image1, image2, image3;
SeekBar opacityBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTitle = (TextView)findViewById(R.id.title);
image1 = (ImageView)findViewById(R.id.image1);
image2 = (ImageView)findViewById(R.id.image2);
image3 = (ImageView)findViewById(R.id.image3);
opacityBar = (SeekBar)findViewById(R.id.opacity);

opacityBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
//setAlpha (float alpha)
//alpha between 0 and 1
textTitle.setAlpha((float)progress/255);

image1.setAlpha((float)progress/255);

//setAlpha (int alpha) deprecated in API level 16.
image2.setAlpha(progress);

//require API level 16
image3.setImageAlpha(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}});
}

}

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidimageview.MainActivity" >

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<SeekBar
android:id="@+id/opacity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255" />

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

</LinearLayout>


Next:
Set opacity (Alpha) of ImageView and background