Android 微信拍照控件

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;
        }
    }
}

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值