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;
}
Related
I have an OnTouchListener however when I actually test my program and touch the screen is displays a colour that is not what it should be. I tested using blocks of colours and it works however the coordinates are still off and if you touch too far on one colour it will show you a different one. I believe this is because the the coordinates produced are in comparison to the whole screen instead of the image view - where i want it to be selected from. How would i make the OnTouchListener be for just the image view and not the whole screen? Thank you.
final Bitmap bitmap2 = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Matrix inverse = new Matrix();
v.getMatrix().invert(inverse);
float[] touchPoint = new float[] {event.getX(), event.getY()};
inverse.mapPoints(touchPoint);
int x = Integer.valueOf((int)touchPoint[0]);
int y = Integer.valueOf((int)touchPoint[1]);
// int x = (int) event.getX();
// int y = (int) event.getY();
int pixel = bitmap2.getPixel(x, y);
int red = Color.red(pixel);
int blue = Color.blue(pixel);
int green = Color.green(pixel);
Bitmap bitmap = BitmapFactory.decodeFile(output.getAbsolutePath());
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.rgb(red,green,blue));
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawRect(50, 50, 500, 250, paint);
ImageView imageView = (ImageView)findViewById(R.id.Image_view);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
return true;
}
});
}
}
I believe this is because the the coordinates produced are in
comparison to the whole screen
Yes, you need to do some simple deduction to get the touch coordinates on the imageView:
int[] imgCoords = new int[2];
imageView.getLocationOnScreen(imgCoords);
get the X,Y of the touch and take away the imageView coordinates from it:
int wholeX = (int) event.getX();
int wholeY = (int) event.getY();
int imageX = wholeX - imgCoords[0];
int imageY = wholeY - imgCoords[1];
So I have an ImageView where I set png as a background. Let's assume the png picture is a circle, so it doesn't take all the space of the ImageView.
I need to catch the event of clicking only the content of the ImageView (the circle) and not the remaining empty area.
Is this actually possible at all or with any other Android control?
Edit:
I still can't get this to work.
So to be more precise I have this picture of an egg which I set as ImageView src
And I want to be able to click only on it. Foe example if I click slightly outside the egg I want to know that and prevent code inside the ImageView click event.
I tried this code but I don't have any idea how this helps me:
ImageView imageView = (ImageView)findViewById(R.id.imageview);
Drawable drawable = imageView.getDrawable();
Rect imageBounds = drawable.getBounds();
//original height and width of the bitmap
int intrinsicHeight = drawable.getIntrinsicHeight();
int intrinsicWidth = drawable.getIntrinsicWidth();
//height and width of the visible (scaled) image
int scaledHeight = imageBounds.height();
int scaledWidth = imageBounds.width();
//Find the ratio of the original image to the scaled image
//Should normally be equal unless a disproportionate scaling
//(e.g. fitXY) is used.
float heightRatio = intrinsicHeight / scaledHeight;
float widthRatio = intrinsicWidth / scaledWidth;
//do whatever magic to get your touch point
//MotionEvent event;
//get the distance from the left and top of the image bounds
int scaledImageOffsetX = event.getX() - imageBounds.left;
int scaledImageOffsetY = event.getY() - imageBounds.top;
//scale these distances according to the ratio of your scaling
//For example, if the original image is 1.5x the size of the scaled
//image, and your offset is (10, 20), your original image offset
//values should be (15, 30).
int originalImageOffsetX = scaledImageOffsetX * widthRatio;
int originalImageOffsetY = scaledImageOffsetY * heightRatio;
here is how you would get the click event:
int[] viewCoords = new int[2];
imageView.getLocationOnScreen(viewCoords);
//From this and the touch coordinates you can calculate the point inside the ImageView:
int touchX = (int) event.getX();
int touchY = (int) event.getY();
int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate
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;
}
I need to drag an image like in a videogame. I have a few lines drawed and I need to drag it and see the lines drawed before "out" of the window. I give you two images trying to help to understand it.
Image before drag:
Then I touch the screen with my finger and I make a move to the top of the screen, so this three lines have to be upper than the initial image, like this:
I don't know how to do this "drag efect", can someone help me? This is not an image as a .jpg file, is three lines drawed with "drawLine" on a canvas into an imageView.
My onTouchEvent Code and my drawRectangle code:
public boolean onTouch(View view, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_UP:
{
x = event.getX();
y = event.getY();
getFirstFoot();
return true;
}
case MotionEvent.ACTION_MOVE:
{
ammountX = event.getX() - startX;
ammountY = event.getY() - startY;
canvas.save();
canvas.translate(ammountX, ammountY);
//canvas.restore();
imageView.invalidate();
return true;
}
case MotionEvent.ACTION_DOWN:
{
startX = event.getX();
startY = event.getY();
}
}
return true;
}
drawRectagle code:
private void drawRectangle(boolean erase)
{
buttonRed.setVisibility(View.VISIBLE);
buttonGree.setVisibility(View.VISIBLE);
buttonYellow.setVisibility(View.VISIBLE);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.ascensorsininfo);
canvas.drawBitmap(bitmap2, x - (bitmap2.getWidth() / 2), y - bitmap2.getHeight(), new Paint());
drawedPoints.add(String.valueOf(x - (bitmap2.getWidth() / 2)));
drawedPoints.add(String.valueOf(y - bitmap2.getHeight()));
imageView.invalidate();
y -= bitmap2.getHeight();
}
Any solution would be apreciated. Thanks!
Check out Canvas.translate
It will "move" everything you draw afterwards the ammount you say in x and y axis.
Don't forget to save and restore it, like:
canvas.save();
canvas.translate(ammountX, ammountY);
// [draw your stuff here]
canvas.restore();
If your question is more towards how to detect the dragging effect, you have to Override your view's onTouchEvent method. Something like:
#Override
public boolean onTouchEvent (MotionEvent event){
if(event.getActionMasked() == MotionEvent.DOWN){
startX = event.getX();
startY = event.getY();
return true;
}else if(event.getActionMasked() == MotionEvent.MOVE){
ammountX = event.getX() - startX;
ammountY = event.getY() - startY;
return true;
}
return super.onTouchEvent(event);
}
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.