Android- How to get Rgb Color programmaticall.
Get the pixel in
height And width when touch on imageView.
activity_main.xml
...................................................................................................................................................................
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="wrap_content"
android:text="Android Beginner tutorial point"
tools:context=".MainActivity"
android:textSize="40dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="touched color: "/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/multicolor"
android:layout_marginTop="40dp"/>
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivit extends Activity {
TextView touchedXY, invertedXY, imgSize, colorRGB;
ImageView imgSource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_code_activity);
touchedXY = (TextView)findViewById(R.id.TutorialBlog);
colorRGB = (TextView)findViewById(R.id.colorAvarage);
imgSource = (ImageView)findViewById(R.id.image);
imgSource.setOnTouchListener(imgOnTouchListener);
}
OnTouchListener imgOnTouchListener = new OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
float[] eventXY = new float[] {eventX, eventY};
Matrix invertMatrix = new Matrix();
((ImageView)view).getImageMatrix().invert(invertMatrix);
invertMatrix.mapPoints(eventXY);
int x = Integer.valueOf((int)eventXY[0]);
int y = Integer.valueOf((int)eventXY[1]);
Drawable imgDrawable = ((ImageView)view).getDrawable();
Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap();
//Limit x, y range within bitmap
if(x < 0){
x = 0;
}else if(x > bitmap.getWidth()-1){
x = bitmap.getWidth()-1;
}
if(y < 0){
y = 0;
}else if(y > bitmap.getHeight()-1){
y = bitmap.getHeight()-1;
}
int touchedRGB = bitmap.getPixel(x, y);
colorRGB.setText("Color Code In Hexa-------> " + "#" + Integer.toHexString(touchedRGB));
colorRGB.setTextColor(touchedRGB);
touchedXY.setTextColor(touchedRGB);
return true;
}};
}
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivit extends Activity {
TextView touchedXY, invertedXY, imgSize, colorRGB;
ImageView imgSource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_code_activity);
touchedXY = (TextView)findViewById(R.id.TutorialBlog);
colorRGB = (TextView)findViewById(R.id.colorAvarage);
imgSource = (ImageView)findViewById(R.id.image);
imgSource.setOnTouchListener(imgOnTouchListener);
}
OnTouchListener imgOnTouchListener = new OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
float[] eventXY = new float[] {eventX, eventY};
Matrix invertMatrix = new Matrix();
((ImageView)view).getImageMatrix().invert(invertMatrix);
invertMatrix.mapPoints(eventXY);
int x = Integer.valueOf((int)eventXY[0]);
int y = Integer.valueOf((int)eventXY[1]);
Drawable imgDrawable = ((ImageView)view).getDrawable();
Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap();
//Limit x, y range within bitmap
if(x < 0){
x = 0;
}else if(x > bitmap.getWidth()-1){
x = bitmap.getWidth()-1;
}
if(y < 0){
y = 0;
}else if(y > bitmap.getHeight()-1){
y = bitmap.getHeight()-1;
}
int touchedRGB = bitmap.getPixel(x, y);
colorRGB.setText("Color Code In Hexa-------> " + "#" + Integer.toHexString(touchedRGB));
colorRGB.setTextColor(touchedRGB);
touchedXY.setTextColor(touchedRGB);
return true;
}};
}
...................................................................................................................................................................
0 comments