android ImageView one finger rotation - android

I looked on many posts but couldnt find the answer.
I would like to rotate an root image with another small image that is located on the lower side of the root image.
while rotation its working but the first time I press it, it jumps to 45 degrees because of the math.tan(), I think I have a calculation problem.
rotateImage.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
final int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
rotateX = event.getRawX() - rotateImage.getWidth() / 2;
rotateY = event.getRawY() - rotateImage.getHeight() / 2;
break;
case MotionEvent.ACTION_MOVE:
float angle = (float) Math.toDegrees(Math.atan2(event.getRawY() - rotateY, event.getRawX() - rotateX));
if (angle < 0){
angle += 360;
}
mBinding.getRoot().setRotation(angle);
}
return true;
}
});
please advice.

I think this library can help you.
https://github.com/kencheung4/android-StickerView

Related

How to create a movable button which doesn't move out of the screen

I'm using this code to move a button in the screen but when I reach the sides it is moving out of the screens.
private float mPrezX, mPrevY;
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;
#Override
public boolean onTouch(View view, MotionEvent event) {
int action = event.getActionMasked();
Button gvup = (Button)findViewById(R.id.giveup);
gvup.setBackground(getResources().getDrawable(R.drawable.btn));
switch (action ) {
case MotionEvent.ACTION_DOWN: {
mPrevX = view.getX() - event.getRawX();
mPrevY = view.getY() - event.getRawY();
startClickTime = Calendar.getInstance().getTimeInMillis();//!!
gvup.setBackground(getResources().getDrawable(R.drawable.btn1));
break;
}
case MotionEvent.ACTION_MOVE:
{
view.animate()
.x(event.getRawX() + mPrevX)
.y(event.getRawY() + mPrevY)
.setDuration(0)
.start();
gvup.setBackground(getResources().getDrawable(R.drawable.btn1));
break;
}
case MotionEvent.ACTION_CANCEL:
gvup.setBackground(getResources().getDrawable(R.drawable.btn1));
break;
case MotionEvent.ACTION_UP:
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if(clickDuration < MAX_CLICK_DURATION) {
//click event has occurred
gvup.setBackground(getResources().getDrawable(R.drawable.btn));
giveUp();
}
break;
}
return true;
}
I'm also changing its background color if pressed and also made it clickable.
I need to keep that button inside the layout.
I have also tried this and this links but both of these approaches leading to squeeze the button when moved to the sides.
Please help.
Thanks in advance.
Well you need to get its X location and add its width to it to see where the "right" edge is and compare if it greater or equal to your viewport's width. I haven't written JAVA in a while but that should do the trick.
Are you checking its location?
Perhaps
int rightEdge = currentPos.X + itsWidth;
if(rightEdge >= screenWidth) // something here
Maybe in your animate() method do your checking. Its hard for me to guess withough looking at what is making this happen. What is firing the event? What is event.getRawX() equal to? Is that a touch point on the screen? Are you animating after the point is selected? Or you can define a min/max x and min/max y and not let your circle past that.
view.animate()
if( .x(event.getRawX() + mPrevX) > screenWidth ){
.x(screenwidth - circleWidth)
} // do the same for the Y - must check the max and min for both Y and X
.x(event.getRawX() + mPrevX)
.y(event.getRawY() + mPrevY)
.setDuration(0)
.start();

Android- Rotate image with single finger gesture

I want to know about How to rotate the bitmap image with single touch gestures.kindly help and suggest some solutions. I done scaling the bitmap with the help of
http://grishma102.blogspot.in/2013/10/drag-and-drop-functionality-to-move.html . Now I need to rotate the whole image while touch and rotate the resize button. How to acheive it?
Thanks in advance
Check out my blogspot in which i have tried to implement the functionality of stretch the image on arrow click and also delete it, and also you can move the image on the screen using gesture.
Drag-Drop image Also check out the Demo of DragDropImage
function that handles rotation with one finger, the main idea is to calculate the centerX and centerY of your view and taking into consideration the status bar height if you using one.
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (action) {
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
rotateX = event.getRawX();
rotateY = event.getRawY();
centerX = view.getX() + ((View) getParent()).getX() + (float) view.getWidth() / 2;
centerY = view.getY() + statusBarHeight + (float) view.getHeight() / 2;
break;
case MotionEvent.ACTION_MOVE:
newRotateX = event.getRawX();
newRotateY = event.getRawY();
double angle = Math.atan2(event.getRawY() - centerY, event.getRawX() - centerX) * 180 / Math.PI;
view.setRotation((float) angle - 45);
rotateX = newRotateX;
rotateY = newRotateY;
}
}
return true;
}
};

