1343502387_6472  

剛一眼打量過去,吸引我們的就是那四個大點。就從它開始吧,目前看來這個大點是一個圖片,當點擊下去的時候有加亮的效果,可能這又是一張圖片。我們先不要考慮這些,先把它當做一個普通的Point來處理就好了。正常來講的話,我們這裏需要四個Point,但是考慮到後面我們對這些點進行复雜的運算和事件處理,只要申請兩個Point就好了,以便降低問題的复雜度。兩個即leftUpPoint和rightDownPoint。(其餘兩個點只要由這個兩點拼一下就可以了)

那麼畫中間的框就用下面的代碼:

        paint.setARGB(255, 255, 255, 255);
        paint.setStrokeWidth(3);
        paint.setStyle(Paint.Style.STROKE);
        Rect frameRect = new Rect(leftUpPoint.x + offsetLeftUpPoint.x,
                leftUpPoint.y + offsetLeftUpPoint.y, rightDownPoint.x
                        + offsetRightDownPoint.x, rightDownPoint.y
                        + offsetRightDownPoint.y);
        canvas.drawRect(frameRect, paint);

offsetLeftUpPoint和offsetRightDownPoint是leftUpPoint和rightDownPoint相對的偏移量,後面事件處理的時候會用到。

然後順勢把裏面的線條和數字畫出來。代碼如下:

 

// 畫內框裏線條
        paint.setARGB(90, 255, 255, 255);
        paint.setStrokeWidth(2);
        canvas.drawLine(leftUpPoint.x + this.getDynamicFrameWidth() / 3
                + offsetLeftUpPoint.x, leftUpPoint.y + offsetLeftUpPoint.y,
                leftUpPoint.x + this.getDynamicFrameWidth() / 3
                        + offsetLeftUpPoint.x, rightDownPoint.y
                        + offsetRightDownPoint.y, paint);
        canvas.drawLine(rightDownPoint.x - this.getDynamicFrameWidth() / 3
                + offsetRightDownPoint.x, leftUpPoint.y + offsetLeftUpPoint.y,
                rightDownPoint.x - this.getDynamicFrameWidth() / 3
                        + offsetRightDownPoint.x, rightDownPoint.y
                        + offsetRightDownPoint.y, paint);
        canvas.drawLine(leftUpPoint.x + offsetLeftUpPoint.x, leftUpPoint.y
                + this.getDynamicFrameHeight() / 3 + offsetLeftUpPoint.y,
                rightDownPoint.x + offsetRightDownPoint.x,
                leftUpPoint.y + this.getDynamicFrameHeight() / 3
                        + offsetLeftUpPoint.y, paint);
        canvas.drawLine(leftUpPoint.x + offsetLeftUpPoint.x, rightDownPoint.y
                - this.getDynamicFrameHeight() / 3 + offsetRightDownPoint.y,
                rightDownPoint.x + offsetRightDownPoint.x, rightDownPoint.y
                        - this.getDynamicFrameHeight() / 3
                        + offsetRightDownPoint.y, paint);
        // 畫外框裏的數字
        paint.setARGB(255, 255, 255, 255);
        paint.setStrokeWidth(0);
        paint.setTextSize(10);
        paint.setTextAlign(Align.CENTER);
        canvas.drawText(
                this.getDynamicFrameWidth() + "*"
                        + this.getDynamicFrameHeight(),
                (leftUpPoint.x + offsetLeftUpPoint.x + rightDownPoint.x + offsetRightDownPoint.x) / 2,
                (leftUpPoint.y + offsetLeftUpPoint.y + rightDownPoint.y
                        + offsetRightDownPoint.y + 10) / 2, paint);

沒有什麼說的,大家只要注意一下線條和數字與中間框的坐標關系就可以了,代碼上提到的:getDynamicFrameWidth()和getDynamicFrameHeight()這兩個方法,後面也會說明的。

 

