How to detect area click into Image in Android - java

I have a problem with detect area click into an image.
I have the images below:
https://i.stack.imgur.com/fZmq0.jpg
https://i.stack.imgur.com/sb6Va.jpg
https://i.stack.imgur.com/YMwpy.jpg
How can I detect when touching into the yellow area?

It can be easy. You don't need to slice it.
The thing you have to do is
Click event.
Get the position where you clicked.
Get the colour of the position.
Check if the colour is yellow or not.
Do what you want when it is yellow.
It seems like the yellow is not just one yellow colour. So, What you can do is have a range of the yellow colour and compare all the possible yellow colours.
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
float x = event.getX();
float y = event.getY();
if(getColourOfThePixel(x, y)){
// DO SOMETHING
}
}
return super.onTouchEvent(event);
}
public boolean getColourOfThePixel(float x, float y){
int colorXY = mBitmap.getPixel(rangeX, rangeY);
int r = Color.red(colorXY);
int g = Color.green(colorXY);
int b = Color.blue(colorXY);
if (isYellow(r, g, b)) {
return true;
}
return false;
}
public boolean isYellow(int r, int g, int b){
// CHECK THE RANGE OF YELLOW COLOUR
}
To make it sure, you'd better have +-x, y ranges(like a square, maybe +-20pixel) because checking only one pixel may not work sometimes. So, if the square has a yellow colour, the event you wanna implement would work out.
I hope this is helpful.

Related

Processing - Zoom on clicked area then click again to zoom back out

I have a picture then used a flashlight type of light to only show where the mouse is hovering over. That part of the code works, but now I want to use if/else statements to zoom in on the selected area and then click again to zoom back out. Any other way to zoom in on specific area then back out of that area also helps. Really any help will be appreciated!
PImage ispy;
void setup () {
size(1024,768);
ispy = loadImage("ispy2.jpeg");
}
void draw () {
loadPixels();
ispy.loadPixels();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int loc = x+y*width;
float r = red(ispy.pixels[loc]);
float g = green(ispy.pixels[loc]);
float b = blue(ispy.pixels[loc]);
float d = dist(mouseX, mouseY, x, y); //
float factor = map(d, 0, 200, 2, 0);
pixels[loc] = color(r*factor, g*factor, b*factor);
}
}
updatePixels();
}
Here is my interpretation of what you are talking about. We store a isClicked boolean to store the state of whether we should zoom in or not. When we are going to draw the image, we translate() to the mouse, then we scale(), then we translate() back the same amount that we moved before, but in the opposite direction. What this does is it does the scale transform around the mouse position.
One thing that I couldn't find a way around way your way of updating the pixels directly from the image and the flashlight effect. What the program is doing instead is using your method to make a mask image and applying that to a PGraphics object. Another thing that I noticed is that when just rendering straight to the screen, there is considerable lag from the scaling. Instead, I have moved the drawing to a PGraphics object. This improves the performance.
In the end, to render, the program is drawing everything on the PGraphics object, then applying the mask to that object to get the flashlight effect.
Here is the code that I have:
PImage ispy, distMask;
boolean isClicked = false;
PGraphics renderer;
void createDistanceMask(PImage distMask){ //Takes image and changes its pixels to "alpha" for the PGraphics renderer
distMask.loadPixels();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int loc = x+(height-y-1)*width;
float d = dist(mouseX, mouseY, x, y); //
int factor = int(map(d, 0, 200, 400, 0)); //Pixel data will be between 0 and 255, must scale down later.
if (factor > 255)
factor = 255;
distMask.pixels[loc] = color(factor,factor,factor);
}
}
distMask.updatePixels();
}
void setup () {
size(1024,768, P2D);
ispy = loadImage("ispy2.jpeg");
distMask = new PImage(width,height);
renderer = createGraphics(width,height,P2D);
mouseX = width/2; //Not necessary, but will have black screen until mouse is moved
mouseY = height/2;
}
void draw () {
background(0);
pushMatrix();
createDistanceMask(distMask);
renderer.beginDraw(); //Starts processing stuff into PGraphics object
renderer.background(0);
if(isClicked){ //This is to get the zoom effect
renderer.translate(mouseX, mouseY);
renderer.scale(2);
renderer.translate(-mouseX, -mouseY);
}
renderer.image(ispy,0,0); //Render Image
renderer.endDraw();
renderer.mask(distMask); //Apply Distance mask for flashlight effect
image(renderer,0,0); //Draw renderer result to screen
popMatrix();
}
void mouseClicked(){
isClicked = !isClicked;
}
In my comment, I asked about having the screen move to the mouse, which is what this is doing. If you want to "freeze" the screen in one position, what you can do is store a lastMouseClickPosition PVector or simply just ints. Then, when translating, translate to the position instead of the PVector.
Here's the code that would change:
PVector lastClickPos = new PVector(); //Make the position
if(isClicked){ //When Rendering
renderer.translate(lastClickPos.x, lastClickPos.y);
renderer.scale(scalingFactor);
renderer.translate(-lastClickPos.x, -lastClickPos.y);
}
void mouseClicked(){ //At the bottom
isClicked = !isClicked;
lastClickPos.set(mouseX, mouseY);
}

