wuapnjie / StickerView

[No more support] A view which can add sticker and zoom,drag,delete it
MIT License
1.27k stars 311 forks source link

更改sticker的双指手势 #139

Open wens-jie opened 5 years ago

wens-jie commented 5 years ago

public class StickerView extends com.xiaopo.flying.sticker.StickerView {

public StickerView(Context context) {
    super(context);
}

public StickerView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public StickerView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void handleCurrentMode(@NonNull MotionEvent event) {
    try {
        int currentMode;
        Sticker handlingSticker;
        Matrix moveMatrix;
        Class mClass = Class.forName("com.xiaopo.flying.sticker.StickerView");
        Field field = mClass.getDeclaredField("currentMode");
        field.setAccessible(true);
        currentMode = field.getInt(this);
        field = mClass.getDeclaredField("handlingSticker");
        field.setAccessible(true);
        handlingSticker = (Sticker) field.get(this);
        field = mClass.getDeclaredField("moveMatrix");
        field.setAccessible(true);
        moveMatrix = (Matrix) field.get(this);
        if (currentMode == 2)
            if (handlingSticker != null && moveMatrix != null) {
                float newDistance = calculateDistance(event);
                float newRotation = calculateRotation(event);
                field = mClass.getDeclaredField("downMatrix");
                field.setAccessible(true);
                Matrix downMatrix = (Matrix) field.get(this);
                field = mClass.getDeclaredField("oldDistance");
                field.setAccessible(true);
                float oldDistance = field.getFloat(this);

                field = mClass.getDeclaredField("oldRotation");
                field.setAccessible(true);
                float oldRotation = field.getFloat(this);

                field = mClass.getDeclaredField("midPoint");
                field.setAccessible(true);

                PointF midPoint = (PointF) field.get(this);
                moveMatrix.set(downMatrix);
                moveMatrix.postScale(newDistance / oldDistance, newDistance / oldDistance, midPoint.x,
                        midPoint.y);
                moveMatrix.postRotate(newRotation - oldRotation, midPoint.x, midPoint.y);
                field = mClass.getDeclaredField("downX");
                field.setAccessible(true);
                float downX = field.getFloat(this);
                field = mClass.getDeclaredField("downY");
                field.setAccessible(true);
                float downY = field.getFloat(this);
                moveMatrix.postTranslate(event.getX() - downX, event.getY() - downY);
                handlingSticker.setMatrix(moveMatrix);
                return;
            }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    super.handleCurrentMode(event);
}

}

wens-jie commented 5 years ago

可以保证同时移动缩放旋转