文档章节

自定义进度条

h
 hjqjl
发布于 2017/01/12 15:11
字数 516
阅读 10
收藏 0

本例子是从网上找到的,项目中改改就可以用,翻出来的以前保存的,找不到原网址了
效果图: 输入图片说明

package finals.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.slkj.paotui.customer.R;
public class NodeProgressBar extends View {
        Paint mPaint;
        RectF mRectF;
        float CycleWidth;
        float border;
        float paintSize = 25;
        float node_progress_top;
        public NodeProgressBar(Context context) {
                super(context);
                InitPaint(context, null);
        }
        public NodeProgressBar(Context context, AttributeSet attrs) {
                super(context, attrs);
                InitPaint(context, attrs);
        }
        private void InitPaint(Context context, AttributeSet attrs) {
                mPaint = new Paint();
                mPaint.setAntiAlias(true);
                mRectF = new RectF();
                try {
                        CycleWidth = getResources().getDimension(R.dimen.cycle_width);
                } catch (Exception e) {
                        CycleWidth = 30;
                }
                try {
                        border = getResources().getDimension(R.dimen.node_progress_border_width);
                } catch (Exception e) {
                        border = 50;
                }
                try {
                        paintSize = getResources().getDimension(R.dimen.node_progress_textsize);
                } catch (Exception e) {
                        paintSize = 25;
                }
                try {
                        node_progress_top = getResources().getDimension(R.dimen.node_progress_top);
                } catch (Exception e) {
                        node_progress_top = 13;
                }
                mPaint.setTextSize(paintSize);
        }
        @Override
        protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                DrawBackgroundLine(canvas, 0xFFff8b02, 0xFFdddddd);
                DrawProgress(canvas, 0xFFff8b02, 0xFFdddddd);
                DrawText(canvas, 0xFFff8b02, 0xFFdddddd);
        }
        private void DrawText(Canvas canvas, int currentColor, int color) {
        }
        private void DrawBackgroundLine(Canvas canvas, int currentColor, int color) {
                mPaint.setColor(color);
                mPaint.setStyle(Style.FILL);
                mPaint.setStrokeWidth(CycleWidth);
                float left = CycleWidth + border;
                mRectF.left = left;
                mRectF.top = CycleWidth / 3 + node_progress_top;
                mRectF.right = left + (getWidth() - left * 2) / (max - 1) * (current - 1);
                mRectF.bottom = CycleWidth / 3 * 2 + node_progress_top;
                mPaint.setColor(currentColor);
                canvas.drawRect(mRectF, mPaint);
                mRectF.left = mRectF.right;
                mRectF.right = getWidth() - left;
                mPaint.setColor(color);
                canvas.drawRect(mRectF, mPaint);
        }
        private void DrawProgress(Canvas canvas, int currentColor, int color) {
                float current_border = CycleWidth + border;
                mPaint.setTextAlign(Align.CENTER);
                for (int i = 0; i < max; i++) {
                        if (i < current) {
                                mPaint.setColor(currentColor);
                        } else {
                                mPaint.setColor(color);
                        }
                        float left = current_border + (getWidth() - current_border * 2) / (max - 1) * i;
                        canvas.drawCircle(left, CycleWidth / 2 + node_progress_top, CycleWidth / 2, mPaint);
                        if (i < current) {
                                mPaint.setColor(currentColor);
                        } else {
                                mPaint.setColor(0xFF999999);
                        }
                        if (moneys != null) {
                                canvas.drawText(moneys[i] + "元", left, CycleWidth * 2 + paintSize + node_progress_top, mPaint);
                        } else {
                                canvas.drawText(i * 5 + "元", left, CycleWidth * 2 + paintSize + node_progress_top, mPaint);
                        }
                }
        }
        int max = 4;
        public int getMax() {
                return max;
        }
        public void setMax(int max) {
                this.max = max;
        }
        int current = 2;
        public int getCurrent() {
                return current;
        }
        public void setCurrent(int current) {
                if (current <= 1) {
                        current = 2;
                } else if (current > max) {
                        current = max;
                }
                this.current = current;
                this.postInvalidate();
        }
        int currentX = 0;
        @Override
        public boolean onTouchEvent(MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                        int current_border = (int) (CycleWidth + border);
                        //
                        currentX = (int) (event.getX() - current_border);
                        int itemWidth = (getWidth() - current_border * 2) / ((max - 1));
                        int select = currentX / itemWidth + ((currentX % itemWidth) / (itemWidth / 2) > 0 ? 1 : 0) + 1;
                        setCurrent(select);
                        break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                        break;
                default:
                        break;
                }
                return true;
        }
        public int getFee() {
                if (this.moneys != null) {
                        return this.moneys[current - 1];
                }
                return (current - 1) * 5;
        }
        /**
         * 设置金币价格
         * 
         * @param moneys
         */
        public void setFrees(int[] moneys) {
                this.moneys = moneys;
                this.max = moneys.length;
                postInvalidate();
        }
        int[] moneys;
}

如果想要点击事件,自己写回调函数。
几个关键部分

    public void setOnNodeProgressBarListener(NodeProgressBar.onNodeProgressBarListener onNodeProgressBarListener) {
        this.onNodeProgressBarListener = onNodeProgressBarListener;
    }


    public interface onNodeProgressBarListener {
        void getCurrentNodeAndMoney(int currentNode, int currentMoney);
    }
 case MotionEvent.ACTION_CANCEL:
                if (onNodeProgressBarListener != null) {
                    onNodeProgressBarListener.getCurrentNodeAndMoney(nodeNum, money);
                }
                break;

© 著作权归作者所有

h
粉丝 0
博文 6
码字总数 6809
作品 0
长宁
程序员
私信 提问

暂无文章

从0开始学FreeRTOS-(列表&列表项)-6

FreeRTOS列表&列表项的源码解读 第一次看列表与列表项的时候,感觉很像是链表,虽然我自己的链表也不太会,但是就是感觉很像。 在FreeRTOS中,列表与列表项使用得非常多,是FreeRTOS的一个数...

杰杰1号
26分钟前
4
0
Java的23种设计模式,详细讲解(一)

一、概述 设计模式是解决问题的方案,学习现有的设计模式可以做到经验复用。 拥有设计模式词汇,在沟通时就能用更少的词汇来讨论,并且不需要了解底层细节。 二、创建型 1. 单例(Singleton...

李红欧巴
42分钟前
5
0
android 使用asynctask结合fragment更新UI(另附线程池管理示例)

https://blog.csdn.net/qq_16064871/article/details/70767949

shzwork
42分钟前
3
0
SpringCloud实现分库分表模式下,数据库实时扩容方案

本文源码:GitHub·点这里 || GitEE·点这里 一、项目结构 1、工程结构 2、模块命名 shard-common-entity: 公共代码块shard-open-inte: 开放接口管理shard-eureka-7001: ...

知了一笑
43分钟前
5
0
js--时间切割装换工具类

<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> /* * 修改data原型对象Format方法 ......

zhengzhixiang
53分钟前
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部