In an image editor application, transforming an original image is a common task. In this Android tip, I am going to show you how to transform an image by applying color filter to it. You can create a color filter by creating an instance of ColorFilter class passing a ColorMatrix object. The ColorMatrix object is a 4x5 matrix that is represented by a one-dimensional array. It is used to transform the color+alpha components of the original image. You can come up with a color matrix as shown below:
colorMatrix=[
a,b,c,d,e,
f,g,h,i,j,
k,l,m,n,o,
p,q,r,s,t
]
Here are the color components (Rr, Gr, Br, and Ar) of the transformed image after the color matrix is applied to its original color components (R, G, B, and A):
Rr=a*R+b*G+c*B+d*A+e;
Gr=f*R+g*G+h*B+i*A+j;
Br=k*R+l*G+m*B+n*A+o;
Ar=p*R+q*G+r*B+s*A+t;
Now to have an example application on image color filter, you create a new Android project. Then add an ImageView to the activity_main.xml file to display the transformed image.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
android:id="@+id/llayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imgview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="10dp"
/>
</LinearLayout>
In the MainActivity class, you have to load an original image from a res/drawable directory, transform it, and display the result image in the ImageView.import java.util.HashMap;
import java.util.List;
import android.app.Activity;
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;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.imgview);
// Get the front image
Bitmap originalImg = BitmapFactory.decodeResource(getResources(), R.drawable.or_image);
// Create blank bitmap object
Bitmap bitmap = Bitmap.createBitmap(originalImg.getWidth(), originalImg.getHeight(),
Bitmap.Config.ARGB_8888);
// Create Canvas object for drawing on the bitmap object
Canvas canvas = new Canvas(bitmap);
// Create paint object
final Paint paint = new Paint();
// Create color matrix object
ColorMatrix colorMatrix=
new ColorMatrix(new float[]{
1, 0, 0, 1, 253,
0, 0, 0, 1, 253,
1, 0, 0, 1, 253,
0, 0, 0, 1, 253});
ColorFilter colorFilter= new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorFilter);
// Apply color filtering to the original image
canvas.drawBitmap(originalImg, 0, 0, paint);
// Display the result image
imageView.setImageBitmap(bitmap);
}
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.