7

我正在尝试将图像从其中心点旋转一圈,但我无法在所需位置停止,因为我可以进行旋转,但我想在360'(1 round).

public class RotateRoundActivity extends Activity implements OnTouchListener
{

    private ImageView dialer;
    //private float y=0;
    private float x=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dialer = (ImageView) findViewById(R.id.big_button);
        dialer.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    //  double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());

        double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-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;
    }

旋转方式@

    private void updateRotation(double rot){
        float newRot=new Float(rot);
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        Matrix matrix=new Matrix();
        matrix.postRotate(newRot,bitmap.getWidth(),bitmap.getHeight());
        Log.i("demo===>", "matrix==>" + matrix);
     //   Log.i("demo===", "y===>" + y);
        Log.i("demo===", "x===>" + x);

        if(x>250){
            Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
            dialer.setImageBitmap(reDrawnBitmap);
        }
        else{
            Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
            dialer.setImageBitmap(reDrawnBitmap);
        }
    }

}

你的建议是可观的。

4

2 回答 2

3

您必须保存以前的rot值。并添加签入updateRotation方法,如果previousRot在 360' 度的左侧并且rot在 360' 度的右侧,那么我们进行了 1 轮并需要停止旋转。

顺时针案例的示例代码

if (previousRot >= 300 && previousRot <= 360 && rot >= 0 && rot <= 60) {
    rot = 359.99; // or here can be 360'
}

对于逆时针情况,它几乎相同,但值交换了

if (previousRot >= 0 && previousRot <= 60 && rot >= 300 && rot <= 360) {
    rot = 0;
}

此代码将停止旋转。从一开始previousRot应该是 0 顺时针的情况和 359.99 逆时针


另一种方法是再添加一个变量来存储总行驶角度。从一开始traveledAngle就必须等于 0。如果你按顺时针方向旋转,你必须通过 和 之间的差来增加rotpreviousRot。逆时针旋转时,将其减小相同的值。

traveledAngle += rot - previousRot;

traveledAngle大于 360' 时需要停止顺时针方向旋转,当小于 0 时需要停止逆时针方向旋转。

于 2012-07-05T09:15:40.657 回答
2

我已经使用了您的演示并添加了一些逻辑,较新的演示如下:

public class RotateRoundActivity extends Activity implements OnTouchListener {
    float rot1=0.0F, rot2=0.0F;
    boolean clockwise, rotationDone = false, halfrotated = false;
    int rotcall=0;

    private ImageView dialer;
    //private float y=0;
    private int x=0;
    //private int y=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dialer = (ImageView) findViewById(R.id.big_button);
        dialer.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    //  double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
        double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
        int rotation=(int)Math.toDegrees(r);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                x=(int) event.getX();
                //y=(int) event.getY();
                updateRotation(rotation);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }//switch       

        return true;
    }

    private void updateRotation(double rot){
        float newRot = new Float(rot);

        rotcall++;
        if(rotcall == 1)
            rot1 = new Float(rot);
        if(rotcall == 2)
            rot2 = new Float(rot);
        if(rot1 != 0.0F && rot2 != 0.0F)
            if(rot1 < rot2)
                clockwise = true;
            else
                clockwise = false;
        System.out.println("Rotate :: "+newRot);

        if(clockwise && rot1>=0 ) {
            if(newRot < 0)
                halfrotated = true;
            if(halfrotated && newRot > 0)
                rotationDone = true;
            if(rotationDone)
                newRot = 0;
        }
        if(clockwise && rot1<0) {
            if(newRot > 0)
                halfrotated = true;
            if(halfrotated && newRot < 0)
                rotationDone = true;
            if(rotationDone)
                newRot = 0;
        }
        if(!clockwise && rot1<0) {
            if(newRot > 0)
                halfrotated = true;
            if(halfrotated && newRot < 0)
                rotationDone = true;
            if(rotationDone)
                newRot = 0;
        }
        if(!clockwise && rot1>=0) {
            if(newRot < 0)
                halfrotated = true;
            if(halfrotated && newRot > 0)
                rotationDone = true;
            if(rotationDone)
                newRot = 0;
        }

        System.out.println("Rotation Done :: "+rotationDone);

        if(!rotationDone) {
            //BitmapDrawable bitmapDrawable = (BitmapDrawable) dialer.getDrawable();
            //Bitmap bitmap = bitmapDrawable.getBitmap();
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.  YOUR_DRBL  );
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Matrix matrix = new Matrix();
            matrix.postRotate(newRot, width, height);
            System.out.println("x===>" + x);
            //System.out.println("y===>" + y);

            //if (x > 250) {
                Bitmap reDrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
                dialer.setImageBitmap(reDrawnBitmap);
            /*} else {
                Bitmap reDrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                        width, height, matrix, true);
                dialer.setImageBitmap(reDrawnBitmap);
            }*/
        }
    }

}
于 2012-07-05T14:12:37.447 回答