Home »
Android
How to change image view color using bitmap in Android?
In this article, we are going to learn about bitmap class and use it to change image view color in Android.
Submitted by Manu Jemini, on February 08, 2018
In the example below, we are going to use the Graphics.Bitmap class to change the color of an image through a bitmap.
To implement this in your program you will need to import in our java file:
android.graphics.Bitmap
android.graphics.Color
android.graphics.drawable.BitmapDrawable
Then create a layout file with a single ImageView and Button like:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:layout_centerHorizontal="true"
android:src="@drawable/includehelp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to change color"
android:onClick="change_color"
android:id="@+id/button"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true" />
In the button above we have put an onClick property and set it to change_color function which is in the Java File.
First, we will take the reference of the ImageView and Button from the layout file, then set a bitmap variable with a BitmapDrawable like this:
BitmapDrawable BD = (BitmapDrawable) I.getDrawable();
bitmap = BD.getBitmap();
Then when ever user clicks the button we set the second bitmap variable with the first variable and change the color of the ImageView like this: O = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig()).
This variable then goes on iterating through the bitmap and in the end, ImageView set to that bitmap variable.
1) Java file:
package com.example.hp.myapplication;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
Button B;
ImageView I;
Bitmap bitmap;
Bitmap O;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
B = (Button) findViewById(R.id.button);
I = (ImageView) findViewById(R.id.image);
BitmapDrawable BD = (BitmapDrawable) I.getDrawable();
bitmap = BD.getBitmap();
}
public void change_color(View view){
O = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());
for(int i=0; i<bitmap.getWidth(); i++){
for(int j=0; j<bitmap.getHeight(); j++){
int p = bitmap.getPixel(i, j);
int b = Color.blue(p);
int x = 0;
int y = 0;
b = b+150;
O.setPixel(i, j, Color.argb(Color.alpha(p), x, y, b));
}
}
I.setImageBitmap(O);
}
}
2) XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.hp.myapplication.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:layout_centerHorizontal="true"
android:src="@drawable/includehelp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to change color"
android:onClick="change_color"
android:id="@+id/button"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true" />
</RelativeLayout>
3) Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output