package edu.shu.com.drawcircle;import android.annotation.TargetApi;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.os.Build;import android.util.AttributeSet;import android.util.Log;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import static android.R.attr.x;import static android.R.attr.y;/** * 上海大学 * Created by 王志坚 on 2017/9/7. */public class MyCircle extends View {    private MyCircleCallBack myCircleCallBack;    private String TAG = getClass().getSimpleName();    /**     * 手势     */    private GestureDetector gestureDetector;    /**     * 画圆弧的时间 默认 15 秒     */    private int DRAW_ARC_TIME = 15 * 1000;    /**     * 大圆小圆变化 时间 0.3 秒     */    private int DRAW_SMALL_BIG_CIRCLE_TIME = 300;    /**     * 0 :     * 1:拍照     * 2:录制视频     */    private int statuFlag = 0;    /**     * 原始状态 大圆半径     */    private final int ORGIN_BIG_CIRCLE = 130;    /**     * 原始状态 小圆半径     */    private final int ORGIN_SMALL_CIRCLE = 100;    /**     * 小圆最大半径     */    private int smallCirle_MAX_Radius = ORGIN_BIG_CIRCLE;    /**     * 校园最小半径     */    private int smallCirle_MIX_Radius = ORGIN_SMALL_CIRCLE;    /**     * 当前小圆半径     */    private int smallCirleRadius = 100;    /**     * 大圆的最大半径     */    private int bigCirle_MAX_Radius = 240;    /**     * 大圆的最小半径     */    private int bigCirle_MIX_Radius = 150;    /**     * 当前的大圆半径     */    private int bigCirleRadius = bigCirle_MIX_Radius;    /**     * 当前圆弧的弧度     */    private float mArc = 0;    public MyCircle(Context context) {        super(context);        init();    }    public MyCircle(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public MyCircle(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    @TargetApi(Build.VERSION_CODES.LOLLIPOP)    public MyCircle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);        init();    }    void init() {        gestureDetector = new GestureDetector(getContext(), new MyOnGestureListener());    }    @Override    public void draw(Canvas canvas) {        super.draw(canvas);        //创建画笔        Paint paint = new Paint();        //设置画笔的颜色        paint.setColor(Color.GRAY);        paint.setAntiAlias(true);//        画一个大圆//        if(bigCirle_MAX_Radius == bigCirleRadius){////        }else{////        }        if (bigCirleRadius > bigCirle_MAX_Radius) {            bigCirleRadius = bigCirle_MAX_Radius;        }        canvas.drawCircle(bigCirle_MAX_Radius, bigCirle_MAX_Radius, bigCirleRadius, paint);        //画一个小圆        paint.setColor(Color.WHITE);        if (smallCirleRadius < smallCirle_MIX_Radius) {            smallCirleRadius = smallCirle_MIX_Radius;        }        canvas.drawCircle(bigCirle_MAX_Radius, bigCirle_MAX_Radius, smallCirleRadius, paint);        paint.setAntiAlias(true);//        画圆弧        paint.setColor(Color.GREEN);        paint.setStyle(Paint.Style.STROKE);        float x = 0;        float y = 0;        RectF oval = new RectF(x + 10, y + 10, bigCirle_MAX_Radius * 2 - 10, bigCirle_MAX_Radius * 2 - 10);        paint.setAntiAlias(true);        paint.setStrokeWidth(20);        canvas.drawArc(oval, -90, mArc, false, paint);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        float x = event.getX();        float y = event.getY();        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                statuFlag = 2;//                Log.e(TAG, " x = " + x + "   y = " + y + "  按下");//                startCarmare();                break;            case MotionEvent.ACTION_MOVE://                Log.e(TAG, " x = " + x + "   y = " + y + "  移动");                break;            case MotionEvent.ACTION_UP:            case MotionEvent.ACTION_CANCEL://                Log.e(TAG, " x = " + x + "   y = " + y + "   ACTION_UP");                initCircle();                if(myCircleCallBack!=null){                    myCircleCallBack.done();                }//                Log.e(TAG, " x = " + x + "   y = " + y + "   取消");                initCircle();                break;        }        gestureDetector.onTouchEvent(event);        return true;    }    public void setArc(int arc) {        this.mArc = arc;        if (mArc > 360) {        } else {            postInvalidate();        }    }    private CircleRunnable circleRunnable;    private ArcRunnable arcRunnable;    private void startCarmare() {        //将大圆半径置为最小        bigCirleRadius = bigCirle_MIX_Radius;//                将小圆半径置为最大        smallCirleRadius = smallCirle_MAX_Radius;//        弧度为0        mArc = 0;        if (circleRunnable != null) {            circleRunnable.isDone = true;        }        if (arcRunnable != null) {            arcRunnable.isDone = true;        }        circleRunnable = new CircleRunnable();        new Thread(circleRunnable).start();    }    private class CircleRunnable implements Runnable {        final int SLEEP_TIME = 10;        final float BLOCK = DRAW_SMALL_BIG_CIRCLE_TIME / SLEEP_TIME;        public int count = 0;        public boolean isDone = false;        @Override        public void run() {            while (count < DRAW_SMALL_BIG_CIRCLE_TIME) {                try {                    if (isDone) {                        break;                    }                    count += SLEEP_TIME;                    int curBig = (int) ((bigCirle_MAX_Radius - bigCirle_MIX_Radius) / BLOCK);                    bigCirleRadius += curBig;                    int curSmall = (int) ((smallCirle_MAX_Radius - smallCirle_MIX_Radius) / BLOCK);                    smallCirleRadius -= curSmall;                    postInvalidate();                    Thread.sleep(SLEEP_TIME);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            if (statuFlag == 2 && !isDone) {                if (arcRunnable != null) {                    arcRunnable.isDone = true;                }                arcRunnable = new ArcRunnable();                new Thread(arcRunnable).start();            }        }    }    private class ArcRunnable implements Runnable {        final int SLEEP_TIME = 100;        final float block = DRAW_ARC_TIME / SLEEP_TIME;        public int count = 0;        public boolean isDone = false;        @Override        public void run() {            while (count < DRAW_ARC_TIME) {                try {                    if (isDone) {                        break;                    }//                    Log.e(TAG, "  isDone =" + isDone);                    count += SLEEP_TIME;                    float curArc = 360 / block;                    mArc += curArc;                    postInvalidate();                    Thread.sleep(SLEEP_TIME);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }    /**     * 初始化圆     */    public void initCircle() {        //将大圆半径置为最小        if (circleRunnable != null) {            circleRunnable.isDone = true;        }        if (arcRunnable != null) {            arcRunnable.isDone = true;        }        bigCirleRadius = ORGIN_BIG_CIRCLE;//                将小圆半径置为最大        smallCirleRadius = ORGIN_SMALL_CIRCLE;//        弧度为0        mArc = 0;        postInvalidate();    }    public interface MyCircleCallBack {        void startRecord();        void done();        void TakePhone();    }    private class MyOnGestureListener implements GestureDetector.OnGestureListener {        @Override        public boolean onDown(MotionEvent motionEvent) {//            UtilsLog.e("");            return false;        }        @Override        public void onShowPress(MotionEvent motionEvent) {            if(myCircleCallBack!=null){                myCircleCallBack.TakePhone();            }//            UtilsLog.e("");        }        @Override        public boolean onSingleTapUp(MotionEvent motionEvent) {//            UtilsLog.e("");            return true;        }        @Override        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {//            UtilsLog.e("");            return true;        }        @Override        public void onLongPress(MotionEvent motionEvent) {//            UtilsLog.e("");            statuFlag = 2;            Log.e(TAG, " x = " + x + "   y = " + y + "  按下");            startCarmare();        }        @Override        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {//            UtilsLog.e("");            return true;        }    }}


更多相关文章

  1. Android中drawable使用Shape资源
  2. android shape的使用
  3. Android中Shape 和 Selector的用法
  4. android > 布局文件 > 背景圆角
  5. android之shape使用
  6. XML-SHAPE
  7. Android程序Shape使用总结!
  8. android shape的使用
  9. Android(安卓)Drawable之GradientDrawable

随机推荐

  1. [Android Studio] 使用本地 aar 文件
  2. 在64位Ubuntu上安装Android SDK
  3. Android(安卓)getevent及sendevent分析
  4. Android中SQLite数据库的使用
  5. Android ADK with a standard Arduino Un
  6. Android系统启动流程 -- bootloader
  7. 转:Android——定位和地图
  8. Android(安卓)数据库初窥
  9. android 蓝牙4.0广播功能应用
  10. Android Studio 实时显示布局文件Preview