zoukankan      html  css  js  c++  java
  • 自定义view圆环的改变

     //次线程更新ui
        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                invalidate();
                if (du>=360){
                    timer.cancel();
                }
            }
        };
        Timer timer = null;
        int outColor = Color.GRAY;
        float du = 0;
        public MyView(Context context) {
            super(context);
        }

        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        @Override
        protected void onCreateContextMenu(ContextMenu menu) {
            super.onCreateContextMenu(menu);
        }

        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            //最上面的字
            Paint p1 = new Paint();
            p1.setColor(Color.BLACK);
            p1.setTextSize(32);
            canvas.drawText("改变外层圆环的颜色",0,32,p1);

            //最外面的园
            Paint p2 = new Paint();
            p2.setColor(outColor);
            canvas.drawCircle(getWidth()/2,getHeight()/2,100,p2);

            //进度
            Paint p5 = new Paint();
            p5.setColor(Color.RED);
            RectF rectF = new RectF(getWidth()/2-100,getHeight()/2-100,getWidth()/2+100,getHeight()/2+100);
            canvas.drawArc(rectF,0,du,true,p5);

            //中心的白色园
            Paint p3 = new Paint();
            p3.setColor(Color.WHITE);
            canvas.drawCircle(getWidth()/2,getHeight()/2,50,p3);

            //中间的文字
            Paint p4 = new Paint();
            p4.setColor(Color.BLACK);
            canvas.drawText("78%",getWidth()/2,getHeight()/2,p4);

            //开始
            Paint p6 = new Paint();
            p6.setColor(Color.BLACK);
            p6.setTextSize(32);
            canvas.drawText("开始",0,getHeight()-100,p6);

            //重置
            Paint p7 = new Paint();
            p7.setColor(Color.BLACK);
            p7.setTextSize(32);
            canvas.drawText("重置",getWidth()/2,getHeight()-100,p6);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                if (event.getY() <= 32) {//更改上面的文字为绿色
                    outColor = Color.GREEN;
                    invalidate();
                }
                if (event.getX() < getWidth()/2 && event.getY()<getHeight()-100 && event.getY()>getHeight()-132){
                    //Log.d("mylog","开始");
                    //点击开始后启动timer不断的给du进行++
                    timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            du += 12;
                            Log.d("mylog","度数:"+du);
                            handler.sendEmptyMessage(1);
                        }
                    },1000,1000);
                }
                if (event.getX() >= getWidth()/2 && event.getY()<getHeight()-100 && event.getY()>getHeight()-132){
                    //Log.d("mylog","重置");
                    //重置,把所有的变量回归初始值,更新界面,关闭timer
                    du = 0;
                    outColor = Color.GRAY;
                    invalidate();
                    timer.cancel();
                }
            }
            return super.onTouchEvent(event);
        }
    }

  • 相关阅读:
    注意 定义类的时候 不要null 别忘了new 实例化 否则报错
    如果通过key获取dictionary里面的value
    C#邮箱身份验证
    时间选择器 可以选择日期和时间
    C#的Timer解析
    使用微软的组件发邮件
    将html代码写入临时文件夹下面的 然后发来给webbrowser使用
    c#中DataGridView 如何设置 才能选中一行 设置鼠标事件
    lumisoft 获取邮件的方法
    C# 动态添加CheckedListBox的选项,并设置选项为 选中? 怎么做?
  • 原文地址:https://www.cnblogs.com/zzwerzi/p/7642815.html
Copyright © 2011-2022 走看看