美文网首页
自定义画时钟,可移动,放大缩小

自定义画时钟,可移动,放大缩小

作者: f6a40ba32131 | 来源:发表于2016-06-28 10:05 被阅读80次

    效果图:


    11.png
    /**
     * Created by Monica on 2015/10/21.
     */
    public class ClockView extends View implements View.OnTouchListener {
        protected int screenWidth;
        protected int screenHeight;
        protected int lastX;
        protected int lastY;
        private int oriLeft;
        private int oriRight;
        private int oriTop;
        private int oriBottom;
        private int dragDirection;
        private static final int TOP = 0x15;
        private static final int LEFT = 0x16;
        private static final int BOTTOM = 0x17;
        private static final int RIGHT = 0x18;
        private static final int LEFT_TOP = 0x11;
        private static final int RIGHT_TOP = 0x12;
        private static final int LEFT_BOTTOM = 0x13;
        private static final int RIGHT_BOTTOM = 0x14;
        private static final int CENTER = 0x19;
        private int offset = 0;
    
        //边框开关
        private int bk_check;//1选中
        //边框样式
        private String bkImg;
        //边框速度
        private int bkSpeed;
        //边框方向
        private int bkFX;
        private int with;
    
        private int height;
        public boolean move = false;
    
        public boolean allow = true;
    
        public void setMove(boolean move) {
            this.move = move;
        }
    
        public void setViewWidth(int with) {
            this.with = (int)(with*KalerkaApp.px);
        }
    
        public void setViewHeigh(int height) {
            this.height = (int)(height*KalerkaApp.px);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            if(with==0&&height==0){
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }else {
                setMeasuredDimension(with,height);
            }
        }
    
        public void setBk_check(int bk_check) {
            this.bk_check = bk_check;
            invalidate();
        }
    
        public void setBkImg(String bkImg) {
            this.bkImg = bkImg;
            invalidate();
        }
    
        private String bkClass;
    
        public void setBkClass(String bkClass) {
            this.bkClass = bkClass;
            invalidate();
        }
    
        public void setBkSpeed(int bkSpeed) {
            this.bkSpeed = bkSpeed;
            invalidate();
        }
    
        public void setBkFX(int bkFX) {
            this.bkFX = bkFX;
            invalidate();
        }
    
    
        private Paint paint;
    //    private Context context;
    
        //设置时钟的宽高----根据点距的高度来设置
        private int clockHeight = 50;
        private int smallRadius = 4;
        private int bigRadius = 4;
    
        public ClockView(Context context) {
            super(context);
            setOnTouchListener(this);
            initScreenW_H();
        }
    
        /**
         * 初始化获取屏幕宽高
         */
        protected void initScreenW_H() {
            screenHeight = getResources().getDisplayMetrics().heightPixels - 10;
            screenWidth = getResources().getDisplayMetrics().widthPixels;
        }
    
        public void initHeight(int heigh) {
            clockHeight = heigh;
            invalidate();
        }
    
        public ClockView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setOnTouchListener(this);
            initScreenW_H();
        }
    
        public ClockView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setOnTouchListener(this);
            initScreenW_H();
        }
    
        /** 可见屏幕宽度 **/
        public void setScreen_W(int screen_W) {
            this.screenWidth = screen_W;
        }
    
        /** 可见屏幕高度 **/
        public void setScreen_H(int screen_H) {
            this.screenHeight = screen_H;
        }
    
        private int strokeHH = 4;
        private String HHcolor = "red";
        private int strokeMM = 3;
        private String MMcolor = "red";
        private int strokeSS = 2;
        private String SScolor = "red";
    
        private int size369= 3;
        private String color369 = "red";
        private String round369 = "round";
    
        //整点
        private int sizeZD = 2;
        private String colorZD = "red";
        private String roundZD = "round";
    
        private int mx = 0;
        private int my = 0;
    
        public void setHHWidth(int width) {
            this.strokeHH = width;
            invalidate();
        }
    
        public void setHHcolor(String color) {
            this.HHcolor = color;
            invalidate();
        }
    
        public void setMMWidth(int width) {
            this.strokeMM = width;
            invalidate();
        }
    
        public void setMMcolor(String color) {
            this.MMcolor = color;
            invalidate();
        }
    
        public void setSSWidth(int width) {
            this.strokeSS = width;
            invalidate();
        }
    
        public void setSScolor(String color) {
            this.SScolor = color;
            invalidate();
        }
    
        public void setSize369(int size369) {
            this.size369 = size369;
            invalidate();
        }
    
        public void setColor369(String color369) {
            this.color369 = color369;
            invalidate();
        }
    
        public void setRound369(String round369) {
            this.round369 = round369;
            invalidate();
        }
    
        public void setSizeZD(int sizeZD) {
            this.sizeZD = sizeZD;
            invalidate();
        }
    
        public void setColorZD(String colorZD) {
            this.colorZD = colorZD;
            invalidate();
        }
    
        public void setRoundZD(String roundZD) {
            this.roundZD = roundZD;
            invalidate();
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
    
            clockHeight = getWidth()<getHeight()?getWidth():getHeight();
    
            mx = (getWidth() - clockHeight)/2;
    
            my = (getHeight() - clockHeight)/2;
    
            paint = new Paint();
            //消除锯齿
            paint.setAntiAlias(true);
            //设置颜色
            paint.setColor(Color.RED);
            //绘制时钟的表盘
            drawClockPandle(canvas, paint);
            //绘制空心圆
            paint.setStyle(Paint.Style.STROKE);
            //绘制时针、分针、秒针
            drawClockPointer(clockHeight - (size369 > sizeZD ? size369 : sizeZD) * 2, canvas, paint);
            //绘制点击的左上角、右下角的触摸点
            drawClockLine(canvas, paint);
    
    
            if(1==bk_check){
                canvas.save();
                // 画出指定的位图,位图将自动--》缩放/自动转换,以填补目标矩形
                Bitmap up = BitmapFactory.decodeFile(Consts.MY_PATH + "/frame/"+bkClass+"/up_" + bkImg + ".bmp");
                int imgHeight = 1;
                if(up!=null){
                    imgHeight = DensityUtil.dip2px(getContext(), up.getHeight());
                }
                Rect dst = new Rect();// 屏幕 >>目标矩形
                dst.left = 0;
                dst.top = 0;
                dst.right = Math.abs(getWidth());
                dst.bottom = imgHeight;
                if(up!=null){
                    canvas.drawBitmap(up, null, dst, null);
                }
                Bitmap right = BitmapFactory.decodeFile(Consts.MY_PATH + "/frame/"+bkClass+"/right_"+bkImg+".bmp");
                dst.left = getWidth()-imgHeight;
                dst.top = 0;
                dst.right = Math.abs(getWidth());
                dst.bottom = Math.abs(getHeight());
                if(right!=null) {
                    canvas.drawBitmap(right, null, dst, null);
                }
    
                Bitmap down = BitmapFactory.decodeFile(Consts.MY_PATH + "/frame/"+bkClass+"/down_"+bkImg+".bmp");
                dst.left = 0;
                dst.top = Math.abs(getHeight())-imgHeight;
                dst.right = Math.abs(getWidth());
                dst.bottom = Math.abs(getHeight());
                if(down!=null) {
                    canvas.drawBitmap(down, null, dst, null);
                }
    
                Bitmap leftMap = BitmapFactory.decodeFile(Consts.MY_PATH + "/frame/"+bkClass+"/left_"+bkImg+".bmp");
                dst.left = 0;
                dst.top = 0;
                dst.right = imgHeight;
                dst.bottom = Math.abs(getHeight());
                if(leftMap!=null) {
                    canvas.drawBitmap(leftMap, null, dst, null);
                }
    
                canvas.restore();
    
            }
    
            super.onDraw(canvas);
        }
    
        private void drawClockLine(Canvas canvas, Paint paint) {
            paint.setStrokeWidth(2);
    
            //画笔二 --左上
            canvas.save();
            RectF left = new RectF(0, 0, 40, 40);
            canvas.save();
            if(move&&allow){
                paint.setColor(Color.GREEN);
            }else {
                paint.setColor(Color.GRAY);
            }
            paint.setAlpha(50);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(left, paint);
            canvas.restore();
    
            //画笔 -- 右下
            canvas.save();
            RectF bottom = new RectF(getWidth() - 40, getHeight() - 40, getWidth(), getHeight());
            canvas.save();
            if(move&&allow){
                paint.setColor(Color.GREEN);
            }else {
                paint.setColor(Color.GRAY);
            }
            paint.setAlpha(50);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(bottom, paint);
            canvas.restore();
    
            //画笔一
            canvas.save();
            canvas.save();
            if(move&&allow){
                paint.setColor(Color.GREEN);
            }else {
                paint.setColor(Color.GRAY);
            }
            paint.setStrokeWidth(4.0f);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(offset, offset, getWidth() - offset, getHeight()
                    - offset, paint);
            canvas.restore();
        }
    
        /**
         * 绘制时钟的表盘
         * @param canvas
         * @param paint
         */
        private void drawClockPandle(Canvas canvas, Paint paint) {
    
            if(size369>0){
    
                //画四角大圆
                bigRadius  = size369;
                paint.setColor(KalerkaApp.getColorRGB(color369));
                //--顶边中心
                canvas.save();
                canvas.drawCircle(clockHeight / 2 + mx, bigRadius + 2 +my, bigRadius, paint);
                canvas.restore();
    
                //--左边中心
                canvas.save();
                canvas.drawCircle(bigRadius + 2 + mx, clockHeight / 2+my, bigRadius, paint);
                canvas.restore();
    
                //底边中心
                canvas.save();
                canvas.drawCircle(clockHeight / 2+ mx, clockHeight - 2 - bigRadius+my, bigRadius, paint);
                canvas.restore();
    
                //右边中心
                canvas.save();
                canvas.drawCircle(clockHeight-bigRadius-2+ mx, clockHeight / 2+my, bigRadius, paint);
                canvas.restore();
    
            }
    
            if(sizeZD>0) {
    
                int R = clockHeight / 2 - 2 - bigRadius;
    
                //画 1, 2点
    //        x = R*(new BigDecimal(Math.cos(Math.toRadians(90.0 - 6.0 * 1))).setScale(4,   BigDecimal.ROUND_HALF_UP).doubleValue())+clockHeight / 2;
    //        y = R*(new BigDecimal(Math.sin(Math.toRadians(90.0 - 6.0 * 1))).setScale(4,   BigDecimal.ROUND_HALF_UP).doubleValue())+clockHeight / 2;
    
                smallRadius = sizeZD;
                paint.setColor(KalerkaApp.getColorRGB(colorZD));
    
                canvas.save();
                canvas.drawCircle((int) (R * (new BigDecimal(Math.cos(Math.toRadians(90.0 - 30.0 * 1))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + mx,
                        (int) (R * (new BigDecimal(Math.sin(Math.toRadians(90.0 - 30.0 * 1))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + my,
                        smallRadius, paint);
                canvas.restore();
    
                canvas.save();
                canvas.drawCircle((int) (R * (new BigDecimal(Math.cos(Math.toRadians(90.0 - 30.0 * 2))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + mx,
                        (int) (R * (new BigDecimal(Math.sin(Math.toRadians(90.0 - 30.0 * 2))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + my,
                        smallRadius, paint);
                canvas.restore();
    
    
                //画4,5点
                //4 对应 2
                canvas.save();
                canvas.drawCircle((int) (R * (new BigDecimal(Math.cos(Math.toRadians(90.0 - 30.0 * 4))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + mx,
                        (int) (R * (new BigDecimal(Math.sin(Math.toRadians(90.0 - 30.0 * 4))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + my,
                        smallRadius, paint);
                canvas.restore();
    
                canvas.save();
                canvas.drawCircle((int) (R * (new BigDecimal(Math.cos(Math.toRadians(90.0 - 30.0 * 5))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + mx,
                        (int) (R * (new BigDecimal(Math.sin(Math.toRadians(90.0 - 30.0 * 5))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + my,
                        smallRadius, paint);
                canvas.restore();
    
    
                //********************** 左半部 **********************
    
                //画7, 8点
                canvas.save();
                canvas.drawCircle((int) (R * (new BigDecimal(Math.cos(Math.toRadians(90.0 - 30.0 * 7))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + mx,
                        (int) (R * (new BigDecimal(Math.sin(Math.toRadians(90.0 - 30.0 * 7))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + my,
                        smallRadius, paint);
                canvas.restore();
    
                canvas.save();
                canvas.drawCircle((int) (R * (new BigDecimal(Math.cos(Math.toRadians(90.0 - 30.0 * 8))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + mx,
                        (int) (R * (new BigDecimal(Math.sin(Math.toRadians(90.0 - 30.0 * 8))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + my,
                        smallRadius, paint);
                canvas.restore();
    
                //画10.11点
                canvas.save();
                canvas.drawCircle((int) (R * (new BigDecimal(Math.cos(Math.toRadians(90.0 - 30.0 * 10))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + mx,
                        (int) (R * (new BigDecimal(Math.sin(Math.toRadians(90.0 - 30.0 * 10))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + my,
                        smallRadius, paint);
                canvas.restore();
    
                canvas.save();
                canvas.drawCircle((int) (R * (new BigDecimal(Math.cos(Math.toRadians(90.0 - 30.0 * 11))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + mx,
                        (int) (R * (new BigDecimal(Math.sin(Math.toRadians(90.0 - 30.0 * 11))).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()) + clockHeight / 2) + my,
                        smallRadius, paint);
                canvas.restore();
    
            }
    
        }
    
        public class PointBean {
            double x;
            double y;
        }
    
        /**
         * 绘制时钟的指针
         * @param canvas
         * @param paint
         */
        //ClockHalfHeigh 表盘的半径
        //PointWidth 指针的宽度
        //
        //                 p2
        //                /\
        //               /  \
        //          p1  /    \  p3
        //              \    /
        //               \  /
        //                \/
        //                 p4(0,0)
        //
        private void drawClockPointer(int showLength,Canvas canvas, Paint paint) {
            paint.setStyle(Paint.Style.FILL);
            int x = getWidth();
            int y = getHeight();
            int hour;
            int minute;
            int second;
    
            final Calendar calendar = Calendar.getInstance();
            hour = calendar.get(Calendar.HOUR);
            minute = calendar.get(Calendar.MINUTE);
            second = calendar.get(Calendar.SECOND);
    
            float h = ((hour + (float) minute / 60) / 12) * 360;
            float m = ((minute + (float) second / 60) / 60) * 360;
            float s = ((float) second / 60) * 360;
    
            paint.setColor(KalerkaApp.getColorRGB(HHcolor));
            canvas.save(); // 线锁定画布
            canvas.rotate(h, x / 2, y / 2); // 旋转画布
    
            //初始都指向12点
            double px;//指针长度与表盘半径的关系
            //获得时针 1/2
    
            px = 1/(double)2;
    
            PointBean sp1 = new PointBean();
            sp1.x = (x/2 -(strokeHH/(double)2));
            sp1.y = (y/2-strokeHH/(double)2*Math.sqrt(3));
    
            PointBean sp2 = new PointBean();
            sp2.x = x/2;
            sp2.y = y/2-px*showLength/2;
    
            PointBean sp3 = new PointBean();
            sp3.x = (x/2 +(strokeHH/(double)2));
            sp3.y = (y/2-strokeHH/(double)2*Math.sqrt(3));
    
            PointBean sp4 = new PointBean();
            sp4.x = x/2;
            sp4.y = y/2;
    
            //开始画三角形
            Path path1 = new Path();
            path1.moveTo((float) sp1.x, (float) sp1.y);
            path1.lineTo((float) sp2.x, (float) sp2.y);
            path1.lineTo((float) sp3.x, (float) sp3.y);
            path1.close(); //闭合画笔路径
            canvas.drawPath(path1, paint);//开始画
    
            Path path1_1 = new Path();
            path1_1.moveTo((float) sp1.x, (float) sp1.y);
            path1_1.lineTo((float) sp3.x, (float) sp3.y);
            path1_1.lineTo((float) sp4.x, (float) sp4.y);
            path1_1.close(); //闭合画笔路径
            canvas.drawPath(path1_1, paint);//开始画
            canvas.restore();
    
    
            // 分针
            paint.setColor(KalerkaApp.getColorRGB(MMcolor));
            canvas.save(); // 线锁定画布
            canvas.rotate(m, x / 2, y / 2); // 旋转画布
    
            px = 3/(double)4;
    
            PointBean fp1 = new PointBean();
            fp1.x = (x/2 -(strokeMM/(double)2));
            fp1.y = (y/2-strokeMM/(double)2*Math.sqrt(3));
    
            PointBean fp2 = new PointBean();
            fp2.x = x/2;
            fp2.y = y/2-px*showLength/2;
    
            PointBean fp3 = new PointBean();
            fp3.x = (x/2 +(strokeMM/(double)2));
            fp3.y = (y/2-strokeMM/(double)2*Math.sqrt(3));
    
            PointBean fp4 = new PointBean();
            fp4.x = x/2;
            fp4.y = y/2;
    
            //开始画三角形
            Path path2 = new Path();
            path2.moveTo((float) fp1.x, (float) fp1.y);
            path2.lineTo((float) fp2.x, (float) fp2.y);
            path2.lineTo((float) fp3.x, (float) fp3.y);
            path2.close(); //闭合画笔路径
            canvas.drawPath(path2, paint);//开始画
    
            Path path2_1 = new Path();
            path2_1.moveTo((float) fp1.x, (float) fp1.y);
            path2_1.lineTo((float) fp3.x, (float) fp3.y);
            path2_1.lineTo((float) fp4.x, (float) fp4.y);
            path2_1.close(); //闭合画笔路径
            canvas.drawPath(path2_1, paint);//开始画
            canvas.restore();
    
            // 秒针
            paint.setColor(KalerkaApp.getColorRGB(SScolor));
            canvas.save(); // 线锁定画布
            canvas.rotate(s, x / 2, y / 2); // 旋转画布
    
            px = 5/(double)6;
    
            PointBean mp1 = new PointBean();
            mp1.x = (x/2 -(strokeSS/(double)2));
            mp1.y = (y/2-strokeSS/(double)2*Math.sqrt(3));
    
            PointBean mp2 = new PointBean();
            mp2.x = x/2;
            mp2.y = y/2-px*showLength/2;
    
            PointBean mp3 = new PointBean();
            mp3.x = (x/2 +(strokeSS/(double)2));
            mp3.y = (y/2-strokeSS/(double)2*Math.sqrt(3));
    
            PointBean mp4 = new PointBean();
            mp4.x = x/2;
            mp4.y = y/2;
    
            //开始画三角形
            Path path3 = new Path();
            path3.moveTo((float) mp1.x, (float) mp1.y);
            path3.lineTo((float) mp2.x, (float) mp2.y);
            path3.lineTo((float) mp3.x, (float) mp3.y);
            path3.close(); //闭合画笔路径
            canvas.drawPath(path3, paint);//开始画
    
            Path path3_1 = new Path();
            path3_1.moveTo((float) mp1.x, (float) mp1.y);
            path3_1.lineTo((float) mp3.x, (float) mp3.y);
            path3_1.lineTo((float) mp4.x, (float) mp4.y);
            path3_1.close(); //闭合画笔路径
            canvas.drawPath(path3_1, paint);//开始画
            canvas.restore();
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                if(mLocationMovedListener!=null){
                    mLocationMovedListener.onLocationTouch(v);
                }
                oriLeft = v.getLeft();
                oriRight = v.getRight();
                oriTop = v.getTop();
                oriBottom = v.getBottom();
                lastY = (int) event.getRawY();
                lastX = (int) event.getRawX();
                with = oriRight-oriLeft;
                height = oriBottom-oriTop;
                dragDirection = getDirection(v, (int) event.getX(),
                        (int) event.getY());
                move = true;
            }if (action == MotionEvent.ACTION_UP) {
                if(mLocationMovedListener!=null){
                    mLocationMovedListener.onLocationMoved(v,v.getLeft(),v.getTop(),v.getWidth(),v.getHeight());
                }
            }
            // 处理拖动事件
            delDrag(v, event, action);
            invalidate();
            return false;
        }
    
        /**
         * 处理拖动事件
         *
         * @param v
         * @param event
         * @param action
         */
        protected void delDrag(View v, MotionEvent event, int action) {
            switch (action) {
                case MotionEvent.ACTION_MOVE:
                    int dx = (int) event.getRawX() - lastX;
                    int dy = (int) event.getRawY() - lastY;
                    switch (dragDirection) {
                        case LEFT: // 左边缘
                            left(v, dx);
                            break;
                        case RIGHT: // 右边缘
                            right(v, dx);
                            break;
                        case BOTTOM: // 下边缘
                            bottom(v, dy);
                            break;
                        case TOP: // 上边缘
                            top(v, dy);
                            break;
                        case CENTER: // 点击中心-->>移动
                            center(v, dx, dy);
                            break;
                        case LEFT_BOTTOM: // 左下
                            left(v, dx);
                            bottom(v, dy);
                            break;
                        case LEFT_TOP: // 左上
                            left(v, dx);
                            top(v, dy);
                            break;
                        case RIGHT_BOTTOM: // 右下
                            right(v, dx);
                            bottom(v, dy);
                            break;
                        case RIGHT_TOP: // 右上
                            right(v, dx);
                            top(v, dy);
                            break;
                    }
    
                    if (dragDirection != CENTER) {
                        clockHeight = oriBottom - oriTop;
                        with = oriRight-oriLeft;
                        height = oriBottom-oriTop;
                        v.layout(oriLeft, oriTop, oriRight, oriBottom);
                        v.invalidate();
                    }
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_UP:
                    dragDirection = 0;
                    break;
            }
        }
    
        /**
         * 触摸点为中心->>移动
         *
         * @param v
         * @param dx
         * @param dy
         */
        private void center(View v, int dx, int dy) {
            int left = v.getLeft() + dx;
            int top = v.getTop() + dy;
            int right = v.getRight() + dx;
            int bottom = v.getBottom() + dy;
            if (left < -offset) {
                left = -offset;
                right = left + v.getWidth();
            }
            if (right > screenWidth + offset) {
                right = screenWidth + offset;
                left = right - v.getWidth();
            }
            if (top < - offset) {
                top = -offset;
                bottom = top + v.getHeight();
            }
            if (bottom > screenHeight + offset) {
                bottom = screenHeight + offset;
                top = bottom - v.getHeight();
            }
            v.layout(left, top, right, bottom);
        }
    
        /**
         * 触摸点为上边缘
         *
         * @param v
         * @param dy
         */
        private void top(View v, int dy) {
            oriTop += dy;
            if (oriTop < -offset) {
                oriTop = -offset;
            }
            if (oriBottom - oriTop - 2 * offset < 40) {
                oriTop = oriBottom - 2 * offset - 40;
            }
        }
    
        /**
         * 触摸点为下边缘
         *
         * @param v
         * @param dy
         */
        private void bottom(View v, int dy) {
            oriBottom += dy;
            if (oriBottom > screenHeight + offset) {
                oriBottom = screenHeight + offset;
            }
            if (oriBottom - oriTop - 2 * offset < 40) {
                oriBottom = 40 + oriTop + 2 * offset;
            }
        }
    
        /**
         * 触摸点为右边缘
         *
         * @param v
         * @param dx
         */
        private void right(View v, int dx) {
            oriRight += dx;
            if (oriRight > screenWidth + offset) {
                oriRight = screenWidth + offset;
            }
            if (oriRight - oriLeft - 2 * offset < 40) {
                oriRight = oriLeft + 2 * offset + 40;
            }
        }
    
        /**
         * 触摸点为左边缘
         *
         * @param v
         * @param dx
         */
        private void left(View v, int dx) {
            oriLeft += dx;
            if (oriLeft < -offset) {
                oriLeft = -offset;
            }
            if (oriRight - oriLeft - 2 * offset < 40) {
                oriLeft = oriRight - 2 * offset - 40;
            }
        }
    
        /**
         * 获取触摸点flag
         *
         * @param v
         * @param x
         * @param y
         * @return
         */
        protected int getDirection(View v, int x, int y) {
            int left = v.getLeft();
            int right = v.getRight();
            int bottom = v.getBottom();
            int top = v.getTop();
    
    
            if(20>=left&&60>=right){
    
                if (x < 40 && y < 40) {
                    return RIGHT_BOTTOM;
                }
    
            }
    
            if(KalerkaApp.screen_W-60<=left&&KalerkaApp.screen_W-20<=right){
    
                if (right - left - x < 40 && bottom - top - y < 40) {
                    return LEFT_TOP;
                }
    
            }
    
    
            if (x < 40 && y < 40) {
                return LEFT_TOP;
            }
    //        if (y < 40 && right - left - x < 40) {
    //            return RIGHT_TOP;
    //        }
    //        if (x < 40 && bottom - top - y < 40) {
    //            return LEFT_BOTTOM;
    //        }
            if (right - left - x < 40 && bottom - top - y < 40) {
                return RIGHT_BOTTOM;
            }
            if (x < 40) {
                return LEFT;
            }
            if (y < 40) {
                return TOP;
            }
            if (right - left - x < 40) {
                return RIGHT;
            }
            if (bottom - top - y < 40) {
                return BOTTOM;
            }
            return CENTER;
        }
    
        /**
         * 获取截取宽度
         *
         * @return
         */
        public int getCutWidth() {
            return getWidth() - 2 * offset;
        }
    
        /**
         * 获取截取高度
         *
         * @return
         */
        public int getCutHeight() {
            return getHeight() - 2 * offset;
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
        }
    
        private OnLocationMovedListener mLocationMovedListener;
    
        public void setOnLocationMovedListener(OnLocationMovedListener mLocationMovedListener) {
            this.mLocationMovedListener = mLocationMovedListener;
        }
    }
    

    相关文章

      网友评论

          本文标题:自定义画时钟,可移动,放大缩小

          本文链接:https://www.haomeiwen.com/subject/llktjttx.html