Orthographic Camera screen on different sized phones

My Orthographic camera is initialized to be size 540x960 (the size of my HTC One screen). However, when I try it on another phone with a larger screen, whenever I tap to move my Texture, there is an offset from where I touch and where the Texture moves to. Should I use a different sizing technique? Such as size it by Gdx.graphics.getWidth() and .getHeight()?
From comment below:
#Override
public boolean touchDown(float x, float y, int pointer, int button) {
xpos = x;
return false;
}
You are only getting the screen's x position. To convert it to world space (the coordinate system of your game), you need to put it into a Vector3 and unproject it from the camera like this:
#Override
public boolean touchDown(float x, float y, int pointer, int button) {
Vector3 touchPoint = new Vector3(x, y, 0); //0 is arbitrary since this is in 2D
camera.unproject(touchPoint); //now touchPoint contains the world position of the touch
xpos = touchPoint.x;
ypos = touchPoint.y; //if you need it.
return false;
}

Android Getting Co ordinates

A little problem for you but Break point for me during the development of an application.
I have a background image of Layout in which I have a Zip(Cloth zip) in the middle and at the top of image.
I want to do the following some things:
get coordinates of center-point(that is the starting point of Touch) of that image where that zip exists.
On moving the finger down, getting the changed coordinates.
X coordinate should be constant after getting the mid point, means the user can only drag on the middle of the screen vertically.
So badly waiting for an expert response.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
int center1 = (width/2) - (aspected touchable area you need);
int center2 = (width/2) + (aspected touchable area you need);
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Logic for Zip motion
case MotionEvent.ACTION_UP:
//Logic for Zip motion
}
return false;
}

Displaying image in specific position OnClick Android

I have a row of 7 circles and 6 columns I want to display an image depending on the circle that is clicked using Onclick
int rows, cols;
rows = 7;
cols = 6;
for (int i=0; i <rows; i++) {
for (int j=0; j< cols; j++) {
canvas.drawCircle(90 + (100*i), 155 + (115*j), 40, white);
}
}
Using
.OnClickListener
how is that possible?
If you override onTouchEven you can get the x and y of the touch.
#Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return true
}
Now if you use the drawCircle function using the x and y valorized in the onTouchEvent you will draw it exactly where you touched.
Override onTouchEvent.
Use instance variables float x,y
#Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
invalidate(); // to refresh draw
return true;
}
Use x and y to draw the image.
public boolean onTouchEvent (MotionEvent event)
Added in API level 1
Implement this method to handle touch screen motion events.
If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick(). This will ensure consistent system behavior, including:
obeying click sound preferences
dispatching OnClickListener calls
handling ACTION_CLICK when accessibility features are enabled
Parameters
event The motion event.
Returns
True if the event was handled, false otherwise.
You need to know the center and the radius of the circle to detect touch withing the circle.
My be this helps you understand
Creating a spray effect on touch draw in android

Touch within view only- android

I am trying to do this but it gave me headache
I have a view that draw a color bar. And I want to allow user to move it around (drag and drop)
this is on draw method:
int r,g,b;
for(int i = 0; i < 64; i++)
{
r = Math.round((float)colorBuffer[i][0]*MAX_SCALING);
g = Math.round((float)colorBuffer[i][1]*MAX_SCALING);
b = Math.round((float)colorBuffer[i][2]*MAX_SCALING);
paint.setColor(Color.argb(MAX_SCALING, r, g, b));
canvas.drawLine(x1, y1, x2, y1, paint);
y1+=3;
}
rgb is just the color.
this is my ontouch implementation.
public boolean onTouchEvent(MotionEvent e)
{
float x = e.getX();
float y = e.getY();
switch (e.getAction())
{
case MotionEvent.ACTION_DOWN:
{
Toast t = Toast.makeText(getContext(), "Touch", Toast.LENGTH_SHORT);
t.show();
break;
}
case MotionEvent.ACTION_MOVE:
{
x1=(int) x;
x2=(int) (x+30);
y1=(int) y;
break;
}
case MotionEvent.ACTION_UP:
{
Toast t = Toast.makeText(getContext(), "Release", Toast.LENGTH_SHORT);
t.show();
x1=(int) x;
x2=(int) (x+30);
y1=(int) y;
break;
}
}
invalidate();
return true;
}
this works perfectly fine, but the problem is that it works even outside the view.
so if i touch outsite the view my bar move over there too
even when i click a button my bar try to move too
I want to drag and drop the component only if the user touch within the view
any help would be appreciate
thanks
On touch get the x, y, width, and height of your view. Then every time when you are moving the bar, as I understand in MotionEvent.ACTION_MOVE, check if current coordinates are whithin the views's bounds, if this is true, then allow to move the bar.

Categories