How do I make the following type on Menu? I made it but the buttons dont touch the sides and
they leave out the background..
Help?
What I would suggest is that you make an ImageView and set the background image as the one you have shown. Then handle the onTouch event on it according to position of touch. So by trial and error get the bounds you want for all three regions. You can get the position of click using:
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = event.getX();
int y = event.getY();
return true;
}
So use the x,y coordinates you get using this code and the bounds for the 3 parts of the image you get by trial and error. Compare them and accordingly execute your code.
Hope it helps! Cheers.
Related
I am a beginner developer. I would like to build an app, but I am facing a problem.
As the image shows, I have a button. If the user clicks on the top side of this button I would like to change the color of just the top line. Also on the other sides, just the side that the user clicked. If user clicked for example on the top side then the right side, I would like to change the color of both side which is shown by the picture.
Any ideas how to do this? The main problem for me, is I can not detect which area the user clicks.
You can use touch detection like described here:
https://developer.android.com/training/gestures/detector.html
#Override
public boolean onSingleTapConfirmed(MotionEvent event) {
float clickedX = event.getX();
float clickedY = event.getY();
float btnTop = obtainTop(yourBtn);
float btnBot = obtainBot(yourBtn);
float btnLeft = obtainLeft(yourBtn);
float btnRight = obtainRight(yourBtn);
// detect where the button was clicked
// change button how you need
return true;
}
For obtain x y coordinates of View you can find something here
Getting View's coordinates relative to the root layout
How to place a picture at some user input specific pixels on the screen in android. if a user taps at a certain point than an image should be pasted on that point. my main concern is not getting the pixels but putting an image within that set of pixels (I am having a set of pixels within that i have to re-scale the image and than place it at that position )
as told by Ran hassid first find the touch coordinates on screen by using
view.setOnTouchListener(new View.OnTouchListener() {
#Override public boolean onTouch(View view, MotionEvent motionEvent) {
motionEvent.getX()
motionEvent.getY()
return false; } });
and then follow this answer to show image at obtained cordinates by above code-
link
What I need is to be able to know which regions on a image is being pressed, I have a piece of image taken from google earth, and there are some buildings that are relevant, the way that i am using right now is to place images with low alpha so they are transparent and placing them on top of the buildings, and implementing onTouchListener on them, but the problem with this is that only works on certain android devices, for example on an Samsung galaxy 4 they are on wrong places.
I need to be able to detect this buildings that are being pressed regardless of the screen resolution.
Thanks.
You need to use the touch events on the image to detect where the press happens:
this.view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event)
{
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
}
}
You will need to scale the X and Y coordinates by the size of the view to get the position touched within the original image.
How can I create a custom button like this?
It should be just clickable area not a real button.
I use a crapload of irregular shaped buttons on my app, and to change the "hot zone" or "clickable area" of the button, I just use the Bitmap.getPixel() method to check for alpha on the image used. If the method returns 0, then don't perform click event.
Example:
(1) Create your button as usual, whichever way you would like.
(2) Define a Bitmap and assign to it the same image drawable used for the button.
(3) Get the X and Y coordinates of the touch or click action.
(4) Pass the coordinates to the .getPixel(x,y) method.
Sample Code:
// ** Declare your Bitmap somewhere **
final Bitmap TheBitmap;
TheBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.TheImage);
// ** My onTouch code **
public boolean onTouch(View v, MotionEvent event) {
int eventPadTouch = event.getAction();
switch (eventPadTouch) {
case MotionEvent.ACTION_DOWN:
if (iX>=0 & iY>=0 & iX<TheBitmap.getWidth() & iY<TheBitmap.getHeight()) { // ** Makes sure that X and Y are not less than 0, and no more than the height and width of the image.
if (TheBitmap.getPixel(iX,iY)!=0) {
// * A non-alpha area was clicked, do something
}
}
return true;
}
return false;
}
The event.getX() and event.getY() simply give you the coordinates of where you touched the button.
** The above sample is to guide you in the correct direction. There are some checks to add to the code to assure no errors occur.
ceate using your own canvas or make a image using photoshop like this and then using Bitmap.createScaledBitmap scale it according to dimension of your button and hence you will get this button.
using canvas you have to write more code just do this it will work fine
Simply just create an image like that and you can use either ImageView or Button w/o text, and implement an OnClickListener. It just works!
save that as a png and and put it in your drawables folder. Then in your xml use something like this
<Button
android:height="wrap_content"
android:width="wrap_content"
android:background="#drawable/your_png"
/>
I am not 100% positive that the corner cut out is going to work properly. That corner area that is gone may end up being clickable. If that is the case and if you don't want it to be then you'll have to slice your picture in half somewhere create two buttons that you can set next to each other to make up that shape and use the same click listener for both. From the users perspective it will still seem like one button.
Speaking of events we know that the touch screen method onTouchEvent (MotionEvent events) allows you to capture touch events on the screen and event.getX () and
event.getY () give me the coordinates of the touch as float values (ie values with a comma).
Specifically, I realized that taking logcat using the fixed point the finger in a mobile phone screen and not moving it, the event is not only perceived but MotionEvent.ACTION_DOWN MotionEvent.ACTION_MOVE and returned coordinates are manifold. This i can understand because the surface of the finger touches more points and more by reading the device coordinates believe that the finger moves while holding it.
My problem is that I would read the color of one pixel of a color image when I hold your finger still. In particular I need the central area occupied by the finger.
First, assuming you have a Bitmap object, I have to round to integer coordinates of the touch as the getPixel (intX, int y) coordinates of the entire Bitmap wants to return the pixel color.
Then how do I get the central point of touch? Be that there is a function that can help me? Keep in mind that while I stopped and read the value of a pixel on the image then I start to move slowly and even though I always record every move the center pixel. I need this because every move, if you change the color of the image in pixels, I want to vibrate or not the device.
If I could stop thinking about themselves in a dynamic array to store the coordinates of different pixels and search for the center of gravity, but moving I do not know what to do. I think however, that the research center of gravity, if not done very efficiently, can be slow during the move and then give the wrong results. If the CG is not quite enough for me to another point belonging to the environment.
I hope you can help me maybe with a few lines of code sketched.
Thanks in advance.
To get the touch position of finger, just cast the getX() and getY() to int and perform your desired tasks.
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
getPixelAt((int)event.getX(), (int)event.getY());
}
}
you can put you on main activity class when you can want to touch event occurs. let see you can put following code or not ? and return is most important as well.
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mDistanceX = 0;
}
return super.onTouchEvent(event);
}