如何旋转图像定位点以及触摸android系统 [英] How to rotate image with fix point along with touch in android

查看:107
本文介绍了如何旋转图像定位点以及触摸android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找了一天解决这个问题,但没有什么帮助,即便是在这里的答案不帮我.... 马问题是马图像旋转,但不是位图的大小是varing同时旋转......

I've been looking for over a day for a solution to this problem but nothing helps, even the answers here dont help me.... ma problem is ma image is rotating but not bitmap size is varing while rotating .....

下面是马云code

package dharam.rotate;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class Rotate extends Activity {
    View baseView ;
    ImageView turntable;
    TextView bottomText;
    GestureDetector   gestureDetector;
    private float x , y;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    baseView = (View) findViewById(R.id.baseView);
    turntable = (ImageView) findViewById(R.id.turntable);

    turntable.setOnTouchListener(onTableTouched);
    baseView.setOnTouchListener(onTableTouched);
}

public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent evt) {
        double r = Math.atan2(evt.getX() - turntable.getWidth() / 2,
                (turntable.getHeight() / 2) - evt.getY());
        int rotation = (int) Math.toDegrees(r);
        Log.i("R is ", ""+r);
        if (evt.getAction() == MotionEvent.ACTION_DOWN) {
        }

        if (evt.getAction() == MotionEvent.ACTION_MOVE) {
            x= evt.getX();
            y= evt.getY();
            updateRotation(rotation);
        }

        if (evt.getAction() == MotionEvent.ACTION_UP) {
                //
        }
        return true;
    }
};
private void updateRotation(double rot) {
    float newRot = new Float(rot);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
    R.drawable.orsl_circle_transparent);
    Matrix matrix = new Matrix();
//        matrix.setTranslate(getWindowManager().getDefaultDisplay().getWidth()/2, getWindowManager().getDefaultDisplay().getHeight());
        matrix.postRotate(newRot,bitmap.getWidth()/2,bitmap.getHeight()/2);
//      matrix.setSinCos(newRot, newRot/2, 100, 100);
//      matrix.postRotate(newRot);
        Log.i("THE ROATTION ", "  "+    newRot);

    if(y>250)
    {
        Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap
                .getWidth(), bitmap.getHeight(), matrix, true);
        turntable.setImageBitmap(redrawnBitmap);
    }else
    {
        Bitmap  redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap
                .getWidth(), bitmap.getHeight(), matrix, true);
         turntable.setImageBitmap(redrawnBitmap);
         Log.i("GUITAR _IMAGE", "");
    }

}

}

推荐答案

我presume?如果是这样,延长SimpleOnGestureListener像这样的例子:

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
    }
}

一旦你得到了触摸事件的屏幕坐标,你可以申请一个旋转动画有关的地步 - 在这里看到更多的细节:的 http://developer.android.com/guide/topics/graphics/2d-graphics.html

这篇关于如何旋转图像定位点以及触摸android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