自定义imageView

来源:互联网 发布:量化分析 java python 编辑:程序博客网 时间:2024/05/16 18:21
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);     //右        }    }}
原创粉丝点击