再看仔細看一下中間框的外圍的情況,像是有一種透明黒的效果,那就對了,這也是一種效果哦,也需要“畫”的哦。

還是注意分析與中間框的坐標關系,代碼如下咯:

// 畫內框外圍
        paint.setARGB(180, 0, 0, 0);
        Rect top = new Rect(0, 0, width, leftUpPoint.y + offsetLeftUpPoint.y);
        Rect bottom = new Rect(0, rightDownPoint.y + offsetRightDownPoint.y,
                width, height);
        Rect left = new Rect(0, leftUpPoint.y + offsetLeftUpPoint.y,
private int getDynamicFrameWidth() {
        return (rightDownPoint.x + offsetRightDownPoint.x)
                - (leftUpPoint.x + offsetLeftUpPoint.x);
    }

    private int getDynamicFrameHeight() {
        return (rightDownPoint.y + offsetRightDownPoint.y)
                - (leftUpPoint.y + offsetLeftUpPoint.y);
    }

    private int getStaticFrameWidth() {
        return rightDownPoint.x - leftUpPoint.x;
    }

    private int getFrameHeight() {
        return rightDownPoint.y - leftUpPoint.y;
    }

leftUpPoint.x + offsetLeftUpPoint.x, rightDownPoint.y + offsetRightDownPoint.y); Rect right = new Rect(rightDownPoint.x + offsetRightDownPoint.x, leftUpPoint.y + offsetLeftUpPoint.y, width, rightDownPoint.y + offsetRightDownPoint.y); canvas.drawRect(top, paint); canvas.drawRect(bottom, paint); canvas.drawRect(left, paint); canvas.drawRect(right, paint);
 

靜態的問題已經基本處理完了,下面我們要把拖動和放大縮小這兩動態問題考慮進來。

那就需要我們重寫View的onTouchEvent()這個方法,我們要實現整個效果全在這個方法裏呢。

第一步,我要將這個方法的返回值設置成true,否則就接收不到後面的ACTION_MOVE和ACTION_UP這兩個事件了。要是這样就悲剧了大哭

第二步,在設置返回值之前,要執行一下this.invalidate();這個方法,因为View的onDraw()方法是被動的,只有當View無效的時候才會執行。

預熱一下,說明一下我們需要東西:

a:onTouchEvent()方法的框架,即能處理ACTION_DOWN、ACTION_MOVE和ACTION_UP三種事件。

b:記錄leftUpPoint和rightDownPoint兩個點的偏移量offsetLeftUpPoint和offsetRightDownPoint

c:記錄onTouchEvent()上一次點擊的坐標lastPoint,在執行完ACTION_DOWN事件,再執行ACTION_MOVE事件時會用到。

d:六種動作事件的標識符EVENT_NONE = 0, EVENT_MOVE = 1,            EVENT_SCALE_LEFTUP = 2, EVENT_SCALE_LEFTDOWN = 3,            EVENT_SCALE_RIGHTUP = 4, EVENT_SCALE_RIGHTDOWN = 5;及它們的掌控件變量eventType

e:還有那四個取得中間框長和寬和方法

private int getDynamicFrameWidth() {
        return (rightDownPoint.x + offsetRightDownPoint.x)
                - (leftUpPoint.x + offsetLeftUpPoint.x);
    }

    private int getDynamicFrameHeight() {
        return (rightDownPoint.y + offsetRightDownPoint.y)
                - (leftUpPoint.y + offsetLeftUpPoint.y);
    }

    private int getStaticFrameWidth() {
        return rightDownPoint.x - leftUpPoint.x;
    }

    private int getFrameHeight() {
        return rightDownPoint.y - leftUpPoint.y;
    }

什麼是靜態?什麼是又動態呢?在onTouchEvent()方法裏用到長和寬就是靜態,在onDraw()方法用到長和寬就是動態。關於這一點,大家在仔細想一想就會明白了。

不用我多說什麼,我把整個onTouchEvent()方法裏的代碼貼上來,大家看一下就會明白了,大笑

