I have a set of imageButtons placed within a relative layout, and each imageButton has a shape within it that is visible while the rest of it is set to alpha. I have currently set these buttons to slightly overlap, and I am trying to code it so that when the alpha part of one button is pressed, it ignores that button and checks the button underneath it.
I am currently using onTouch() with an OnTouchListener to get the x and y coordinates of the touch on the screen, but that is calculated based on the whole screen from what I can tell. Is there a way to use the position found from event.getX() and event.getY() to look at where the button is on the screen and see if that spot clicked on the button is transparent or not?
Use View.getLocationOnScreen() and/or getLocationInWindow().
https://stackoverflow.com/a/2226048/1979882
In order to check if alpha-channel exists, I would use:
public static Bitmap loadBitmapFromView(View v) {
Bitmap bitmap;
v.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return bitmap;
}
and than detect the ARGB value to a particular pixel.
int pixel = bitmap.getPixel(x,y);
Now you can get each channel with:
int alphaValue = Color.alpha(pixel);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
https://stackoverflow.com/a/31775271/1979882
Related
I am making an android application in which I want the user to tap on an image and a blob shows up and stores these points in the database. Very similar to the below image
from another app. Any ideas about what can be done? Is it overlaying on another picture or what?
you can identify the color from the image and check with color if you click on image that time specific color code pick and checked with predefine color, if match then you perform you operation.
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y); // x and y are coordinates
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
if(pixel == Color.RED){
// color is red
}
I have a code that can only give hex code on touch pixel on image.
Now I want to give the name of the color as well.
How is this possible using Android?? Please suggest a way.
public boolean onTouch(View v, MotionEvent event) {
// get x coordinates
int x = (int) event.getX();
// get y coordinates
int y = (int) event.getY();
// get bitmap in touch listener
final Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
//xand y coordinates stored in to pixel
int pixel = bitmap.getPixel(x, y);
//RGB values
redValue = Color.red(pixel);
blueValue = Color.blue(pixel);
greenValue = Color.green(pixel);
selected_colour.setText(""+redValue+""+blueValue+""+greenValue);
selected_colour.setText("touched color:" + "#" + Integer.toHexString(redValue) + Integer.toHexString(greenValue) + Integer.toHexString(blueValue));
selected_colour.setTextColor(pixel);
return false;
}
});
What you're asking for is a bit tricky, because although there are many colours whose names are widely agreed upon (for instance, FFFFFF is called "White" everywhere), most other colour names are not a global convention but rather developed by the specific people who are naming the colours.
That being said, there are several tools out there which can do this. Check out this link, it's a website which you can provide a colour in hex code, and it will name it according to the closest pre-defined list of colour names.
You can view the javascript code and adapt it to Android. It's a pretty straight-forward algorithm which measures the distance of the colour you gave to the closest hit in a predefined colour list, by measuring the distance in RGB and HSL.
I want to change the HSV of bitmap I found out some help online but they are changing the HSV of whole image. In my case i have circular selector that enable user to select specific part of image and than change its HSV. It look like this
Can we do that without using any other third party image processing library?
If you know how to specifically get the pixel indices of the circle here is what you do:
int pixel = bitmap.getPixel(X,Y);
float[] HSV=new float[3];
Color.RGBToHSV(Color.red(pixel), Color.green(pixel), Color.blue(pixel), HSV);
// Manipulate the HSV array as you want then,
bitmap.setPixel(X,Y, Color.HSVToColor(HSV));
PS: If you want to know how to get the X and Y coordinates of pixel please do comment. I'll edit the post as per your requirements.
EDIT :
Here is how you get pixels of the circular area (and manipulate it) provided you have the co-ordinate of the center of the circle and the radius of the circle you want the manipulation to.
int centerX = 100; // This is the X co-ordinate of the center of your circle
int centerY = 100; // This is the Y co-ordinate of the center of your circle
int radius = 40; // This is the radius of the circle you want
for(int Y=centerY-radius; Y<=centerY+radius;Y++)
{
for(int X=centerX-radius;X<=centerX+radius;X++)
{
int distance = (int)Math.sqrt(Math.pow((X-centerX),2) + Math.pow((Y-centerY),2));
if(distance<=radius)
{
int pixel = bitmap.getPixel(X,Y);
float[] HSV=new float[3];
Color.RGBToHSV(Color.red(pixel), Color.green(pixel), Color.blue(pixel), HSV);
// Manipulate the HSV array as you want then,
bitmap.setPixel(X,Y, Color.HSVToColor(Color.alpha(pixel),HSV));
}
}
}
you can use Bitmap.setPixel(int, int, int); , to change the color at position(x, y).
or
you can create a canvas use from a bitmap use
Canvas canvas = new Canvas(bitmap);
and you can add new content on the original bitmap.
I am selecting some area of image in displayed imageview. Then i do some work on selected pixels of that bitmap which is displayed in imageview. But i am not able to hit those pixel which user selected. I know i have to do some pixel mapping of displayed image and actual bitmap. See images for better understanding of my problem.
User select some pixel using circular selector
After processing affected pixels are different not those which user selected
I have tried some thing like this to get accurate pixels
Bitmap image = EyeColorChangerActivity.getImageBitmap();
int displayedwidht = image.getWidth();
int displayedheight = image.getHeight();
x = x*((float)displayedwidht/mScreenWidth);
y = y*((float)displayedheight/mScreenHeight);
Set OnTouchListener on your ImageView.
Inside onTouch method of OnTouchListener use the following code to get the selected bitmap pixel.
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
int x = (int) arg1.getX();
int y = (int)arg1.getY();
int pixel = mBitmap.getPixel(x, y);
// process new color for selected pixel.
....
//set new color to bitmap pixel
return false;
}
I want to remove the white background color in a bitmap like in this example image.
This is my first try:
BitmapDrawable drawableImg = getImage();
drawableImg.setColorFilter(new PorterDuffColorFilter(Color.WHITE,
PorterDuff.Mode.DST_OUT));
Second try:
To remove the white colors range by setting fromBgColor and toBgColor, But when I replace the color with android transparent color the image turns into black here is the code:
public static Bitmap getBitmapWithTransparentBG(Bitmap srcBitmap,
int fromBgColor, int toBgColor) {
Bitmap result = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);
int nWidth = result.getWidth();
int nHeight = result.getHeight();
for (int y = 0; y < nHeight; ++y)
for (int x = 0; x < nWidth; ++x) {
int nPixelColor = result.getPixel(x, y);
if (nPixelColor >= fromBgColor && nPixelColor <= toBgColor)
result.setPixel(x, y, Color.TRANSPARENT);
}
return result;
}
Hint I had a hint that bitmap does not support transparent bits and I should use PNG or Gif instead of bitmaps is that right?
Hint 2 It turned out that bitmap in Android has an option to show transparency since API level 1 using Bitmap.Config enum. Check this link in the documentation of Android.
what is beneath your image? Maybe it could be a layout issue. Have you tried setting android:background="#android:color/transparent" in the xml?
I see you tried the getBitmapWithTransparentBG described in
Android: Make specific (green) color in background image transparent
But have you tried also this?
How to change colors of a Drawable in Android?
About the image being jpg or png, I've managed to work with both after loading and setting it to a imageview (getting the bitmap back from it), and it allows to set the transparency using the getBitmapWithTransparentBG function as quoted above.