使用onTouchListener旋转图像而不缩放图像

时间:2016-02-02 17:15:07

标签: java android ontouchlistener image-rotation

我有这个简单的代码用onTouchListener旋转图像,但它似乎也在图像上放大和缩小。我真的很擅长使用onTouchListener。有人能告诉我发生了什么,或者如何解决这个问题。任何事情都有所帮助,谢谢:)

public class MainActivity extends Activity implements OnTouchListener {

private ImageView dialer;
private float y=0;
public boolean onTouch(View v, MotionEvent event) {
    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;

}//onTouch
private void updateRotation(double rot){
    float newRot= new Float(rot);
    Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.round_button_big);
    Matrix matrix= new Matrix();
    matrix.postRotate(newRot,bitmap.getWidth()/2,bitmap.getHeight()/2);
    if(y>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);
    }
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dialer = (ImageView) findViewById(R.id.image);
    dialer.setOnTouchListener(this);
}//onCreate

}

1 个答案:

答案 0 :(得分:1)

尝试在触摸事件中使用此代码:

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;

在这里你可以找到一个例子:http://code.tutsplus.com/tutorials/android-sdk-creating-a-rotating-dialer--mobile-8868