自定义"捏缩放" edittext无法编辑

时间:2014-07-07 04:57:07

标签: android drag-and-drop android-edittext pinchzoom

我的目标是创建一个缩放来缩放edittext。

经过一些谷歌搜索,有人愿意在github上分享这个课程:

public ZoomEditTextView(Context context, AttributeSet attrs) {
    super(context, attrs, 0);
    this.mContext = context;
    init();

}

public ZoomEditTextView(Context context) {
    super(context, null, 0);
    this.mContext = context;
    init();

    setFocusable(true);
    requestFocus();

    /*@formatter:off
    this.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent me) {

            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
            imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);

            return true;
        }

    });*/

}

private void init() {

    // Create our ScaleGestureDetector
    mScaleDetector = new ScaleGestureDetector(mContext, new ScaleListener());

    // Sets up drawing tools
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mCirclePaint = new Paint();
    mCirclePath = new Path();
    mCirclePaint.setAntiAlias(true);
    mCirclePaint.setColor(Color.CYAN);
    mCirclePaint.setStyle(Paint.Style.STROKE);
    mCirclePaint.setStrokeJoin(Paint.Join.MITER);
    mCirclePaint.setStrokeWidth(4f);

    //

    mDrawingPaint = new Paint();

    mDrawingPaint.setAntiAlias(true);
    mDrawingPaint.setDither(true);
    mDrawingPaint.setColor(Color.GREEN);
    mDrawingPaint.setStyle(Paint.Style.STROKE);
    mDrawingPaint.setStrokeJoin(Paint.Join.ROUND);
    mDrawingPaint.setStrokeCap(Paint.Cap.ROUND);
    mDrawingPaint.setStrokeWidth(mBrushSize);

    mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mRectPaint.setStyle(Paint.Style.STROKE);
    mRectPaint.setColor(Color.WHITE);

    mDrawable = mContext.getResources().getDrawable(R.drawable.icon);
    mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
    setDrawingCacheEnabled(true);
    buildDrawingCache();

}

@Override
protected void onDraw(Canvas canvas) {

    canvas.save();
    //
    canvas.translate(mPosX, mPosY);
    canvas.scale(mScaleFactor, mScaleFactor);
    // canvas.drawRect(new Rect(0, 0, canvas.getWidth(),
    // canvas.getHeight()), mRectPaint);

    super.onDraw(canvas);

    // mDrawable.draw(canvas);
    // if (mBitmap != null && canvas != null) {
    // canvas.drawBitmap(mBitmap, mPosX, mPosY, mPaint);
    // }

    canvas.restore();

}

// Listen for multi-touch drag event and redraw the view accordingly
@Override
public boolean onTouchEvent(MotionEvent event) {
    // Let the ScaleGestureDetector inspect all events.
    mScaleDetector.onTouchEvent(event);

    final int action = event.getAction();
    switch (action) {

    // a touch down
        case MotionEvent.ACTION_DOWN: {
            // Scale detector is not in progress
            if (!mScaleDetector.isInProgress()) {
                final float x = event.getX();
                final float y = event.getY();
                // Save the ID of this pointer
                mActivePointerId = event.getPointerId(0);

                // Remember where we started
                mLastTouchX = x;
                mLastTouchY = y;

                // Save the ID of this pointer
                mActivePointerId = event.getPointerId(0);

                break;

            }
        }

        case MotionEvent.ACTION_MOVE: {

            // Only move the image if the scale detector is not in progress
            if (!mScaleDetector.isInProgress()) {
                // Find the index of active pointer and save its position
                final int pointerIndex = event.findPointerIndex(mActivePointerId);
                final float x = event.getX(pointerIndex);
                final float y = event.getY(pointerIndex);

                // mBitmap = getDrawingCache();

                // Calculate the distance moved
                float dx = x - mLastTouchX;
                float dy = y - mLastTouchY;

                // Move the object
                mPosX += dx;
                mPosY += dy;

                // Remember this touch position for the next move event
                mLastTouchX = x;
                mLastTouchY = y;

                // Invalidate to request a redraw
                invalidate();

                // break;

            } /*
               * else {
               * 
               * final float gx = mScaleDetector.getFocusX(); final float gy
               * = mScaleDetector.getFocusY();
               * 
               * final float gdx = gx - mLastGestureX; final float gdy = gy
               * - mLastGestureY;
               * 
               * mPosX += gdx; mPosY += gdy;
               * 
               * invalidate();
               * 
               * mLastGestureX = gx; mLastGestureY = gy;
               * 
               * }
               */

            break;
        }

        case MotionEvent.ACTION_UP: {
            //setFocusable(true);
            //requestFocus();

            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
            //imm.showSoftInput(ZoomEditTextView.this, InputMethodManager.SHOW_IMPLICIT);
            imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);

            // Reset the active pointer id
            mActivePointerId = INVALID_POINTER_ID;

            break;
        }

        case MotionEvent.ACTION_CANCEL: {

            mActivePointerId = INVALID_POINTER_ID;
            break;

        }

        case MotionEvent.ACTION_POINTER_UP: {
            // Extract the index of the pointer that left the touch sensor
            final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            final int pointerId = event.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;

                if (event.getPointerCount() >= 2) {
                    mLastTouchX = event.getX(newPointerIndex);
                    mLastTouchY = event.getY(newPointerIndex);
                }
                mActivePointerId = event.getPointerId(newPointerIndex);

            } else {
                final int tempPointerIndex = event.findPointerIndex(mActivePointerId);
                mLastTouchX = event.getX(tempPointerIndex);
                mLastTouchY = event.getY(tempPointerIndex);
            }
            break;

        }

    }

    return true;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        invalidate();

        return true;
    }
}

该类适用于“拖放”和“缩放缩放”,但编辑文本无法编辑。显示键盘,但编辑文本没有聚焦,所以我无法输入任何内容。

编辑部分在此代码上:

    case MotionEvent.ACTION_UP: {
        //setFocusable(true);
        //requestFocus();

        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
        //imm.showSoftInput(ZoomEditTextView.this, InputMethodManager.SHOW_IMPLICIT);
        imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);

        // Reset the active pointer id
        mActivePointerId = INVALID_POINTER_ID;

        break;
    }

请帮助我,如果您有更多问题,请告诉我。感谢

2 个答案:

答案 0 :(得分:2)

链接在这里:android pinch zoom

答案 1 :(得分:0)

只需将setFocusable(true);更改为setFocusableInTouchMode(true);即可。它的工作但尚未完美。您无法在编辑文本中自由点按,但编辑文本可以使用此编辑。