无法编辑自定义"pinch to zoom"编辑文本



我的目标是创建一个缩放编辑文本。

经过一番谷歌搜索,有人好心地在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;
    }
}

类是完美的"拖放"one_answers"缩放",但编辑文本不能被编辑。键盘显示,但编辑文本不集中,所以我不能输入任何东西。

编辑部分在这个代码上:

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

请帮助我,如果你有更多的问题请告诉我。由于

链接在这里:android pinch zoom

setFocusable(true);改为setFocusableInTouchMode(true);。它起作用了,但还不够完美。你不能在你的edittext里面随意点击,但是edittext是可以编辑的。

最新更新