Showing posts with label Android code sample: bitmap and image. Show all posts
Showing posts with label Android code sample: bitmap and image. 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 .

Draw text on Bitmap

Example to draw text on Bitmap.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends ActionBarActivity {

SeekBar textSizeBar;
ImageView image1, image2;
Button btnDrawText;
EditText textIn;

Bitmap bitmapOriginal;

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

textSizeBar = (SeekBar) findViewById(R.id.textsize);
btnDrawText = (Button) findViewById(R.id.drawtext);
textIn = (EditText) findViewById(R.id.textin);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);

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

btnDrawText.setOnClickListener(btnDrawTextOnClickListener);

ReloadImage();

}

OnClickListener btnDrawTextOnClickListener = new OnClickListener(){

@Override
public void onClick(View v) {
ReloadImage();
}};

private void ReloadImage() {

int textSize = textSizeBar.getProgress();
String textToDraw = textIn.getText().toString();

Bitmap newBitmap = bitmapOriginal.copy(bitmapOriginal.getConfig(), true);

Canvas newCanvas = new Canvas(newBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setTextSize(textSize);

Rect bounds = new Rect();
paint.getTextBounds(textToDraw, 0, textToDraw.length(), bounds);
int x = 0;
int y = newBitmap.getHeight();

newCanvas.drawText(textToDraw, x, y, paint);

image1.setImageBitmap(newBitmap);
image2.setImageBitmap(newBitmap);

}

}

<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" />

<EditText
android:id="@+id/textin"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<SeekBar
android:id="@+id/textsize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"
android:progress="10" />

<Button
android:id="@+id/drawtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Draw text on bitmap" />

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

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

<ImageView
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D0D0D0" />
</LinearLayout>

</LinearLayout>


Merge bitmaps

Example show how to combin two bitmaps to one.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends ActionBarActivity {

SeekBar xScaleBar, yScaleBar;
ImageView image1, image2, image3, image4;

Bitmap bitmapOriginal;

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

xScaleBar = (SeekBar) findViewById(R.id.xscale);
yScaleBar = (SeekBar) findViewById(R.id.yscale);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);
image4 = (ImageView) findViewById(R.id.image4);

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

xScaleBar.setOnSeekBarChangeListener(OnScaleChangeListener);
yScaleBar.setOnSeekBarChangeListener(OnScaleChangeListener);

ReloadImage();

}

OnSeekBarChangeListener OnScaleChangeListener = new OnSeekBarChangeListener() {

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

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

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

private void ReloadImage() {

float xScale = (float)(xScaleBar.getProgress()-10) / 10.0f;
float yScale = (float)(yScaleBar.getProgress()-10) / 10.0f;

//between +1 and -1,
//cannot be 0
if (xScale >= 0 && xScale < 0.1f) {
xScale = 0.1f;
}else if(xScale < 0 && xScale > -0.1f){
xScale = -0.1f;
}

if (yScale >= 0 && yScale < 0.1f) {
yScale = 0.1f;
}else if(yScale < 0 && yScale > -0.1f){
yScale = -0.1f;
}

// create scaled bitmap using Matrix
Matrix matrix = new Matrix();
matrix.postScale(xScale, yScale);

Bitmap bitmapScaled = Bitmap.createBitmap(bitmapOriginal, 0, 0,
bitmapOriginal.getWidth(), bitmapOriginal.getHeight(), matrix,
false);

image2.setImageBitmap(bitmapScaled);

//Merge two bitmaps to one
Bitmap bitmapMerged = Bitmap.createBitmap(
bitmapOriginal.getWidth(),
bitmapOriginal.getHeight(),
bitmapOriginal.getConfig());
Canvas canvasMerged = new Canvas(bitmapMerged);
canvasMerged.drawBitmap(bitmapOriginal, 0, 0, null);
canvasMerged.drawBitmap(bitmapScaled, 0, 0, null);

image3.setImageBitmap(bitmapMerged);
image4.setImageBitmap(bitmapMerged);

}

}

<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/xscale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="20"
android:progress="20" />

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

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

<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"
android:background="#D0D0D0" />

<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#B0B0B0" />

<ImageView
android:id="@+id/image4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#909090" />
</LinearLayout>

</LinearLayout>


Flip bitmap using Matrix

Example to create flipped bitmap using Matrix.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends ActionBarActivity {

SeekBar xScaleBar, yScaleBar;
ImageView image1, image2, image3;

Bitmap bitmapOriginal;

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

xScaleBar = (SeekBar) findViewById(R.id.xscale);
yScaleBar = (SeekBar) findViewById(R.id.yscale);
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);

xScaleBar.setOnSeekBarChangeListener(OnScaleChangeListener);
yScaleBar.setOnSeekBarChangeListener(OnScaleChangeListener);

ReloadImage();

}

