美文网首页
VideoView 按着转圈,10S 后回调关闭

VideoView 按着转圈,10S 后回调关闭

作者: 勤劳的蚂蚁 | 来源:发表于2020-02-27 14:50 被阅读0次
    小结:
    1. bitmap 获取方式
          Drawable drawable = getDrawable ();
            //获取资源图片
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap ();
    2. Canvas canvas = new Canvas (bitmap); 画板可以对bitmap 进行处理
    3.bitmap 获取最好在 构造函数里面获取,避免在draw 中获取,因为 draw 会多次调用,消耗性能
    4. draw 方法调用时,其是一帧一帧绘制的,如果draw 方法中有的内容相同,也不能在开始的时候初始化,之后不处理,因为那样只会展示最后的绘制图形,前期的绘制会被遮盖。
    5. 获取圆形图形的处理
        public Bitmap getBitmap() {
            Paint paint = new Paint ();
            paint.setAntiAlias (true);
            Drawable drawable = getDrawable ();
            //获取资源图片
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap ();
            //创建空位图 可以565
            Bitmap output = Bitmap.createBitmap (getWidth (), getHeight (), Bitmap.Config.ARGB_8888);
            //创建画板
            Canvas canvas = new Canvas (output);
            //绘制整个画板为透明
            canvas.drawColor (Color.TRANSPARENT);
            paint.setColor (Color.WHITE);
            //绘制圆角图片
            if (type == ROUND) {
                canvas.drawRoundRect (new RectF (0, 0, getWidth (), getHeight ()), mRound, mRound, paint);
            } else {
                //绘制圆形图片
    
                //取view宽高中的小值 尽量保证图片内容的显示
                int minValue = Math.min (getWidth (), getHeight ());
                //设置半径
                mRadius = minValue / 2;
                canvas.drawCircle (mRadius, mRadius, mRadius - ring_padding, paint);
            }
            //设置图形相交模式
            paint.setXfermode (new PorterDuffXfermode (PorterDuff.Mode.SRC_IN));
            Rect src = new Rect (0, 0, bitmap.getWidth (), bitmap.getHeight ());
            Rect dst = new Rect (0, 0, output.getWidth (), output.getHeight ());
            canvas.drawBitmap (bitmap, src, dst, paint);
            return output;
        }
    

    功能描述:有时间限制的触摸按钮
    按钮 中间会变为 圆形图片,按着该按钮 最外层会有 一圈 逐渐充满,充满时间可以设置,如果操过该时间就会回调结束


    1582787372722063.2020-02-27 15_12_03.gif

    定义样式:

      <declare-styleable name="attr_voice_circle">
            <!-- 圆的半径 -->
            <attr name="circle_radio" format="dimension"/>
    
            <!-- 圆环的宽度 -->
            <attr name="ring_width" format="dimension"/>
    
            <!-- 圆环的间距 -->
            <attr name="ring_padding" format="dimension"/>
    
            <!-- 环的颜色 -->
            <attr name="circle_color" format="color"/>
            <attr name="back_ground_color" format="color"/>
            <!--        <attr name="imageType">-->
            <!--            <enum name="circle" value="0"/>-->
            <!--            <enum name="round" value="1"/>-->
            <!--        </attr>-->
            <!--        &lt;!&ndash;角度&ndash;&gt;-->
            <!--        <attr name="imageRound" format="dimension"/>-->
            <!-- 图片背景 -->
    
        </declare-styleable>
    

    主要代码:

    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.PorterDuff;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.Handler;
    import android.os.Message;
    import android.support.annotation.Nullable;
    import android.util.AttributeSet;
    import android.util.TypedValue;
    import android.view.MotionEvent;
    import android.widget.ImageView;
    
    
    
    
    /**
     * 使用Xfermode 设置图片相交模式
     * 简单说呢 在一张画布上画了两张图片 这两张图的以怎样的方式显示出来 例如:只显示上层图片,只显示下层图片 ,显示两张图的交集部分 等等等
     * <p>
     * 实现思路
     * <p>
     * 创建一个空bitmap 根据这个bitmap创建一个Canvas
     * 设置Canvas透明 画一个想要实现的形状
     * 设置图形相交模式
     * 获取图片资源 绘制到Canvas
     * <p>
     * <p>
     * PorterDuff.Mode.CLEAR 清除画布上图像
     * PorterDuff.Mode.SRC 显示上层图像
     * PorterDuff.Mode.DST 显示下层图像
     * PorterDuff.Mode.SRC_OVER上下层图像都显示,上层居上显示
     * PorterDuff.Mode.DST_OVER 上下层都显示,下层居上显示
     * PorterDuff.Mode.SRC_IN 取两层图像交集部分只显示上层图像
     * PorterDuff.Mode.DST_IN 取两层图像交集部分,只显示下层图像
     * PorterDuff.Mode.SRC_OUT 取上层图像非交集部分
     * PorterDuff.Mode.DST_OUT 取下层图像非交集部分
     * PorterDuff.Mode.SRC_ATOP 取下层图像非交集部分与上层图像交集部分
     * PorterDuff.Mode.DST_ATOP 取上层图像非交集部分与下层图像交集部分
     * PorterDuff.Mode.XOR 取两层图像的非交集部分
     */
    
    @SuppressLint("AppCompatCustomView")
    public class VoiceView extends ImageView {
    
        private static final String TAG = VoiceView.class.getSimpleName ();
        /**
         * 录制最长时间 秒
         */
        private static final int RECORD_DEFAULT_MAX_TIME = 10;
        /**
         * 录制最小时间 秒
         */
        private final int RECORD_DEFAULT_MIN_TIME = 1;
        /**
         * 视频录制内圆半径
         */
        private final float RECORD_DEFAULT_INNER_CIRCLE_RADIUS = 5f;
        private final int CIRCLE_RADIO = 50;//dp 圆环半径
    
        private final int CIRCLE = 0;//圆形
        private final int ROUND = 1;//圆角
    
        private int ring_width = 0;//环的宽度
        private int ring_padding = 0;//环的间距
        private Drawable back_ground;//背景图片
        private Paint mPaint;
        private Paint mPaintColor;
        private Paint backgroundcolor;
        private Matrix matrix;
        private int type = CIRCLE;// 默认为圆形
        private int mRound;// 角度
        private int mRadius;// 设置半径
        private int back_ground_color;
        //画进度条
        private float mProgressValue = 0f;//度数
        private long mRecordTime = 0;
        private boolean mIsStartRecord;
        private int num;
    
        private Handler mHandler = new Handler () {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage (msg);
    
                if (!mIsStartRecord) {
                    return;
                }
                num++;
                int actTime = (int) ((System.currentTimeMillis () - mRecordTime) / 1000);
    
                mProgressValue = 36f / RECORD_DEFAULT_MAX_TIME * num + mProgressValue;
    
                //当没有达到最大值时一直绘制
                if (actTime < RECORD_DEFAULT_MAX_TIME) {
                    mHandler.sendEmptyMessageDelayed (0, 100);
                } else {//录制完成 code
                    if (listener != null) {
                        listener.endRecordTime (actTime);
                    }
                }
                postInvalidate ();
            }
        };
    
        //重新该方法来完成触摸时,圆变大的效果
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction ()) {
                case MotionEvent.ACTION_DOWN:
                    mIsStartRecord = true;
                    mRecordTime = System.currentTimeMillis ();
                    mHandler.sendEmptyMessage (0);
                    //这里可以回调出去表示已经开始录制了
                    //                //code.....
                    if(listener!=null){
                        listener.startRecordTime (0)  ;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (mRecordTime > 0) {
                        //录制的时间(单位:秒)
                        int actualRecordTime = (int) ((System.currentTimeMillis () - mRecordTime) / 1000);
                        //这里回调出去表示已经取消录制了
                        //code.....
                        if (listener != null) {
                            listener.endRecordTime (actualRecordTime);
                        }
                    }
                    mHandler.removeMessages (0);
                    mIsStartRecord = false;
                    mRecordTime = 0;
                    mProgressValue = 0;
                    num = 0;
                    postInvalidate ();
                    break;
                case MotionEvent.ACTION_CANCEL:
                    //这里可以回调出去表示已经取消录制了
                    //code.....
                    if (mRecordTime > 0) {
                        //录制的时间(单位:秒)
                        int actualRecordTime = (int) ((System.currentTimeMillis () - mRecordTime) / 1000);
                        //这里回调出去表示已经取消录制了
                        //code.....
                        if (listener != null) {
                            listener.endRecordTime (actualRecordTime);
                        }
                    }
                    mHandler.removeMessages (0);
                    mIsStartRecord = false;
                    mRecordTime = 0;
                    mProgressValue = 0;
                    num = 0;
                    postInvalidate ();
                    break;
            }
    
            return true;
        }
    
        public VoiceView(Context context) {
            this (context, null);
        }
    
        public VoiceView(Context context, @Nullable AttributeSet attrs) {
            this (context, attrs, 0);
        }
    
        public VoiceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super (context, attrs, defStyleAttr);
            init (context, attrs);
        }
    
        private void init(Context context, AttributeSet attrs) {
    
            TypedArray a = context.obtainStyledAttributes (attrs, R.styleable.attr_voice_circle);
    //         circle_radio=dip2px(context,a.getDimension (R.styleable.attr_voice_circle_circle_radio,CIRCLE_RADIO));
            ring_width = a.getDimensionPixelSize (R.styleable.attr_voice_circle_ring_width,
                    (int) TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_DIP, 10, getResources ().getDisplayMetrics ()));
            ring_padding = a.getDimensionPixelSize (R.styleable.attr_voice_circle_ring_padding,
                    (int) TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_DIP, 10, getResources ().getDisplayMetrics ()));
    
            back_ground_color = a.getColor (R.styleable.attr_voice_circle_back_ground_color, getResources ().getColor (
            android.R.color.transparent));
    
            a.recycle ();
            mPaint = new Paint ();
            mPaint.setAntiAlias (true);//抗锯齿 ,边距较不模糊
            mPaint.setDither (true);//设置防抖动,图片展示较为柔和
            matrix = new Matrix ();
    //      圆弧的画笔
            mPaintColor = new Paint ();
            mPaintColor.setAntiAlias (true);//取消锯齿
            mPaintColor.setStyle (Paint.Style.STROKE);//设置画圆弧的画笔的属性为描边(空心),个人喜欢叫它描边,叫空心有点会引起歧义
            mPaintColor.setStrokeWidth (ring_width);
            mPaintColor.setColor (getResources ().getColor (R.color.colorff5722));
    //      画背景
            backgroundcolor = new Paint ();
            backgroundcolor.setAntiAlias (true);//取消锯齿
            backgroundcolor.setStyle (Paint.Style.FILL);//设置充满模式
            backgroundcolor.setColor (back_ground_color);
        }
    
        //    绘制图片
        @Override
        protected void onDraw(Canvas canvas) {
    //       super.onDraw (canvas);
            if (getDrawable () == null) {
    
                return;
            }
            // 画背景
            RectF oval1 = new RectF (0, 0,
                    getWidth (), getHeight ());
            canvas.drawRect (oval1, backgroundcolor);
    //       画圆
            Bitmap bitmap = getBitmap ();
            canvas.drawBitmap (bitmap, 0, 0, mPaint);
    
    
    //        画圆弧 进度条
            RectF oval = new RectF (0 + ring_width, 0 + ring_width,
                    getWidth () - ring_width, getHeight () - ring_width);
            canvas.drawArc (oval, -90, mProgressValue, false, mPaintColor);
    
        }
    
        /**
         * dp转px
         *
         * @param context
         * @return
         */
        public static int dip2px(Context context, float dpValue) {
            final float scale = context.getResources ().getDisplayMetrics ().density;
            return (int) (dpValue * scale + 0.5f);
        }
    
        public Bitmap getBitmap() {
            Paint paint = new Paint ();
            paint.setAntiAlias (true);
            Drawable drawable = getDrawable ();
            //获取资源图片
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap ();
            //创建空位图
            Bitmap output = Bitmap.createBitmap (getWidth (), getHeight (), Bitmap.Config.ARGB_8888);
            //创建画板
            Canvas canvas = new Canvas (output);
            //绘制整个画板为透明
            canvas.drawColor (Color.TRANSPARENT);
            paint.setColor (Color.WHITE);
            //绘制圆角图片
            if (type == ROUND) {
                canvas.drawRoundRect (new RectF (0, 0, getWidth (), getHeight ()), mRound, mRound, paint);
            } else {
                //绘制圆形图片
    
                //取view宽高中的小值 尽量保证图片内容的显示
                int minValue = Math.min (getWidth (), getHeight ());
                //设置半径
                mRadius = minValue / 2;
                canvas.drawCircle (mRadius, mRadius, mRadius - ring_padding, paint);
            }
            //设置图形相交模式
            paint.setXfermode (new PorterDuffXfermode (PorterDuff.Mode.SRC_IN));
            Rect src = new Rect (0, 0, bitmap.getWidth (), bitmap.getHeight ());
    //            Rect dst=new Rect(0+ring_width,0+ring_width,output.getWidth()-ring_width,output.getHeight()-ring_width);
            Rect dst = new Rect (0, 0, output.getWidth (), output.getHeight ());
            canvas.drawBitmap (bitmap, src, dst, paint);
            return output;
        }
    
    
        public void setTimeListener(RecordTimeListener listener) {
            this.listener = listener;
        }
    
        public RecordTimeListener listener;
    
        public interface RecordTimeListener {
            float endRecordTime(int time);
            float startRecordTime(int time);
        }
    
    }
    
    

    补充: ui 给了圆形图,可以直接画上去

    
    
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.PorterDuff;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.Handler;
    import android.os.Message;
    import android.support.annotation.Nullable;
    import android.util.AttributeSet;
    import android.util.TypedValue;
    import android.view.MotionEvent;
    import android.widget.ImageView;
    
    import com.txx.app.main.R;
    import com.txx.app.main.commom.ui.MyLog;
    
    
    /**
     * 使用Xfermode 设置图片相交模式
     * 简单说呢 在一张画布上画了两张图片 这两张图的以怎样的方式显示出来 例如:只显示上层图片,只显示下层图片 ,显示两张图的交集部分 等等等
     * <p>
     * 实现思路
     * <p>
     * 创建一个空bitmap 根据这个bitmap创建一个Canvas
     * 设置Canvas透明 画一个想要实现的形状
     * 设置图形相交模式
     * 获取图片资源 绘制到Canvas
     * <p>
     * <p>
     * PorterDuff.Mode.CLEAR 清除画布上图像
     * PorterDuff.Mode.SRC 显示上层图像
     * PorterDuff.Mode.DST 显示下层图像
     * PorterDuff.Mode.SRC_OVER上下层图像都显示,上层居上显示
     * PorterDuff.Mode.DST_OVER 上下层都显示,下层居上显示
     * PorterDuff.Mode.SRC_IN 取两层图像交集部分只显示上层图像
     * PorterDuff.Mode.DST_IN 取两层图像交集部分,只显示下层图像
     * PorterDuff.Mode.SRC_OUT 取上层图像非交集部分
     * PorterDuff.Mode.DST_OUT 取下层图像非交集部分
     * PorterDuff.Mode.SRC_ATOP 取下层图像非交集部分与上层图像交集部分
     * PorterDuff.Mode.DST_ATOP 取上层图像非交集部分与下层图像交集部分
     * PorterDuff.Mode.XOR 取两层图像的非交集部分
     */
    
    @SuppressLint("AppCompatCustomView")
    public class VoiceView extends ImageView {
    
        private static final String TAG = VoiceView.class.getSimpleName ();
        /**
         * 录制最长时间 秒
         */
        private static final int RECORD_DEFAULT_MAX_TIME = 100;//0.1 s
        /**
         * 录制最小时间 秒
         */
        private final int RECORD_DEFAULT_MIN_TIME = 1;
        /**
         * 视频录制内圆半径
         */
        private final float RECORD_DEFAULT_INNER_CIRCLE_RADIUS = 5f;
        private final int CIRCLE_RADIO = 50;//dp 圆环半径
    
        private final int CIRCLE = 0;//圆形
        private final int ROUND = 1;//圆角
    
        private int ring_width = 0;//环的宽度
        private int ring_padding = 0;//环的间距
        private Drawable back_ground;//背景图片
        private Paint mPaint;
        private Paint mPaintColor;
        private Paint bgPaint;
        private Paint backgroundcolor;
        private Matrix matrix;
        private int type = CIRCLE;// 默认为圆形
        private int mRound;// 角度
        private int mRadius;// 设置半径
        private int back_ground_color;
        //画进度条
        private float mProgressValue = 0f;//度数
        private long mRecordTime = 0;
        private boolean mIsStartRecord;
        private int num;
        private int countTime;
    
        private Handler mHandler = new Handler () {
            @Override
            public void handleMessage(Message msg) {
    
                switch (msg.what){
                    case 0:
    
                        if (!mIsStartRecord) {
                            return;
                        }
                        num++;
                        MyLog.wtf ("hyl num",num+"");
                        int actTime = (int) ((System.currentTimeMillis () - mRecordTime) / 1000);
    
                        mProgressValue = (float) (360*num /100  );
                        MyLog.wtf ("hyl mProgressValue :",mProgressValue+"");
                        //当没有达到最大值时一直绘制
                        if (actTime < RECORD_DEFAULT_MAX_TIME) {
                            mHandler.sendEmptyMessageDelayed (0, 100);
                        } else {//录制完成 code
                            if (listener != null) {
                                listener.endRecordTime (actTime);
                            }
                        }
                        postInvalidate ();
                        break;
    
                    case 101:
                        if(countTime<(11)){
                            countTime++;
                            MyLog.wtf ("hyl countTime",countTime+"");
                            mHandler.sendEmptyMessageDelayed (101,1000);
    
                        }else {
                            //code.....
                            if (listener != null) {
                                listener.endRecordTime (10);
                            }
                            countTime=0;//重置
                        }
    
    
                        break;
    
                }
                super.handleMessage (msg);
            }
        };
    
        //重新该方法来完成触摸时,圆变大的效果
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction ()) {
                case MotionEvent.ACTION_DOWN:
                    mIsStartRecord = true;
                    mRecordTime = System.currentTimeMillis ();
                    num=0;
                    mHandler.sendEmptyMessage (0);
                    //这里可以回调出去表示已经开始录制了
                    //                //code.....
                    if(listener!=null){
                        listener.startRecordTime (0)  ;
                    }
    
                    countTime=0;
                    mHandler.sendEmptyMessageDelayed (101,1000);
    
                    break;
                case MotionEvent.ACTION_UP:
                    if (mRecordTime > 0) {
                        //录制的时间(单位:秒)
                        int actualRecordTime = (int) ((System.currentTimeMillis () - mRecordTime) / 1000);
                        //这里回调出去表示已经取消录制了
                        //code.....
                        if (listener != null) {
                            listener.endRecordTime (actualRecordTime);
                        }
                    }
                    mHandler.removeMessages (0);
                    mHandler.removeMessages (101);
                    mIsStartRecord = false;
                    mRecordTime = 0;
                    mProgressValue = 0;
                    postInvalidate ();
                    break;
                case MotionEvent.ACTION_CANCEL:
                    //这里可以回调出去表示已经取消录制了
                    //code.....
                    if (mRecordTime > 0) {
                        //录制的时间(单位:秒)
                        int actualRecordTime = (int) ((System.currentTimeMillis () - mRecordTime) / 1000);
                        //这里回调出去表示已经取消录制了
                        //code.....
                        if (listener != null) {
                            listener.endRecordTime (actualRecordTime);
                        }
                    }
                    mHandler.removeMessages (0);
                    mHandler.removeMessages (101);
                    mIsStartRecord = false;
                    mRecordTime = 0;
                    mProgressValue = 0;
                    postInvalidate ();
                    break;
            }
    
            return true;
        }
    
        public VoiceView(Context context) {
            this (context, null);
        }
    
        public VoiceView(Context context, @Nullable AttributeSet attrs) {
            this (context, attrs, 0);
        }
    
        public VoiceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super (context, attrs, defStyleAttr);
            init (context, attrs);
        }
    
        private void init(Context context, AttributeSet attrs) {
    
            TypedArray a = context.obtainStyledAttributes (attrs, R.styleable.attr_voice_circle);
    //         circle_radio=dip2px(context,a.getDimension (R.styleable.attr_voice_circle_circle_radio,CIRCLE_RADIO));
            ring_width = a.getDimensionPixelSize (R.styleable.attr_voice_circle_ring_width,
                    (int) TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_DIP, 10, getResources ().getDisplayMetrics ()));
            ring_padding = a.getDimensionPixelSize (R.styleable.attr_voice_circle_ring_padding,
                    (int) TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_DIP, 10, getResources ().getDisplayMetrics ()));
    
            back_ground_color = a.getColor (R.styleable.attr_voice_circle_back_ground_color, getResources ().getColor (
            android.R.color.transparent));
    
            a.recycle ();
            mPaint = new Paint ();
            mPaint.setAntiAlias (true);//抗锯齿 ,边距较不模糊
            mPaint.setDither (true);//设置防抖动,图片展示较为柔和
            matrix = new Matrix ();
    //      圆弧的画笔
            mPaintColor = new Paint ();
            mPaintColor.setAntiAlias (true);//取消锯齿
            mPaintColor.setStyle (Paint.Style.STROKE);//设置画圆弧的画笔的属性为描边(空心),个人喜欢叫它描边,叫空心有点会引起歧义
            mPaintColor.setStrokeWidth (ring_width);
            mPaintColor.setColor (getResources ().getColor (R.color.colorf9b864));
    //      画背景
            backgroundcolor = new Paint ();
            backgroundcolor.setAntiAlias (true);//取消锯齿
            backgroundcolor.setStyle (Paint.Style.FILL);//设置充满模式
            backgroundcolor.setColor (back_ground_color);
            //      画外圈背景
            bgPaint = new Paint ();
            bgPaint.setAntiAlias (true);//取消锯齿
            bgPaint.setAntiAlias (true);//取消锯齿
            bgPaint.setStyle (Paint.Style.STROKE);//设置画圆弧的画笔的属性为描边(空心),个人喜欢叫它描边,叫空心有点会引起歧义
            bgPaint.setStrokeWidth (ring_width);
            bgPaint.setColor (getResources ().getColor (R.color.colorefefef));
        }
    
        //    绘制图片
        @Override
        protected void onDraw(Canvas canvas) {
    //       super.onDraw (canvas);
            if (getDrawable () == null) {
    
                return;
            }
            // 画背景
            RectF oval1 = new RectF (0, 0,
                    getWidth (), getHeight ());
            canvas.drawRect (oval1, backgroundcolor);
    //       画圆
            Bitmap bitmap = getBitmap ();
    //        canvas.drawBitmap (bitmap, 0, 0, mPaint);
            canvas.drawBitmap (bitmap, ring_padding, ring_padding, mPaint);
    
    
            if(mIsStartRecord){//
                //        画圆弧 进度条
                RectF bgoval = new RectF (0 + ring_width, 0 + ring_width,
                        getWidth () - ring_width, getHeight () - ring_width);
                canvas.drawArc (bgoval, -90, 360, false, bgPaint);
    
            }
    
    //        画圆弧 进度条
            RectF oval = new RectF (0 + ring_width, 0 + ring_width,
                    getWidth () - ring_width, getHeight () - ring_width);
            canvas.drawArc (oval, -90, mProgressValue, false, mPaintColor);
    
        }
    
        /**
         * dp转px
         *
         * @param context
         * @return
         */
        public static int dip2px(Context context, float dpValue) {
            final float scale = context.getResources ().getDisplayMetrics ().density;
            return (int) (dpValue * scale + 0.5f);
        }
    
        public Bitmap getBitmap() {
            Paint paint = new Paint ();
            paint.setAntiAlias (true);
            Drawable drawable = getDrawable ();
            //获取资源图片
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap ();
            Bitmap output;
            if(type == ROUND){
                //创建空位图
                 output = Bitmap.createBitmap (getWidth (), getHeight (), Bitmap.Config.ARGB_8888);
    
            }else {
                //创建空位图
                 output = Bitmap.createBitmap (getWidth ()-2*ring_padding, getHeight ()-2*ring_padding, Bitmap.Config.ARGB_8888);
            }
           //创建画板
            Canvas canvas = new Canvas (output);
            //绘制整个画板为透明
            canvas.drawColor (Color.TRANSPARENT);
            paint.setColor (Color.WHITE);
            //绘制圆角图片
            if (type == ROUND) {
                canvas.drawRoundRect (new RectF (0, 0, getWidth (), getHeight ()), mRound, mRound, paint);
            } else {
                //绘制圆形图片
    
                //取view宽高中的小值 尽量保证图片内容的显示
                int minValue = Math.min (getWidth (), getHeight ());
                //设置半径
                mRadius = minValue / 2;
                canvas.drawCircle (mRadius, mRadius, mRadius - ring_padding, paint);
            }
            //设置图形相交模式
            //paint.setXfermode (new PorterDuffXfermode (PorterDuff.Mode.SRC_IN));
           // Rect src = new Rect (0, 0, bitmap.getWidth (), bitmap.getHeight ());
    //            Rect dst=new Rect(0+ring_width,0+ring_width,output.getWidth()-ring_width,output.getHeight()-ring_width);
           // Rect dst = new Rect (0, 0, output.getWidth (), output.getHeight ());
            //canvas.drawBitmap (bitmap, src, dst, paint);
            canvas.drawBitmap (bitmap,0,0,paint);
            return output;
        }
    
    
        public void setTimeListener(RecordTimeListener listener) {
            this.listener = listener;
        }
    
        public RecordTimeListener listener;
    
        public interface RecordTimeListener {
            float endRecordTime(int time);
            float startRecordTime(int time);
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:VideoView 按着转圈,10S 后回调关闭

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