2 Finger Rotation gesture listener in android

孤街醉人 提交于 2019-12-10 10:14:42

问题


I'm trying to figure out the best way to make an image rotate along with a user's finger dragging it left or right, and the angle of rotation.

float x1 = lastEvent[0] - lastEvent[1];
float y1 = lastEvent[2] - lastEvent[3];    
float degrees1 = (float)(Math.atan2(y1, x1));      
float x2 = event.getX(0) - event.getX(1);
float y2 = event.getY(0) - event.getY(1);
float degrees2 = (float)(Math.atan2(y2, x2));   

float degrees = (float) Math.toDegrees(degrees2-degrees1);

But it doesn't rotate like i want it.. Is there any listener for rotation gesture? Thanks


回答1:


/** Determine the degree between the first two fingers */
    private float rotation(MotionEvent event) { 
        double delta_x = (event.getX(0) - event.getX(1));
        double delta_y = (event.getY(0) - event.getY(1));
        double radians = Math.atan2(delta_y, delta_x);       
        if (Constant.TRACE) Log.d("Rotation ~~~~~~~~~~~~~~~~~", delta_x+" ## "+delta_y+" ## "+radians+" ## "
                        +Math.toDegrees(radians));
        return (float) Math.toDegrees(radians);
    }



回答2:


This link is very useful if you are looking a good explanation. By Using this library, you can create a package and copy/past SandboxView, TouchManager and Vector2D classes into that package.

Then, add FrameLayout into your xml file (instead of imageView) and link it to your code.

Finally, add bitmap to the layout by using following code:

try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), mUserImgUri);
            sandboxView = new SandboxView(mContext, bitmap);
            sandboxView.setLayoutParams(new FrameLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            sandboxView.setVisibility(View.INVISIBLE);
            frameLayout.addView(sandboxView);
        } catch (IOException e) {
            e.printStackTrace();
        }


来源:https://stackoverflow.com/questions/8570900/2-finger-rotation-gesture-listener-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!