动态添加edittext应用捏缩放,旋转和拖动

时间:2014-09-24 05:34:20

标签: android rotation android-edittext zoom drag

任何人都可以帮助我,如何实现捏缩放,旋转和拖动动态edittext android。我尝试了很多东西,但没有得到适当的输出。请告诉我实现这个的简单方法。 这是我的活动。

public class MainActivity extends Activity implements OnRotationGestureListener {

RelativeLayout container;
TextView curEt;
private float oldDist = 1f;
int count = 0;
private PointF mid = new PointF();
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private float[] lastEvent = null;
private int mode = NONE;
public int firstClick;
public int secClick;
public int offset_x;
public int offset_y;
private PointF start = new PointF();
private float degrees, prevDegrees;
private RotationGestureDetector mRotationDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_editable_area);
    container = (RelativeLayout) findViewById(R.id.container);
    mRotationDetector = new RotationGestureDetector(this);
    curEt = new EditText(this);
    container.setOnTouchListener(new MyTouch());
}

class MyTouch implements OnTouchListener {
    private static final float MAX_FONT_SIZE = 100;
    private static final float MIN_FONT_SIZE = 10;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mRotationDetector.onTouchEvent(event);
        System.out.println();
        if (container == v) {
            if (MotionEvent.ACTION_DOWN == event.getAction()) {
                createET(event);
            }
        } else {
            curEt = (EditText) v;
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                start.set(event.getX(), event.getY());
                offset_x = (int) event.getX();
                offset_y = (int) event.getY();
                mode = DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                midPoint(mid, event);
                mode = ZOOM;
                lastEvent = new float[4];
                lastEvent[0] = event.getX(0);
                lastEvent[1] = event.getX(1);
                lastEvent[2] = event.getY(0);
                lastEvent[3] = event.getY(1);
                // d = rotation(event);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                prevDegrees = degrees;
                mode = NONE;
                // lastEvent = null;
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    // curEt.setX(event.getRawX() - 40);
                    // curEt.setY(event.getRawY() - 150);
                } else if (mode == ZOOM) {
                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        float scale = newDist / oldDist;
                        if (scale > 1) {
                            scale = 1.1f;
                        } else if (scale < 1) {
                            scale = 0.95f;
                        }
                        float currentSize = curEt.getTextSize() * scale;
                        if ((currentSize < MAX_FONT_SIZE && currentSize > MIN_FONT_SIZE)
                                || (currentSize >= MAX_FONT_SIZE && scale < 1)
                                || (currentSize <= MIN_FONT_SIZE && scale > 1)) {
                            curEt.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                                    currentSize);
                        }
                    }
                    if (lastEvent != null) {
                        // float newRot = rotation(event);
                        // float r = newRot - d;
                        if (degrees < 180 && degrees > -180) {
                            curEt.animate().rotationBy(-degrees);
                            curEt.setRotation(-degrees);
                        }
                        // RotateAnimation an = new RotateAnimation(0.0f,
                        // degrees, 0, 0);
                        // RotateAnimation anim = new RotateAnimation(0,
                        // degrees,
                        // Animation.RELATIVE_TO_SELF, 0f,
                        // Animation.RELATIVE_TO_SELF,0f);
                        // //degrees = (degrees + 30) % 360;
                    }
                }
                break;
            }
        }
        return false;
    }
}
private void createET(MotionEvent event) {
    EditText et = new EditText(MainActivity.this);
    curEt = et;
    curEt.setHint("EditText");
    curEt.setTag(et.getId());
    curEt.setX(event.getX());
    curEt.setFocusable(false);
    curEt.setY(event.getY());
    curEt.setSingleLine(true);
    curEt.setOnTouchListener(new MyTouch());
    container.addView(et);
}

/* Determine the space between the first two fingers */
private float spacing(MotionEvent event) {
    return (float) (Math.pow((event.getX(1) - event.getX(0)), 2) + Math
            .pow((event.getY(1) - event.getY(0)), 2));
}

/* Calculate the mid point of the first two fingers */
private void midPoint(PointF point, MotionEvent event) {
    float x = event.getX(0) + event.getX(1);
    float y = event.getY(0) + event.getY(1);
    point.set(x / 2, y / 2);
}
@Override
public void OnRotation(RotationGestureDetector rotationDetector) {
    degrees = rotationDetector.getAngle();
    System.out.println(degrees);
}
/* Calculate the degree to be rotated by. */
// private float rotation(MotionEvent event) {
// float x = event.getX(0) - event.getX(1);
// float y = event.getY(0) - event.getY(1);
// DisplayMetrics displaymetrics = new DisplayMetrics();
// getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
// int height = displaymetrics.heightPixels;
// int width = displaymetrics.widthPixels;
// double delta_x = x - (width) /2;
// double delta_y = (height) /2 - y;
// double radians = Math.atan2(delta_y, delta_x);
// return (float) Math.toDegrees(radians);
// }

}

和XML布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

请帮我解决这个问题。如果我尝试旋转它会产生闪烁效果。

0 个答案:

没有答案