public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Log.d(TAG, "onTouchEvent");
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            // move
            if (leftUpPoint.x < x && x < rightDownPoint.x && leftUpPoint.y < y
                    && y < rightDownPoint.y) {
                lastPoint = new Point(x, y);
                eventType = EVENT_MOVE;
                // leftup
            } else if (Math.abs(x - leftUpPoint.x) < DOT_RADIO
                    && Math.abs(y - leftUpPoint.y) < DOT_RADIO) {
                lastPoint = new Point(x, y);
                eventType = EVENT_SCALE_LEFTUP;
                // leftdown
            } else if (Math.abs(x - leftUpPoint.x) < DOT_RADIO
                    && Math.abs(y - rightDownPoint.y) < DOT_RADIO) {
                lastPoint = new Point(x, y);
                eventType = EVENT_SCALE_LEFTDOWN;
                // rightup
            } else if (Math.abs(x - rightDownPoint.x) < DOT_RADIO
                    && Math.abs(y - leftUpPoint.y) < DOT_RADIO) {
                lastPoint = new Point(x, y);
                eventType = EVENT_SCALE_RIGHTUP;
                // rightdown
            } else if (Math.abs(x - rightDownPoint.x) < DOT_RADIO
                    && Math.abs(y - rightDownPoint.y) < DOT_RADIO) {
                lastPoint = new Point(x, y);
                eventType = EVENT_SCALE_RIGHTDOWN;
            }

        }
            break;
        case MotionEvent.ACTION_MOVE: {
            Log.d("TAG", "ACTION_MOVE");
            switch (eventType) {
            case EVENT_MOVE: {
                offsetLeftUpPoint.x = x - lastPoint.x;
                if (leftUpPoint.x + offsetLeftUpPoint.x < 0) {
                    offsetLeftUpPoint.x = -leftUpPoint.x;
                } else if (leftUpPoint.x + offsetLeftUpPoint.x > this
                        .getWidth() - this.getStaticFrameWidth()) {
                    offsetLeftUpPoint.x = this.getWidth()
                            - this.getStaticFrameWidth() - leftUpPoint.x;
                }

                offsetLeftUpPoint.y = y - lastPoint.y;
                if (leftUpPoint.y + offsetLeftUpPoint.y < 0) {
                    offsetLeftUpPoint.y = -leftUpPoint.y;
                } else if (leftUpPoint.y + offsetLeftUpPoint.y > this
                        .getHeight() - this.getFrameHeight()) {
                    offsetLeftUpPoint.y = this.getHeight()
                            - this.getFrameHeight() - leftUpPoint.y;
                }

                offsetRightDownPoint.x = offsetLeftUpPoint.x;
                offsetRightDownPoint.y = offsetLeftUpPoint.y;
                break;
            }
            case EVENT_SCALE_LEFTUP: {
                offsetLeftUpPoint.x = x - lastPoint.x;
                if (rightDownPoint.x - (leftUpPoint.x + offsetLeftUpPoint.x) < FRAME_MIN_WIDTH) {
                    offsetLeftUpPoint.x = rightDownPoint.x - FRAME_MIN_WIDTH
                            - leftUpPoint.x;
                }
                offsetLeftUpPoint.y = y - lastPoint.y;
                if (rightDownPoint.y - (leftUpPoint.y + offsetLeftUpPoint.y) < FRAME_MIN_HEIGHT) {
                    offsetLeftUpPoint.y = rightDownPoint.y - FRAME_MIN_HEIGHT
                            - leftUpPoint.y;
                }

                offsetRightDownPoint.x = 0;
                offsetRightDownPoint.y = 0;
                break;
            }
            case EVENT_SCALE_LEFTDOWN: {
                offsetLeftUpPoint.x = x - lastPoint.x;
                if (rightDownPoint.x - (leftUpPoint.x + offsetLeftUpPoint.x) < FRAME_MIN_WIDTH) {
                    offsetLeftUpPoint.x = rightDownPoint.x - FRAME_MIN_WIDTH
                            - leftUpPoint.x;
                }
                offsetRightDownPoint.y = y - lastPoint.y;
                if ((rightDownPoint.y + offsetRightDownPoint.y) - leftUpPoint.y < FRAME_MIN_HEIGHT) {
                    offsetRightDownPoint.y = leftUpPoint.y + FRAME_MIN_HEIGHT
                            - rightDownPoint.y;
                }

                offsetLeftUpPoint.y = 0;
                offsetRightDownPoint.x = 0;

                break;
            }
            case EVENT_SCALE_RIGHTUP: {
                offsetRightDownPoint.x = x - lastPoint.x;
                if ((rightDownPoint.x + offsetRightDownPoint.x) - leftUpPoint.x < FRAME_MIN_WIDTH) {
                    offsetRightDownPoint.x = leftUpPoint.x + FRAME_MIN_WIDTH
                            - rightDownPoint.x;
                }
                offsetLeftUpPoint.y = y - lastPoint.y;
                if (rightDownPoint.y - (leftUpPoint.y + offsetLeftUpPoint.y) < FRAME_MIN_HEIGHT) {
                    offsetLeftUpPoint.y = rightDownPoint.y - FRAME_MIN_HEIGHT
                            - leftUpPoint.y;
                }

                offsetLeftUpPoint.x = 0;
                offsetRightDownPoint.y = 0;
                break;
            }
            case EVENT_SCALE_RIGHTDOWN: {
                offsetRightDownPoint.x = x - lastPoint.x;
                if ((rightDownPoint.x + offsetRightDownPoint.x) - leftUpPoint.x < FRAME_MIN_WIDTH) {
                    offsetRightDownPoint.x = FRAME_MIN_WIDTH + leftUpPoint.x
                            - rightDownPoint.x;
                }
                offsetRightDownPoint.y = y - lastPoint.y;
                if ((rightDownPoint.y + offsetRightDownPoint.y) - leftUpPoint.y < FRAME_MIN_HEIGHT) {
                    offsetRightDownPoint.y = FRAME_MIN_HEIGHT + leftUpPoint.y
                            - rightDownPoint.y;
                }

                offsetLeftUpPoint.x = 0;
                offsetLeftUpPoint.y = 0;
                break;
            }
            default:

            }
        }
            break;
        case MotionEvent.ACTION_UP: {
            Log.d("TAG", "ACTION_UP");
            eventType = EVENT_NONE;

            leftUpPoint.x += offsetLeftUpPoint.x;
            leftUpPoint.y += offsetLeftUpPoint.y;
            rightDownPoint.x += offsetRightDownPoint.x;
            rightDownPoint.y += offsetRightDownPoint.y;

            offsetLeftUpPoint.x = 0;
            offsetLeftUpPoint.y = 0;
            offsetRightDownPoint.x = 0;
            offsetRightDownPoint.y = 0;

        }
            break;
        }
        this.invalidate();
        return true;
    }