Multitouch - PointerIndex out of range

I try to fix an issue which appears in my code when I added multitouch functions in my app.
The problem seems to come from ACTION_POINTER_DOWN :
private float oldDist = 0;
backCard.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent me) {
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
firstX = (int) me.getX();
case MotionEvent.ACTION_POINTER_DOWN:
if(me.getPointerCount() >= 2){
oldDist = getSpacing(me);
System.out.println(oldDist);
}
break;
case MotionEvent.ACTION_MOVE:
float newDist = getSpacing(me);
if(newDist - oldDist > 200 && oldDist != 0){
System.out.println("Enabled");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
break;
}
return true;
}
private float getSpacing(MotionEvent me){
float difx = me.getX(0) - me.getX(1);
float dify = me.getY(0) - me.getY(1);
float spacing = (float) Math.sqrt(difx*difx + dify*dify);
return spacing;
}
});
When I use it without the getPointerCount() condition in ACTION_POINTER_DOWN, I have an out of range error. But if I use the condition, the log doesn't show anything I print in the code. (Of course I use 2 fingers ! :) ), so the condition is never true, even if several fingers touch the screen at the same time.
How can I fix that ? Thank you.
My device is a GS3.
Use me.getActionMasked() instead of me.getAction()

Android, help rotating image on touch

I am trying to rotate one of the transparent PNGs on this image. The number part is what I want to rotate. I am able to do this, but it is not what I am trying to achieve
I want to rotate the numbers like on a real combination lock. So the user will touch and move their finger in a circle. I looked at less precise image rotation on touch/move events, and they were not sufficient.
this is currently my code
public boolean onTouch(View v, MotionEvent event) {
double r=Math.atan2(event.getX()-lockNumbers.getWidth(), lockNumbers.getHeight()-event.getY());
int rotation=(int)Math.toDegrees(r);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
x=event.getX();
y=event.getY();
updateRotation(rotation);
break;
case MotionEvent.ACTION_UP:
break;
}//switch
return true;
}//onTouch
private void updateRotation(double rot){
float newRot=new Float(rot);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.numbers);
Matrix matrix=new Matrix();
matrix.postRotate(newRot,bitmap.getWidth(),bitmap.getHeight());
if(y>250){
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
lockNumbers.setImageBitmap(reDrawnBitmap);
}
else{
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
lockNumbers.setImageBitmap(reDrawnBitmap);
}
}
it also resizes the bitmap when you touch it because of the matrix parameter. This is not the desired effect.
The user will pretty much be required to go in a circle with their finger.
Write below code into your touch event.
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// reset the touched quadrants
for (int i = 0; i < quadrantTouched.length; i++) {
quadrantTouched[i] = false;
}
allowRotating = false;
startAngle = getAngle(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
double currentAngle = getAngle(event.getX(), event.getY());
rotateDialer((float) (startAngle - currentAngle));
startAngle = currentAngle;
break;
case MotionEvent.ACTION_UP:
allowRotating = true;
break;
}
// set the touched quadrant to true
quadrantTouched[getQuadrant(event.getX() - (dialerWidth / 2), dialerHeight - event.getY() - (dialerHeight / 2))] = true;
detector.onTouchEvent(event);
return true;
And use below link for more reference.
Rotate Dialer Example
By accepting #Dipak Keshariya's answer i am putting way to to get the angle rotation which can help you identify selected part of wheel.
private float copy[] = new float[9];
matrix.getValues(copy);
float wheelAngle = Math.round(Math.atan2(copy[Matrix.MSKEW_X],
copy[Matrix.MSCALE_X]) * (180 / Math.PI));
I presume you want to rotate an image at the point where a user touches the screen? If so, extend the SimpleOnGestureListener like this example:
public class MyGestureDetector extends SimpleOnGestureListener
{
#Override
public void onLongPress(MotionEvent event)
{
int X = (int)event.getX();
int Y = (int)event.getY();
...Rotate the image
}
}
Once you've got the screen coordinates of the touch event, you could apply a Rotation Animation about the point - see here for more details: http://developer.android.com/guide/topics/graphics/2d-graphics.html

