我正在尝试旋转此图像上的一个透明PNG.数字部分是我想要旋转的部分.我能够做到这一点,但这不是我想要实现的目标
http://i48.tinypic.com/25exzle.png
我想像真正的密码锁一样旋转数字.因此,用户将触摸并将他们的手指移动一圈.我在触摸/移动事件中看到了不太精确的图像旋转,但它们还不够.
这是我目前的代码
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,bitmap.getHeight(),matrix,true); lockNumbers.setImageBitmap(reDrawnBitmap); } else{ Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,true); lockNumbers.setImageBitmap(reDrawnBitmap); } }
由于矩阵参数,它也会在触摸时调整位图的大小.这不是理想的效果.
用户几乎需要用手指进入一个圆圈.
解决方法
将以下代码写入您的触摸事件.
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;