自定義imageView

package hghl.pdf_test.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import hghl.pdf_test.R;

/**
 * Created by hghl on 2017/8/15.
 */

public class MyView extends View {
    private int mBitWidth = 0,  mBitHeight = 0;
    private Resources mResources;
    private Paint mBitPaint;
    private Bitmap mBitmap;
    private Rect mSrcRect, mDestRect;
    private int mTotalWidth = 0, mTotalHeight = 0;

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

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mResources = getResources();
        mBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.beautiful_girl)).getBitmap();
        mBitWidth =  mBitmap.getWidth();
        mBitHeight =  mBitmap.getHeight();

        mBitPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitPaint.setFilterBitmap(true);
        mBitPaint.setDither(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMySize(100, widthMeasureSpec);
        int height = getMySize(100, heightMeasureSpec);

        Log.e("SDVLDSNVKDSVDS", "==width===" + width + "====height=======" + height);
//        if (width < height) {
//            height = width;
//        } else {
//            width = height;
//        }
        setMeasuredDimension(width, height);
    }

    /**
     *  mSrcRect 截取圖片尺寸大小
     *  mDestRect 圖片存放位置及大小
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mBitmap, mSrcRect, mDestRect, mBitPaint);
    }

    private int getMySize(int defaultSize, int measureSpec) {
        int mySize = defaultSize;

        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        switch (mode) {
            case MeasureSpec.UNSPECIFIED: {//如果沒有指定大小,就設置爲默認大小
                Log.e("SLDVNSDFVBDF", "=====UNSPECIFIED========");
                mySize = defaultSize;
                break;
            }
            case MeasureSpec.AT_MOST: {//如果測量模式是最大取值爲size
                Log.e("SLDVNSDFVBDF", "=====AT_MOST========");
                //我們將大小取最大值,你也可以取其他值
                mySize = size;
                break;
            }
            case MeasureSpec.EXACTLY: {//如果是固定的大小,那就不要去改變它
                Log.e("SLDVNSDFVBDF", "=====EXACTLY========");
                mySize = size;
                break;
            }
        }
        return mySize;
    }

    private int picWidth = 0, picHeight = 0, widthLength = 0, heightLength = 0;
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w;
        mTotalHeight = h;
        picWidth = Math.round(mTotalWidth * 1 / 3f);
        picHeight = Math.round(mTotalHeight * 1 / 3f);
        widthLength = Math.round((mTotalWidth - picWidth) / 2.0f);
        heightLength = Math.round((mTotalHeight - picHeight) / 2.0f);

        /**
         *   原圖片需要繪製區域     取 mBitWidth、mBitHeight 表示繪製原圖片全部區域,
         *   mBitWidth , mBitHeight/2 表示繪製只繪製圖片上半截 (new Rect( 0, 0, mBitWidth , mBitHeight/2));
         *   同理mBitWidth/2 , mBitHeight 表示繪製只繪製圖片左邊半截半截(new Rect( 0, 0, mBitWidth/2 , mBitHeight));
         */
        mSrcRect = new Rect( 0, 0, mBitWidth , mBitHeight);
        mDestRect = new Rect( widthLength, heightLength, (mTotalWidth - picWidth) , (mTotalHeight - picHeight));        //繪製drawable的區域

    }

    /**
     * 位置移動
     * @param x
     * @param y
     */
    public void changePosition( int x, int y) {
        int  currentLeft =  x - pressPointX;
        int  currentTop = y - pressPointY;

        if (mDestRect == null) {
            mDestRect = new Rect(currentLeft, currentTop, currentLeft + picWidth,
                    currentTop + picHeight);
        }
        mDestRect.left = currentLeft;
        mDestRect.right = currentLeft + picWidth;
        mDestRect.top = currentTop;
        mDestRect.bottom = currentTop + picHeight;
    }


    /**
     * 左右移動
     * @param x
     * @param y
     * @param b true 爲左邊界移動, false爲 右邊界移動
     */
    public void changeLeft(int x, int y, boolean b) {
        int  currentLeft = x - Math.round(picWidth/2.0f);
        int  currentTop = y - Math.round(picHeight/2.0f);

        if (mDestRect == null) {
            mDestRect = new Rect(currentLeft, currentTop, currentLeft + picWidth,
                    currentTop + picHeight);
        }
        if (b) {
            mDestRect.left = x;
        } else {
            mDestRect.right = x;
        }
    }

    /**
     * 上下移動
     * @param x
     * @param y
     * @param b true上邊界移動, false 爲下邊界移動
     */
    public void changeTop(int x, int y, boolean b) {
        int  currentLeft = x - Math.round(picWidth/2.0f);
        int  currentTop = y - Math.round(picHeight/2.0f);

        if (mDestRect == null) {
            mDestRect = new Rect(currentLeft, currentTop, currentLeft + picWidth,
                    currentTop + picHeight);
        }
        if (b) {
            mDestRect.top = y;
        } else {
            mDestRect.bottom = y;
        }
    }

    private boolean isMove = false;
    private int pressPointX = 0, pressPointY = 0 ;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = Math.round(event.getX());
        int y = Math.round(event.getY());

        int percentWidth4 = Math.round(picWidth / 4.0f);
        int percentHeight4 = Math.round(picHeight / 4.0f);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isMove = false;
                pressPointX = x - mDestRect.left;
                pressPointY = y - mDestRect.top;
                if (x > mDestRect.left && x < mDestRect.right && y < mDestRect.bottom && y > mDestRect.top) {
                    isMove = true;
                }
                break;
            default:
                break;
        }

        if (!isMove) {
            if (y < (mDestRect.top + percentHeight4)) {
                changeTop( x,  y, true);     //上
            } else if ( y > (mDestRect.bottom - percentHeight4)){
                changeTop( x,  y, false);     //下
            }
            isTouchInItem(x, y, percentWidth4);
        } else{
            changePosition( x,  y);     //  移動位置
        }
        picWidth = mDestRect.right - mDestRect.left;
        picHeight = mDestRect.bottom - mDestRect.top;
        postInvalidate();// 重繪
        return true;
    }

    private void isTouchInItem(int x, int y, int percentWidth4){
        if (x < (mDestRect.left + percentWidth4)) {
            changeLeft( x,  y, true);     //左
        } else if (x > (mDestRect.right - percentWidth4)){
            changeLeft( x,  y, false);     //右
        }
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章