OnSeekBarChangeListener OnScaleChangeListener = new OnSeekBarChangeListener() {

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

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

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

private void ReloadImage() {

float xScale = (float)(xScaleBar.getProgress()-10) / 10.0f;
float yScale = (float)(yScaleBar.getProgress()-10) / 10.0f;

//between +1 and -1,
//cannot be 0
if (xScale >= 0 && xScale < 0.1f) {
xScale = 0.1f;
}else if(xScale < 0 && xScale > -0.1f){
xScale = -0.1f;
}

if (yScale >= 0 && yScale < 0.1f) {
yScale = 0.1f;
}else if(yScale < 0 && yScale > -0.1f){
yScale = -0.1f;
}

// create scaled bitmap using Matrix
Matrix matrix = new Matrix();
matrix.postScale(xScale, yScale);

Bitmap bitmapScaled = Bitmap.createBitmap(bitmapOriginal, 0, 0,
bitmapOriginal.getWidth(), bitmapOriginal.getHeight(), matrix,
false);

image2.setImageBitmap(bitmapScaled);
image3.setImageBitmap(bitmapScaled);

}

}

<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/xscale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="20"
android:progress="20" />

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

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

<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"
android:background="#D0D0D0" />

<ImageView
android:id="@+id/image3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B0B0B0" />
</LinearLayout>

</LinearLayout>


Rotate bitmap using Matrix

Example show how to create rotated bitmap using Matrix.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends ActionBarActivity {

SeekBar rotateBar;
ImageView image1, image2, image3;

Bitmap bitmapOriginal;

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

rotateBar = (SeekBar)findViewById(R.id.rotate);
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);

rotateBar.setOnSeekBarChangeListener(OnRotateChangeListener);

ReloadImage();

}

OnSeekBarChangeListener OnRotateChangeListener = new OnSeekBarChangeListener(){

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

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

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

private void ReloadImage(){

float degrees = rotateBar.getProgress() - 180;

//create rotated bitmap using Matrix
Matrix matrix = new Matrix();
matrix.postRotate(degrees,
bitmapOriginal.getWidth()/2, bitmapOriginal.getHeight()/2);

Bitmap bitmapRot = Bitmap.createBitmap(
bitmapOriginal,
0, 0,
bitmapOriginal.getWidth(), bitmapOriginal.getHeight(),
matrix, true);

image2.setImageBitmap(bitmapRot);
image3.setImageBitmap(bitmapRot);
}

}

<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/rotate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="360"
android:progress="180" />

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

<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"
android:background="#D0D0D0" />

<ImageView
android:id="@+id/image3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B0B0B0" />
</LinearLayout>

</LinearLayout>


Scale bitmap using Matrix

This example show how to create scaled bitmap using Matrix.


package com.example.androidimageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends ActionBarActivity {

SeekBar xScaleBar, yScaleBar;
ImageView image1, image2;

Bitmap bitmapOriginal;

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

xScaleBar = (SeekBar)findViewById(R.id.xscale);
yScaleBar = (SeekBar)findViewById(R.id.yscale);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);

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

xScaleBar.setOnSeekBarChangeListener(OnScaleChangeListener);
yScaleBar.setOnSeekBarChangeListener(OnScaleChangeListener);

Toast.makeText(MainActivity.this,
bitmapOriginal.getWidth() + " x " + bitmapOriginal.getHeight(),
Toast.LENGTH_LONG).show();

ReloadImage();

}

OnSeekBarChangeListener OnScaleChangeListener = new OnSeekBarChangeListener(){

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

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

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

private void ReloadImage(){

float xScale = xScaleBar.getProgress()/10.0f;
float yScale = yScaleBar.getProgress()/10.0f;

if(xScale<=0){
xScale = 0.1f;
}

if(yScale<=0){
yScale = 0.1f;
}

//create scaled bitmap using Matrix
Matrix matrix = new Matrix();
matrix.postScale(xScale, yScale);

Bitmap bitmapScaled = Bitmap.createBitmap(
bitmapOriginal,
0, 0,
bitmapOriginal.getWidth(), bitmapOriginal.getHeight(),
matrix, true);

image2.setImageBitmap(bitmapScaled);

}

}

<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/xscale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="30"
android:progress="10" />

<SeekBar
android:id="@+id/yscale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="30"
android:progress="10" />

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

<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" />
</LinearLayout>

</LinearLayout>


interactive exercise of ScriptIntrinsicConvolve3x3

Last example show how to "Sharpen and blur bitmap using ScriptIntrinsicConvolve3x3", here is a interactive exercise of ScriptIntrinsicConvolve3x3. You can adjust coefficients of ScriptIntrinsicConvolve3x3, and view the result.


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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

ImageView image1, image2;
SeekBar coeff0, coeff1, coeff2;
SeekBar coeff3, coeff4, coeff5;
SeekBar coeff6, coeff7, coeff8;
SeekBar devBy;
Button btnBlur, btnOrg, btnSharpen;

TextView textCoeff;

float[] matrix = {
0, 0, 0,
0, 1, 0,
0, 0, 0
};

