Tuesday, 15 February 2011

image - Android rotate dialer ontouch -



image - Android rotate dialer ontouch -

i reading tutorial rotating image(in case wheel)

i need angle everytime touch wheel 1 time again starts @ 0, has start @ lastly angle.

can explain & show me how that?

here code:

public class mainactivity extends activity { private static bitmap imageoriginal, imagescaled; private static matrix matrix; private imageview dialer; private int dialerheight, dialerwidth; private gesturedetector detector; private boolean[] quadranttouched; private boolean allowrotating; private double startangle, currentangle, current_angle; motionevent e; boolean isclicked = false; private sensormanager msensormanager; private shakeeventlistener msensorlistener; @override public void oncreate(bundle b) { super.oncreate(b); setcontentview(r.layout.activity_soundboard); this.setrequestedorientation(activityinfo.screen_orientation_portrait); /* * dit regelt het roteren van het wiel. */ if (imageoriginal == null) { imageoriginal = bitmapfactory.decoderesource(getresources(), r.drawable.wheelcircle); } // initialize matrix 1 time if (matrix == null) { matrix = new matrix(); } else { // not needed, can post matrix restore old state matrix.reset(); } detector = new gesturedetector(this, new mygesturedetector()); quadranttouched = new boolean[] { false, false, false, false, false }; allowrotating = true; dialer = (imageview) findviewbyid(r.id.imageview); dialer.setontouchlistener(new myontouchlistener()); dialer.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { // method called more once, values need initialized 1 time if (dialerheight == 0 || dialerwidth == 0) { dialerheight = dialer.getheight(); dialerwidth = dialer.getwidth(); // resize matrix resize = new matrix(); resize.postscale((float)math.min(dialerwidth, dialerheight) / (float)imageoriginal.getwidth(), (float)math.min(dialerwidth, dialerheight) / (float)imageoriginal.getheight()); imagescaled = bitmap.createbitmap(imageoriginal, 0, 0, imageoriginal.getwidth(), imageoriginal.getheight(), resize, false); // translate image view's center float translatex = dialerwidth / 2 - imagescaled.getwidth() / 2; float translatey = dialerheight / 2 - imagescaled.getheight() / 2; matrix.posttranslate(translatex, translatey); dialer.setimagebitmap(imagescaled); dialer.setimagematrix(matrix); } } }); } private class myontouchlistener implements view.ontouchlistener { @override public boolean ontouch(view v, motionevent event) { switch (event.getaction()) { case motionevent.action_down: (int = 0; < quadranttouched.length; i++) { quadranttouched[i] = false; } allowrotating = false; startangle = getangle(event.getx(), event.gety()); break; case motionevent.action_move: currentangle = getangle(event.getx(), event.gety()); rotatedialer((float) (startangle - currentangle)); startangle = currentangle; break; case motionevent.action_up: break; } quadranttouched[getquadrant(event.getx() - (dialerwidth / 2), dialerheight - event.gety() - (dialerheight / 2))] = true; detector.ontouchevent(event); log.e("angle", currentangle + ""); homecoming true; } } private class mygesturedetector extends simpleongesturelistener { @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { // quadrant of start , end of fling int q1 = getquadrant(e1.getx() - (dialerwidth / 2), dialerheight - e1.gety() - (dialerheight / 2)); int q2 = getquadrant(e2.getx() - (dialerwidth / 2), dialerheight - e2.gety() - (dialerheight / 2)); // inversed rotations if ((q1 == 2 && q2 == 2 && math.abs(velocityx) < math.abs(velocityy)) || (q1 == 3 && q2 == 3) || (q1 == 1 && q2 == 3) || (q1 == 4 && q2 == 4 && math.abs(velocityx) > math.abs(velocityy)) || ((q1 == 2 && q2 == 3) || (q1 == 3 && q2 == 2)) || ((q1 == 3 && q2 == 4) || (q1 == 4 && q2 == 3)) || (q1 == 2 && q2 == 4 && quadranttouched[3]) || (q1 == 4 && q2 == 2 && quadranttouched[3])) { dialer.post(new flingrunnable(-1 * (velocityx + velocityy))); } else { // normal rotation dialer.post(new flingrunnable(velocityx + velocityy)); } homecoming true; } } private class flingrunnable implements runnable { private float velocity; public flingrunnable(float velocity) { this.velocity = velocity; } @override public void run() { if (math.abs(velocity) > 5 && allowrotating) { rotatedialer(velocity / 75); velocity /= 1.0666f; // post instance 1 time again dialer.post(this); } } } private double getangle(double xtouch, double ytouch) { double x = xtouch - (dialerwidth / 2d); double y = dialerheight - ytouch - (dialerheight / 2d); switch (getquadrant(x, y)) { case 1: homecoming math.asin(y / math.hypot(x, y)) * 180 / math.pi; case 2: homecoming 180 - math.asin(y / math.hypot(x, y)) * 180 / math.pi; case 3: homecoming 180 + (-1 * math.asin(y / math.hypot(x, y)) * 180 / math.pi); case 4: homecoming 360 + math.asin(y / math.hypot(x, y)) * 180 / math.pi; default: homecoming 0; } } /** * @return selected quadrant. */ private static int getquadrant(double x, double y) { if (x >= 0) { homecoming y >= 0 ? 1 : 4; } else { homecoming y >= 0 ? 2 : 3; } } private void rotatedialer(float degrees) { matrix.postrotate(degrees, dialerwidth / 2, dialerheight / 2); dialer.setimagematrix(matrix); }

}

maybe utilize global var saves previous angle?

case motionevent.action_down: (int = 0; < quadranttouched.length; i++) { quadranttouched[i] = false; } allowrotating = false; startangle = previousangle; break; case motionevent.action_move: currentangle = getangle(event.getx(), event.gety()); rotatedialer((float) (startangle - currentangle)); startangle = currentangle; break; case motionevent.action_up: previousangle = currentangle; break;

this won't work idea...

android image bitmap android-animation

No comments:

Post a Comment