Before Tint Color Effect
After Tint Color Effect
| Tint Degree=30 |
| Tint Degree=30 |
| Tint Degree=50 |
| Tint Degree=80 |
Create new Android Project
Project Name: PlayingwithBitmaps
Build Target: Android 2.3.3 //or greater than that
Application Name: PlayingwithBitmaps
Package Name: com.hamad.playingwithbitmaps
Create Activity: Main
Min SDK: 10 // or greater than that
- create main layout:
- One image view to display the image.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:id="@+id/rlMain" >
<ImageView
android:id="@+id/imViewAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/car" />
</RelativeLayout>
2. code of main activity:package com.shaikhhamadali.blogspot.playingwithbitmaps;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
public class Main extends Activity {
ImageView imViewAndroid;
public static final double PI = 3.14159d;
public static final double FULL_CIRCLE_DEGREE = 360d;
public static final double HALF_CIRCLE_DEGREE = 180d;
public static final double RANGE = 256d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imViewAndroid = (ImageView) findViewById(R.id.imViewAndroid);
imViewAndroid.setImageBitmap(applyTintEffect(BitmapFactory.decodeResource(getResources(), R.drawable.car),80));
}
public Bitmap applyTintEffect(Bitmap src, int degree) {
// get source image size
int width = src.getWidth();
int height = src.getHeight();
int[] pix = new int[width * height];
// get pixel array from source
src.getPixels(pix, 0, width, 0, 0, width, height);
int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y;
double angle = (PI * (double)degree) / HALF_CIRCLE_DEGREE;
int S = (int)(RANGE * Math.sin(angle));
int C = (int)(RANGE * Math.cos(angle));
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
int index = y * width + x;
int r = ( pix[index] >> 16 ) & 0xff;
int g = ( pix[index] >> 8 ) & 0xff;
int b = pix[index] & 0xff;
RY = ( 70 * r - 59 * g - 11 * b ) / 100;
GY = (-30 * r + 41 * g - 11 * b ) / 100;
BY = (-30 * r - 59 * g + 89 * b ) / 100;
Y = ( 30 * r + 59 * g + 11 * b ) / 100;
RYY = ( S * BY + C * RY ) / 256;
BYY = ( C * BY - S * RY ) / 256;
GYY = (-51 * RYY - 19 * BYY ) / 100;
R = Y + RYY;
R = ( R < 0 ) ? 0 : (( R > 255 ) ? 255 : R );
G = Y + GYY;
G = ( G < 0 ) ? 0 : (( G > 255 ) ? 255 : G );
B = Y + BYY;
B = ( B < 0 ) ? 0 : (( B > 255 ) ? 255 : B );
pix[index] = 0xff000000 | (R << 16) | (G << 8 ) | B;
}
// output bitmap
Bitmap outBitmap = Bitmap.createBitmap(width, height, src.getConfig());
outBitmap.setPixels(pix, 0, width, 0, 0, width, height);
pix = null;
return outBitmap;
}
}
3. note that:
- i know that,it is a quite complicated algorithm, yet I also don’t really understand it clearly.I’ve found it at this link Working with images in Google Android
- With the help of this,applyTintEffect() method you can apply Tint Color Effect on image on click,on action_down etc.
4. conclusion:
- Some deep information about Tint Color effect .
- Know how to apply Tint Color Effect on image bitmap from drawables.
5. About the post:
- The code seems to explain itself due to comments, and is very easy to understand.
- I Think this Rocking algorithm,what do you think?
- Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
- Hope you enjoy it!
6. Source Code:
you can download the source code here
Cheers,
you can download the source code here
Hamad Ali Shaikh