Touch move gesture logic...how to determine direction of movement in 2d?

How can i determine the direction of gesture ? The use case is shown in the image link. what is the right logic for detecting in which direction the person is trying to move the ball in the circular path ? I have called the direction method in move gesture...Can someone help me fine tune this ... ?
http://www.shrenikvikam.com/wp-content/uploads/2011/04/214e422a43E11S3.png-150x134.png
private String getDirection(float firstTouchX, float finalTouchX){
if((firstTouchX - finalTouchX)>0)
return "Left";
else
return "Right";
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
// MotionEvent class constant signifying a finger-down event
case MotionEvent.ACTION_DOWN: {
Log.d("ACTION DOWN","Value ->");
final float x = event.getX();
final float y = event.getY();
initialTouchX = x;
initialTouchY = y;
break;
}
// MotionEvent class constant signifying a finger-drag event
case MotionEvent.ACTION_MOVE: {
final float x = event.getX();
final float y = event.getY();
final String direction = getDirection(initialTouchX,x);
Log.d("ACTION MOVE","diff in initial and cur value of x ->" + direction + (initialTouchX - x) + initialTouchX + "y->" + initialTouchY);
break;
}
// MotionEvent class constant signifying a finger-up event
case MotionEvent.ACTION_UP: {
Log.d("ACTION UP","Value ->");
break;
}
}
return true;
}
It seems like the slope of the line formed by the two touch points should be equal to the tangent of the circle at that point. This link has most of the math to pursue such a solution
I have used the difference between angles to the center point of the circle with good success. That may be the way to go as well.
If you are trying to determine which way to move the ball around the ciricle, it doesn't make sense for getDirection to return "Left" and "Right", it should be working with "Clockwise" and "Counterclockwise". Consider, for example, when the ball is at the 20 marker in your image: At this point every point on the circle is "Right" of where you are now...
In order to determine if the ball is moving clockwise or counterclockwise you need to consider both the x and y co-ordinates of the touch points, the x co-ordinate alone is not sufficient. You also need to know where the centre of the circle is. I would suggest in order to determine the direction of movement, you calculate the angle between the touch points and the centre of the circle.
prevTouchX = event.getHistoricalX(event.getHistorySize()-1);
currentTouchX = event.getX();
if(currentTouchX<prevTouchX){
Log.d("LEFT",event.getX()+" and "+event.getY());
}
if(currentTouchX>prevTouchX){
Log.d("RIGHT",event.getX()+" and "+event.getY());
}
Similarly for UP/DOWN
I use this code for rotating a view. It works very well. Have a look at this;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
firstX = event.getX();
firstY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
int dx =(int) (event.getX() - firstX);
int dy =(int) (event.getY() - firstY);
Log.d("Distance Rotate Touch",Integer.toString((int) (firstX-dx)));
if (signView.getRotation()<180){
if (firstX - dx > 15 && firstY - dy > 15 ){
View.setRotation(signView.getRotation()-5);
}else if(firstX - dx < -15 && firstY - dy < 15 ){
View.setRotation(signView.getRotation()+5);
}
}else {
if (firstX - dx > 10 && firstY - dy < -10 ){
View.setRotation(signView.getRotation()+5);
}else if(firstX-dx < -10 && firstY - dy < -15 ){
View.setRotation(signView.getRotation()-5);
}
}
break;
default: return true;
}

Categories

Resources