Bitmap bitmapOriginal, bitmapCoeff;

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

coeff0 = (SeekBar)findViewById(R.id.coeff0);
coeff1 = (SeekBar)findViewById(R.id.coeff1);
coeff2 = (SeekBar)findViewById(R.id.coeff2);
coeff3 = (SeekBar)findViewById(R.id.coeff3);
coeff4 = (SeekBar)findViewById(R.id.coeff4);
coeff5 = (SeekBar)findViewById(R.id.coeff5);
coeff6 = (SeekBar)findViewById(R.id.coeff6);
coeff7 = (SeekBar)findViewById(R.id.coeff7);
coeff8 = (SeekBar)findViewById(R.id.coeff8);
devBy = (SeekBar)findViewById(R.id.coeffdivby);
textCoeff = (TextView)findViewById(R.id.textcoeff);
btnBlur = (Button)findViewById(R.id.blur);
btnOrg = (Button)findViewById(R.id.org);
btnSharpen = (Button)findViewById(R.id.sharpen);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);

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

coeff0.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff1.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff2.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff3.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff4.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff5.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff6.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff7.setOnSeekBarChangeListener(OnCoeffChangeListener);
coeff8.setOnSeekBarChangeListener(OnCoeffChangeListener);
devBy.setOnSeekBarChangeListener(OnCoeffChangeListener);

ReloadImage();

btnBlur.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
coeff0.setProgress(1+10);
coeff1.setProgress(1+10);
coeff2.setProgress(1+10);
coeff3.setProgress(1+10);
coeff4.setProgress(1+10);
coeff5.setProgress(1+10);
coeff6.setProgress(1+10);
coeff7.setProgress(1+10);
coeff8.setProgress(1+10);
devBy.setProgress(9-1);
ReloadImage();
}});

btnOrg.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
coeff0.setProgress(0+10);
coeff1.setProgress(0+10);
coeff2.setProgress(0+10);
coeff3.setProgress(0+10);
coeff4.setProgress(1+10);
coeff5.setProgress(0+10);
coeff6.setProgress(0+10);
coeff7.setProgress(0+10);
coeff8.setProgress(0+10);
devBy.setProgress(1-1);
ReloadImage();
}});

btnSharpen.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
coeff0.setProgress(0+10);
coeff1.setProgress(-1+10);
coeff2.setProgress(0+10);
coeff3.setProgress(-1+10);
coeff4.setProgress(5+10);
coeff5.setProgress(-1+10);
coeff6.setProgress(0+10);
coeff7.setProgress(-1+10);
coeff8.setProgress(0+10);
devBy.setProgress(1-1);
ReloadImage();
}});
}

OnSeekBarChangeListener OnCoeffChangeListener = new OnSeekBarChangeListener(){

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

@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

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

private void ReloadImage(){
updateMatrix();
bitmapCoeff = createBitmap_convolve(bitmapOriginal, matrix);
image1.setImageBitmap(bitmapCoeff);
image2.setImageBitmap(bitmapCoeff);
}

private void updateMatrix(){
float div = devBy.getProgress() + 1;
matrix[0] = (coeff0.getProgress()-10)/div;
matrix[1] = (coeff1.getProgress()-10)/div;
matrix[2] = (coeff2.getProgress()-10)/div;
matrix[3] = (coeff3.getProgress()-10)/div;
matrix[4] = (coeff4.getProgress()-10)/div;
matrix[5] = (coeff5.getProgress()-10)/div;
matrix[6] = (coeff6.getProgress()-10)/div;
matrix[7] = (coeff7.getProgress()-10)/div;
matrix[8] = (coeff8.getProgress()-10)/div;

textCoeff.setText(
matrix[0] + " , " + matrix[1] + " , " + matrix[2] + " , \n" +
matrix[3] + " , " + matrix[4] + " , " + matrix[5] + " , \n" +
matrix[6] + " , " + matrix[7] + " , " + matrix[8]);
}

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="wrap_content"
android:orientation="horizontal" >

<SeekBar
android:id="@+id/coeff0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />

<SeekBar
android:id="@+id/coeff1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />

<SeekBar
android:id="@+id/coeff2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />
</LinearLayout>

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

<SeekBar
android:id="@+id/coeff3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />

<SeekBar
android:id="@+id/coeff4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="11" />

<SeekBar
android:id="@+id/coeff5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />
</LinearLayout>

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

<SeekBar
android:id="@+id/coeff6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />

<SeekBar
android:id="@+id/coeff7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />

<SeekBar
android:id="@+id/coeff8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="20"
android:progress="10" />
</LinearLayout>

<SeekBar
android:id="@+id/coeffdivby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="19"
android:progress="0" />

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

<Button
android:id="@+id/blur"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Blur"/>
<Button
android:id="@+id/org"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Original"/>
<Button
android:id="@+id/sharpen"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sharpen"/>

</LinearLayout>

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

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

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

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

</LinearLayout>



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()