費話不多說了,有什麼看不懂的,請在博客下面留言:

對了,我把做完的的效果圖貼上來,供大家参考:

1343505863_9119  
 

我把整個自定義View的源碼也貼上來吧:

package com.example.testmei;

import android.annotation.SuppressLint;

@SuppressLint("DrawAllocation")
public class EditImage extends View {

    private Point lastPoint;
    private Point offsetLeftUpPoint, offsetRightDownPoint;
    private static String TAG = EditImage.class.getSimpleName();
    private Point leftUpPoint;
    private Point rightDownPoint;
    private static final int EVENT_NONE = 0, EVENT_MOVE = 1,
            EVENT_SCALE_LEFTUP = 2, EVENT_SCALE_LEFTDOWN = 3,
            EVENT_SCALE_RIGHTUP = 4, EVENT_SCALE_RIGHTDOWN = 5;
    private int eventType;
    private Bitmap dot, dotSelected;
    private int DOT_RADIO = 10;
    private static final int FRAME_MIN_WIDTH = 50;
    private static final int FRAME_MIN_HEIGHT = 50;

    public EditImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        offsetLeftUpPoint = new Point(0, 0);
        offsetRightDownPoint = new Point(0, 0);

        eventType = EVENT_NONE;
        dot = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.img_cut_dot);
        dotSelected = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.img_cut_dotselect);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        leftUpPoint = new Point(w / 4, h / 4);
        rightDownPoint = new Point(w * 3 / 4, h * 3 / 4);
        Log.d(TAG, "onSizeChanged");
        super.onSizeChanged(w, h, oldw, oldh);
    }

    public void drawMei(Canvas canvas) {
        int width = this.getWidth();
        int height = this.getHeight();

        Log.d(TAG, "offsetLeftUpPoint:" + offsetLeftUpPoint.toString());
        Log.d(TAG, "offsetRightDownPoint:" + offsetRightDownPoint.toString());
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        // 畫內框外圍
        paint.setARGB(180, 0, 0, 0);
        Rect top = new Rect(0, 0, width, leftUpPoint.y + offsetLeftUpPoint.y);
        Rect bottom = new Rect(0, rightDownPoint.y + offsetRightDownPoint.y,
                width, height);
        Rect left = new Rect(0, leftUpPoint.y + offsetLeftUpPoint.y,
                leftUpPoint.x + offsetLeftUpPoint.x, rightDownPoint.y
                        + offsetRightDownPoint.y);
        Rect right = new Rect(rightDownPoint.x + offsetRightDownPoint.x,
                leftUpPoint.y + offsetLeftUpPoint.y, width, rightDownPoint.y
                        + offsetRightDownPoint.y);
        canvas.drawRect(top, paint);
        canvas.drawRect(bottom, paint);
        canvas.drawRect(left, paint);
        canvas.drawRect(right, paint);

        // 畫內框邊緣
        paint.setARGB(255, 255, 255, 255);
        paint.setStrokeWidth(3);
        paint.setStyle(Paint.Style.STROKE);
        Rect frameRect = new Rect(leftUpPoint.x + offsetLeftUpPoint.x,
                leftUpPoint.y + offsetLeftUpPoint.y, rightDownPoint.x
                        + offsetRightDownPoint.x, rightDownPoint.y
                        + offsetRightDownPoint.y);
        canvas.drawRect(frameRect, paint);

        // 畫內框裏線條
        paint.setARGB(90, 255, 255, 255);
        paint.setStrokeWidth(2);
        canvas.drawLine(leftUpPoint.x + this.getDynamicFrameWidth() / 3
                + offsetLeftUpPoint.x, leftUpPoint.y + offsetLeftUpPoint.y,
                leftUpPoint.x + this.getDynamicFrameWidth() / 3
                        + offsetLeftUpPoint.x, rightDownPoint.y
                        + offsetRightDownPoint.y, paint);
        canvas.drawLine(rightDownPoint.x - this.getDynamicFrameWidth() / 3
                + offsetRightDownPoint.x, leftUpPoint.y + offsetLeftUpPoint.y,
                rightDownPoint.x - this.getDynamicFrameWidth() / 3
                        + offsetRightDownPoint.x, rightDownPoint.y
                        + offsetRightDownPoint.y, paint);
        canvas.drawLine(leftUpPoint.x + offsetLeftUpPoint.x, leftUpPoint.y
                + this.getDynamicFrameHeight() / 3 + offsetLeftUpPoint.y,
                rightDownPoint.x + offsetRightDownPoint.x,
                leftUpPoint.y + this.getDynamicFrameHeight() / 3
                        + offsetLeftUpPoint.y, paint);
        canvas.drawLine(leftUpPoint.x + offsetLeftUpPoint.x, rightDownPoint.y
                - this.getDynamicFrameHeight() / 3 + offsetRightDownPoint.y,
                rightDownPoint.x + offsetRightDownPoint.x, rightDownPoint.y
                        - this.getDynamicFrameHeight() / 3
                        + offsetRightDownPoint.y, paint);
        // 畫外框裏的數字
        paint.setARGB(255, 255, 255, 255);
        paint.setStrokeWidth(0);
        paint.setTextSize(10);
        paint.setTextAlign(Align.CENTER);
        canvas.drawText(
                this.getDynamicFrameWidth() + "*"
                        + this.getDynamicFrameHeight(),
                (leftUpPoint.x + offsetLeftUpPoint.x + rightDownPoint.x + offsetRightDownPoint.x) / 2,
                (leftUpPoint.y + offsetLeftUpPoint.y + rightDownPoint.y
                        + offsetRightDownPoint.y + 10) / 2, paint);

        //
        Rect leftUpDot = new Rect(leftUpPoint.x - DOT_RADIO
                + offsetLeftUpPoint.x, leftUpPoint.y - DOT_RADIO
                + offsetLeftUpPoint.y, leftUpPoint.x + DOT_RADIO
                + offsetLeftUpPoint.x, leftUpPoint.y + DOT_RADIO
                + offsetLeftUpPoint.y);
        Rect leftDownDot = new Rect(leftUpPoint.x - DOT_RADIO
                + offsetLeftUpPoint.x, rightDownPoint.y - DOT_RADIO
                + offsetRightDownPoint.y, leftUpPoint.x + DOT_RADIO
                + offsetLeftUpPoint.x, rightDownPoint.y + DOT_RADIO
                + offsetRightDownPoint.y);
        Rect rightUpDot = new Rect(rightDownPoint.x - DOT_RADIO
                + offsetRightDownPoint.x, leftUpPoint.y - DOT_RADIO
                + offsetLeftUpPoint.y, rightDownPoint.x + DOT_RADIO
                + offsetRightDownPoint.x, leftUpPoint.y + DOT_RADIO
                + offsetLeftUpPoint.y);
        Rect rightDownDot = new Rect(rightDownPoint.x - DOT_RADIO
                + offsetRightDownPoint.x, rightDownPoint.y - DOT_RADIO
                + offsetRightDownPoint.y, rightDownPoint.x + DOT_RADIO
                + offsetRightDownPoint.x, rightDownPoint.y + DOT_RADIO
                + offsetRightDownPoint.y);
        switch (eventType) {
        case EVENT_SCALE_LEFTUP:
            canvas.drawBitmap(dotSelected, null, leftUpDot, null);
            canvas.drawBitmap(dot, null, leftDownDot, null);
            canvas.drawBitmap(dot, null, rightUpDot, null);
            canvas.drawBitmap(dot, null, rightDownDot, null);
            break;
        case EVENT_SCALE_LEFTDOWN:
            canvas.drawBitmap(dot, null, leftUpDot, null);
            canvas.drawBitmap(dotSelected, null, leftDownDot, null);
            canvas.drawBitmap(dot, null, rightUpDot, null);
            canvas.drawBitmap(dot, null, rightDownDot, null);
            break;
        case EVENT_SCALE_RIGHTUP:
            canvas.drawBitmap(dot, null, leftUpDot, null);
            canvas.drawBitmap(dot, null, leftDownDot, null);
            canvas.drawBitmap(dotSelected, null, rightUpDot, null);
            canvas.drawBitmap(dot, null, rightDownDot, null);
            break;
        case EVENT_SCALE_RIGHTDOWN:
            canvas.drawBitmap(dot, null, leftUpDot, null);
            canvas.drawBitmap(dot, null, leftDownDot, null);
            canvas.drawBitmap(dot, null, rightUpDot, null);
            canvas.drawBitmap(dotSelected, null, rightDownDot, null);
            break;
        default:
            canvas.drawBitmap(dot, null, leftUpDot, null);
            canvas.drawBitmap(dot, null, leftDownDot, null);
            canvas.drawBitmap(dot, null, rightUpDot, null);
            canvas.drawBitmap(dot, null, rightDownDot, null);
            break;
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        Log.d(TAG, "onDraw");
        drawMei(canvas);
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        Log.d(TAG, "onTouchEvent");
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            // move
            if (leftUpPoint.x < x && x < rightDownPoint.x && leftUpPoint.y < y
                    && y < rightDownPoint.y) {
                lastPoint = new Point(x, y);
                eventType = EVENT_MOVE;
                // leftup
            } else if (Math.abs(x - leftUpPoint.x) < DOT_RADIO
                    && Math.abs(y - leftUpPoint.y) < DOT_RADIO) {
                lastPoint = new Point(x, y);
                eventType = EVENT_SCALE_LEFTUP;
                // leftdown
            } else if (Math.abs(x - leftUpPoint.x) < DOT_RADIO
                    && Math.abs(y - rightDownPoint.y) < DOT_RADIO) {
                lastPoint = new Point(x, y);
                eventType = EVENT_SCALE_LEFTDOWN;
                // rightup
            } else if (Math.abs(x - rightDownPoint.x) < DOT_RADIO
                    && Math.abs(y - leftUpPoint.y) < DOT_RADIO) {
                lastPoint = new Point(x, y);
                eventType = EVENT_SCALE_RIGHTUP;
                // rightdown
            } else if (Math.abs(x - rightDownPoint.x) < DOT_RADIO
                    && Math.abs(y - rightDownPoint.y) < DOT_RADIO) {
                lastPoint = new Point(x, y);
                eventType = EVENT_SCALE_RIGHTDOWN;
            }

        }
            break;
        case MotionEvent.ACTION_MOVE: {
            Log.d("TAG", "ACTION_MOVE");
            switch (eventType) {
            case EVENT_MOVE: {
                offsetLeftUpPoint.x = x - lastPoint.x;
                if (leftUpPoint.x + offsetLeftUpPoint.x < 0) {
                    offsetLeftUpPoint.x = -leftUpPoint.x;
                } else if (leftUpPoint.x + offsetLeftUpPoint.x > this
                        .getWidth() - this.getStaticFrameWidth()) {
                    offsetLeftUpPoint.x = this.getWidth()
                            - this.getStaticFrameWidth() - leftUpPoint.x;
                }

                offsetLeftUpPoint.y = y - lastPoint.y;
                if (leftUpPoint.y + offsetLeftUpPoint.y < 0) {
                    offsetLeftUpPoint.y = -leftUpPoint.y;
                } else if (leftUpPoint.y + offsetLeftUpPoint.y > this
                        .getHeight() - this.getFrameHeight()) {
                    offsetLeftUpPoint.y = this.getHeight()
                            - this.getFrameHeight() - leftUpPoint.y;
                }

                offsetRightDownPoint.x = offsetLeftUpPoint.x;
                offsetRightDownPoint.y = offsetLeftUpPoint.y;
                break;
            }
            case EVENT_SCALE_LEFTUP: {
                offsetLeftUpPoint.x = x - lastPoint.x;
                if (rightDownPoint.x - (leftUpPoint.x + offsetLeftUpPoint.x) < FRAME_MIN_WIDTH) {
                    offsetLeftUpPoint.x = rightDownPoint.x - FRAME_MIN_WIDTH
                            - leftUpPoint.x;
                }
                offsetLeftUpPoint.y = y - lastPoint.y;
                if (rightDownPoint.y - (leftUpPoint.y + offsetLeftUpPoint.y) < FRAME_MIN_HEIGHT) {
                    offsetLeftUpPoint.y = rightDownPoint.y - FRAME_MIN_HEIGHT
                            - leftUpPoint.y;
                }

                offsetRightDownPoint.x = 0;
                offsetRightDownPoint.y = 0;
                break;
            }
            case EVENT_SCALE_LEFTDOWN: {
                offsetLeftUpPoint.x = x - lastPoint.x;
                if (rightDownPoint.x - (leftUpPoint.x + offsetLeftUpPoint.x) < FRAME_MIN_WIDTH) {
                    offsetLeftUpPoint.x = rightDownPoint.x - FRAME_MIN_WIDTH
                            - leftUpPoint.x;
                }
                offsetRightDownPoint.y = y - lastPoint.y;
                if ((rightDownPoint.y + offsetRightDownPoint.y) - leftUpPoint.y < FRAME_MIN_HEIGHT) {
                    offsetRightDownPoint.y = leftUpPoint.y + FRAME_MIN_HEIGHT
                            - rightDownPoint.y;
                }

                offsetLeftUpPoint.y = 0;
                offsetRightDownPoint.x = 0;

                break;
            }
            case EVENT_SCALE_RIGHTUP: {
                offsetRightDownPoint.x = x - lastPoint.x;
                if ((rightDownPoint.x + offsetRightDownPoint.x) - leftUpPoint.x < FRAME_MIN_WIDTH) {
                    offsetRightDownPoint.x = leftUpPoint.x + FRAME_MIN_WIDTH
                            - rightDownPoint.x;
                }
                offsetLeftUpPoint.y = y - lastPoint.y;
                if (rightDownPoint.y - (leftUpPoint.y + offsetLeftUpPoint.y) < FRAME_MIN_HEIGHT) {
                    offsetLeftUpPoint.y = rightDownPoint.y - FRAME_MIN_HEIGHT
                            - leftUpPoint.y;
                }

                offsetLeftUpPoint.x = 0;
                offsetRightDownPoint.y = 0;
                break;
            }
            case EVENT_SCALE_RIGHTDOWN: {
                offsetRightDownPoint.x = x - lastPoint.x;
                if ((rightDownPoint.x + offsetRightDownPoint.x) - leftUpPoint.x < FRAME_MIN_WIDTH) {
                    offsetRightDownPoint.x = FRAME_MIN_WIDTH + leftUpPoint.x
                            - rightDownPoint.x;
                }
                offsetRightDownPoint.y = y - lastPoint.y;
                if ((rightDownPoint.y + offsetRightDownPoint.y) - leftUpPoint.y < FRAME_MIN_HEIGHT) {
                    offsetRightDownPoint.y = FRAME_MIN_HEIGHT + leftUpPoint.y
                            - rightDownPoint.y;
                }

                offsetLeftUpPoint.x = 0;
                offsetLeftUpPoint.y = 0;
                break;
            }
            default:

            }
        }
            break;
        case MotionEvent.ACTION_UP: {
            Log.d("TAG", "ACTION_UP");
            eventType = EVENT_NONE;

            leftUpPoint.x += offsetLeftUpPoint.x;
            leftUpPoint.y += offsetLeftUpPoint.y;
            rightDownPoint.x += offsetRightDownPoint.x;
            rightDownPoint.y += offsetRightDownPoint.y;

            offsetLeftUpPoint.x = 0;
            offsetLeftUpPoint.y = 0;
            offsetRightDownPoint.x = 0;
            offsetRightDownPoint.y = 0;

        }
            break;
        }
        this.invalidate();
        return true;
    }

    private int getDynamicFrameWidth() {
        return (rightDownPoint.x + offsetRightDownPoint.x)
                - (leftUpPoint.x + offsetLeftUpPoint.x);
    }

    private int getDynamicFrameHeight() {
        return (rightDownPoint.y + offsetRightDownPoint.y)
                - (leftUpPoint.y + offsetLeftUpPoint.y);
    }

    private int getStaticFrameWidth() {
        return rightDownPoint.x - leftUpPoint.x;
    }

    private int getFrameHeight() {
        return rightDownPoint.y - leftUpPoint.y;
    }
}


 

 

From:CSDN
 
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()