`
Darar
  • 浏览: 87806 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

圆形不滚动的进度条

 
阅读更多


这种效果的进度条 代码如下:


package com.netqin.antivirus.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import com.nqmobile.antivirus20.R;

import java.util.Timer;
import java.util.TimerTask;

public class RoundProgressBar extends ImageView {


    private Paint mFramePaint;


    //--------------------
    private Paint  mRoundPaints;		// 主进度条画笔
    private RectF  mRoundOval;			// 矩形区域
    private int    mPaintWidth;		// 画笔宽度
    private int    mPaintColor;		// 画笔颜色
    private int mBottomColor;//进度条背景色


    private int mStartProgress;	    // 进度条起始位置
    private int mCurProgress;    		// 主进度条当前位置
    private int mMaxProgress;			// 进度条最终位置

    private boolean mBRoundPaintsFill;	// 是否填充区域
    //---------------------
    private int   mSidePaintInterval;	// 圆环向里缩进的距离

    private Paint mSecondaryPaint;     // 辅助进度条画笔

    private int   mSecondaryCurProgress;	// 辅助进度条当前位置

    private Paint mBottomPaint;		// 进度条背景图画笔

    private boolean mBShowBottom;		// 是否显示进度条背景色


    //----------------------
    private Handler mHandler;

    private boolean mBCartoom;			// 是否正在作动画

    private Timer   mTimer;			// 用于作动画的TIMER

    private MyTimerTask	mTimerTask;		// 动画任务

    private int 	 mSaveMax;			// 在作动画时会临时改变MAX值,该变量用于保存值以便恢复

    private int     mTimerInterval;	// 定时器触发间隔时间(ms)

    private float   mCurFloatProcess;	// 作动画时当前进度值

    private float   mProcessRInterval;	// 作动画时每次增加的进度值

    private final static int TIMER_ID = 0x100;		// 定时器ID

    private long mCurTime;

    public RoundProgressBar(Context context) {
        super(context);

        initParam();
    }

    public RoundProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub

        initParam();

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

        mMaxProgress = array.getInt(R.styleable.RoundProgressBar_max, 100);
        mSaveMax = mMaxProgress;
        mBRoundPaintsFill = array.getBoolean(R.styleable.RoundProgressBar_fill, true);	// 获得是否是填充模式
        if (mBRoundPaintsFill == false)
        {
            mRoundPaints.setStyle(Paint.Style.STROKE);
            mSecondaryPaint.setStyle(Paint.Style.STROKE);
            mBottomPaint.setStyle(Paint.Style.STROKE);
        }


        mSidePaintInterval = array.getInt(R.styleable.RoundProgressBar_inside_interval, 0);// 圆环缩进距离


        mBShowBottom = array.getBoolean(R.styleable.RoundProgressBar_show_bottom, true);

        mPaintWidth = array.getInt(R.styleable.RoundProgressBar_paint_width, 10);
        if (mBRoundPaintsFill)						// 填充模式则画笔长度改为0
        {
            mPaintWidth = 0;
        }

        mRoundPaints.setStrokeWidth(mPaintWidth);
        mSecondaryPaint.setStrokeWidth(mPaintWidth);
        mBottomPaint.setStrokeWidth(mPaintWidth);

        mPaintColor = array.getColor(R.styleable.RoundProgressBar_paint_color, 0xffffcc00);
        mBottomColor = array.getColor(R.styleable.RoundProgressBar_paint_bottom_color, Color.WHITE);

        mRoundPaints.setColor(mPaintColor);
        int color = mPaintColor & 0x00ffffff | 0x66000000;
        mSecondaryPaint.setColor(color);


        array.recycle(); //一定要调用,否则会有问题


    }



    private void initParam()
    {
        mFramePaint = new Paint();
        mFramePaint.setAntiAlias(true);
        mFramePaint.setStyle(Paint.Style.STROKE);
        mFramePaint.setStrokeWidth(0);

        mPaintWidth = 0;
        mPaintColor = 0xffffcc00;


        mRoundPaints = new Paint();
        mRoundPaints.setAntiAlias(true);
        mRoundPaints.setStyle(Paint.Style.FILL);

        mRoundPaints.setStrokeWidth(mPaintWidth);
        mRoundPaints.setColor(mPaintColor);

        mSecondaryPaint = new Paint();
        mSecondaryPaint.setAntiAlias(true);
        mSecondaryPaint.setStyle(Paint.Style.FILL);
        mSecondaryPaint.setStrokeWidth(mPaintWidth);

        int color = mPaintColor & 0x00ffffff | 0x66000000;
        mSecondaryPaint.setColor(color);


        mBottomPaint = new Paint();
        mBottomPaint.setAntiAlias(true);
        mBottomPaint.setStyle(Paint.Style.FILL);
        mBottomPaint.setStrokeWidth(mPaintWidth);
        mBottomPaint.setColor(mBottomColor);


        mStartProgress = -90;
        mCurProgress = 0;
        mMaxProgress = 100;
        mSaveMax = 100;

        mBRoundPaintsFill = true;
        mBShowBottom = true;

        mSidePaintInterval = 0;

        mSecondaryCurProgress = 0;

        mRoundOval = new RectF(0, 0,  0, 0);




        mTimerInterval = 25;


        mCurFloatProcess = 0;

        mProcessRInterval = 0;

        mBCartoom = false;

        mHandler = new Handler()
        {
            public void handleMessage(Message msg) {
                if (msg.what == TIMER_ID)
                {

                    if (mBCartoom == false)
                    {
                        return ;
                    }


                    mCurFloatProcess += mProcessRInterval;
                    setProgress((int) mCurFloatProcess);

                    if (mCurFloatProcess > mSaveMax)
                    {
                        mBCartoom = false;
                        setProgress(mSaveMax);
                        if (mTimerTask != null)
                        {
                            mTimerTask.cancel();
                            mTimerTask = null;
                        }
                    }


                }
            }

        };
        mTimer = new Timer();

    }



    public synchronized void setProgress (int progress)
    {
        mCurProgress = progress;
        if (mCurProgress < 0)
        {
            mCurProgress = 0;
        }

        if (mCurProgress > mMaxProgress)
        {
            mCurProgress = mMaxProgress;
        }

        invalidate();
    }

    public synchronized int getProgress()
    {
        return mCurProgress;
    }

    public synchronized void setSecondaryProgress (int progress)
    {
        mSecondaryCurProgress = progress;
        if (mSecondaryCurProgress < 0)
        {
            mSecondaryCurProgress = 0;
        }

        if (mSecondaryCurProgress > mMaxProgress)
        {
            mSecondaryCurProgress = mMaxProgress;
        }

        invalidate();
    }

    public synchronized int getSecondaryProgress()
    {
        return mSecondaryCurProgress;
    }

    public synchronized void setMax(int max)
    {
        if (max <= 0)
        {
            return ;
        }

        mMaxProgress = max;
        if (mCurProgress > max)
        {
            mCurProgress = max;
        }

        if (mSecondaryCurProgress > max)
        {
            mSecondaryCurProgress = max;
        }

        mSaveMax = mMaxProgress;

        invalidate();
    }

    public synchronized int getMax()
    {
        return mMaxProgress;
    }




    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);

        Log.i("", "W = " + w + ", H = " + h);


        if (mSidePaintInterval != 0)
        {
            mRoundOval.set(mPaintWidth/2 + mSidePaintInterval, mPaintWidth/2 + mSidePaintInterval,
                    w - mPaintWidth/2 - mSidePaintInterval, h - mPaintWidth/2 - mSidePaintInterval);
        }else{

            int sl = getPaddingLeft();
            int sr = getPaddingRight();
            int st = getPaddingTop();
            int sb = getPaddingBottom();


            mRoundOval.set(sl + mPaintWidth/2, st + mPaintWidth/2, w - sr - mPaintWidth/2, h - sb - mPaintWidth/2);
        }





    }



    public synchronized void  startAnimation(int time, int progresLenght)
    {
        if (time <= 0 || mBCartoom == true)
        {
            return ;
        }
        mBCartoom = true;

        if (mTimerTask != null)
        {
            mTimerTask.cancel();
            mTimerTask = null;
        }

        setProgress(0);
        setSecondaryProgress(0);

        mSaveMax = progresLenght;
        mMaxProgress = (1000 / mTimerInterval) * time;

        mProcessRInterval = (float)mTimerInterval * mMaxProgress / (time * 1000);
        mCurFloatProcess = 0;

        mCurTime = 0;

        mTimerTask = new MyTimerTask();
        mTimer.schedule(mTimerTask, mTimerInterval, mTimerInterval);

    }

    public void setShaperColors(int[] colors){
      if (null != colors){
          LinearGradient mLinearGradient = new LinearGradient(0,0,100,100,colors,null,Shader.TileMode.REPEAT);
          mRoundPaints.setShader(mLinearGradient);
      }
    }


    public synchronized void  stopAnimation()
    {

        mBCartoom = false;
        mMaxProgress = mSaveMax;

        setProgress(0);
        if (mTimerTask != null)
        {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }




    public void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);


        if (mBShowBottom)
        {
            canvas.drawArc(mRoundOval, 0, 360, mBRoundPaintsFill, mBottomPaint);
        }

        float secondRate = (float)mSecondaryCurProgress / mMaxProgress;
        float secondSweep = 360 * secondRate;
        canvas.drawArc(mRoundOval, mStartProgress, secondSweep, mBRoundPaintsFill, mSecondaryPaint);

        float rate = (float)mCurProgress / mMaxProgress;
        float sweep = 360 * rate;
        canvas.drawArc(mRoundOval, mStartProgress, sweep, mBRoundPaintsFill, mRoundPaints);


    }


    class MyTimerTask extends TimerTask{

        @Override
        public void run() {
            Message msg = mHandler.obtainMessage(TIMER_ID);
            msg.sendToTarget();

        }

    }

}

  • 大小: 22.9 KB
分享到:
评论

相关推荐

    安卓进度条loadingprogress相关-水波滚动圆环进度条.zip

    首先,让我们了解水波滚动进度条。这种进度条设计通常以波浪形图案展示进度,给人一种动态和生动的感觉。实现这一效果通常需要自定义View,并结合动画来模拟水波的滚动。开发者可能需要重写`onDraw()`方法,绘制水波...

    WPF,C#圆形加载进度条。

    本教程将详细讲解如何使用C#和WPF来实现一个圆形的加载进度条,同时带有百分比显示。 首先,我们需要了解WPF的基本概念。WPF是.NET Framework的一部分,它提供了一个用于构建桌面应用程序的强大框架。它使用XAML...

    水波滚动,圆环进度条

    "水波滚动"和"圆环进度条"是两种常用于增强视觉效果和提供反馈机制的UI元素。这些元素常见于各种应用,如移动应用、网页或者桌面软件,用来展示数据加载、任务完成度等信息。 水波滚动通常指的是在界面上呈现的动态...

    CSS3制作圆形滚动进度条动画的示例

     今天手把手教大家用CSS3制作圆形滚动进度条动画,想不会都难!那么,到底是什么东东呢?先不急,之前我分享了一个css实现进度条效果的博客《CSS实现进度条和订单进度条》,但是呢,那篇博客只是制作出来效果而已,...

    自定义圆形进度条(带圆点)

    本示例中,我们探讨的主题是如何创建一个自定义的圆形进度条,并且带有圆点效果。这个控件可以用于显示某个任务的完成度或者加载状态,通过调整颜色和属性,使其更加符合应用程序的视觉风格。 首先,我们需要创建一...

    Android 电池电量进度条,上下滚动图片的进度条(battery)

    总结,创建一个模拟电池电量的上下滚动进度条涉及到自定义View的绘制、动画效果的实现、电池状态监听以及UI布局设计等多个环节。通过巧妙地结合这些技术,可以为用户提供更直观、动态的电量显示效果。在实际开发中,...

    【JavaScript源代码】JS实现圆形进度条拖拽滑动.docx

    ### JavaScript 实现圆形进度条拖拽滑动 #### 技术背景 在现代网页开发中,动态元素和交互式设计是提升用户体验的关键因素之一。其中,进度条是一种常见的UI组件,用于显示操作或任务的完成程度。传统的线性进度条...

    ReactNative组件用于利用ReactART创建动画圆形进度条

    通过`Animated.timing`函数,我们可以设置一个动画,让进度值随着时间逐渐变化,从而实现进度条的平滑滚动。 在实际代码中,可能的步骤如下: 1. 导入所需的库: ```javascript import React, { Component } ...

    labview进度条

    进度条,请勿在未经授权的情况下上传任何涉及著作权侵权的资源,除非该资源完全由您个人创作

    圆形进度条

    "圆形进度条"就是一种常见的设计,它可以优雅地展示数据加载、任务执行或计时过程。本教程将深入探讨如何实现一个自定义的圆形进度条,并结合Butterknife库和自定义弹性ScrollView来增强功能。 首先,让我们讨论...

    自定义环形进度条 显示百分比

    环形进度条是一种图形化的进度指示器,通常由一个闭合的圆形路径组成,其中一部分被填充以表示已完成的部分,剩余部分表示未完成。在Android中,可以使用`ProgressBar`类的子类如`Ring形ProgressDrawable`来创建;在...

    易语言 漂亮的进度条

    7. 进度条动画效果:为了增加用户体验,还可以为进度条添加动画效果,如平滑滚动、渐变填充等。这可能需要更深入地理解易语言的绘图和动画控制命令。 8. 错误处理与调试:在编程过程中,合理地处理错误和进行调试是...

    SVG+CSS3圆形倒计时滚动条动画效果

    在这个"SVG+CSS3圆形倒计时滚动条动画效果"中,我们将深入探讨如何利用这两者来创建一个引人注目的交互式组件。 SVG是一种基于XML的矢量图形格式,它可以无损地缩放,而且文件体积小,适合在网页上展示复杂的图形。...

    js-swiper-分页标签加进度条-自定义位置加进度条

    Swiper 是一个流行的JavaScript滑动插件,常用于创建轮播图、滑动导航和其它交互式的滚动效果。在这个特定的场景中,我们关注的是如何在Swiper中添加分页标签以及进度条,并且能够自定义它们的位置。下面将详细探讨...

    AndroidLabelView图片右上角的角标图片以及滚动数字效果以及圆形进度条

    在Android开发中,有时我们需要为应用的图标或者某些视图添加一些特殊的效果,例如右上角的角标图片、滚动数字以及圆形进度条等。这些元素可以用来展示通知数量、未读消息或者其他状态信息,增强用户界面的交互性和...

    Android漂亮的自定义圆形进度条源码.zip

    通过设置动画的时间、插值器等参数,可以使进度条的滚动更具有动态感。 6. 属性动画:在Android中,属性动画API允许开发者在不改变对象状态的情况下,模拟对象属性的变化,这在自定义控件的动画效果中非常常用。...

    jQuery/CSS3动感实用的进度条插件 10种漂亮外观

    最近这两天一直在为大家分享一些HTML5、CSS和SVG的炫酷动画,...这次我们来介绍一款实用的jQuery进度条插件,这款jQuery进度条插件的外观利用了CSS3的特性,让进度条外观显得非常时尚漂亮,一共有10种样式,非常实用。

    使用 css3 实现圆形进度条的示例

    在开发微信小程序的时候,遇到圆形进度条的需求。使用canvas绘图比较麻烦: 1、为了实现在不同屏幕上面的适配,必须动态的计算进度条的大小; 2、在小程序中,canvas的画布具有最高的层级,不易于扩展。 但使用css3...

    jQuery实现鼠标滚轮控制的10多种圆形百分比进度条效果.zip

    在本项目中,"jQuery实现鼠标滚轮控制的10多种圆形百分比进度条效果.zip",我们将探讨如何使用jQuery库来创建响应式且可自定义的圆形百分比进度条,这些进度条能够通过用户的鼠标滚轮进行交互。jQuery是一个强大的...

Global site tag (gtag.js) - Google Analytics