45.实现自定义View拖拽效果的5种方法

来源:互联网 发布:星你同人之我知你不知 编辑:程序博客网 时间:2024/06/18 05:17

转载请注明出处 http://blog.csdn.net/qq_31715429/article/details/52241694
本文出自:猴菇先生的博客

public class DragView extends View {    private int mLastX;    private int mLastY;    public DragView(Context context) {        super(context);        init();    }    public DragView(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public DragView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    private void init() {        mPaint = new Paint();        mPaint.setStyle(Paint.Style.FILL);        mPaint.setColor(Color.RED);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        //计算宽高        setMeasuredDimension(measureDimension(widthMeasureSpec), measureDimension(heightMeasureSpec));    }    private int measureDimension(int measureSpec) {        int result = 0;        //获得测量模式和大小        int specMode = MeasureSpec.getMode(measureSpec);        int specSize = MeasureSpec.getSize(measureSpec);        if (specMode == MeasureSpec.EXACTLY) {            result = specSize;        } else {            result = 200;//给一个默认值            if (specMode == MeasureSpec.AT_MOST) {                result = Math.min(result, specSize);            }        }        return result;    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //画一个圆        canvas.drawCircle(getWidth() / 2, getHeight() / 2, Math.min(getWidth() / 2, getHeight() / 2), mPaint);    @Override    public boolean onTouchEvent(MotionEvent event) {        int x = (int) event.getX();        int y = (int) event.getY();        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                //记录手指按下时的坐标                mLastX = x;                mLastY = y;                break;            case MotionEvent.ACTION_MOVE:                //计算偏移量                int offsetX = x - mLastX;                int offsetY = y - mLastY;                //重新计算、绘制布局位置                //方法1.                layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);                //以下方法和layout()方法效果一样                //方法2.//                offsetLeftAndRight(offsetX);//                offsetTopAndBottom(offsetY);                //方法3.//                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();//                params.leftMargin = getLeft() + offsetX;//                params.topMargin = getTop() + offsetY;//                setLayoutParams(params);                //方法4.//                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) getLayoutParams();//                params.leftMargin = getLeft() + offsetX;//                params.topMargin = getTop() + offsetY;//                setLayoutParams(params);                //方法5.//                ((View) getParent()).scrollBy(-offsetX, -offsetY);                break;        }        return true;    }}
1 0
原创粉丝点击