2018-07-22 19:19:53 u013855006 阅读数 2102

日常的开发中经常会需要用到自定义View,这次刚好有个需求,需要用到带有节点的进度条。东西很简单直接继承View就行了。

这里写图片描述

首先定义一些需要的属性
/**
     * 背景画笔
     */
    private Paint bgPaint;
    /**
     * 前景画笔
     */
    private Paint forePaint;
    /**
     * 选中画笔
     */
    private Paint selectPaint;
    /**
     * 未选中画笔
     */
    private Paint unselectPaint;
    /**
     * 背景颜色
     */
    private int bgColor = Color.parseColor("#9C9C9C");
    /**
     * 前景颜色
     */
    private int foreColor = Color.parseColor("#8A2BE2");
    /**
     * 默认高度
     */
    private int defaultHeight;
    /**
     * 节点文字
     */
    private List<String> nodeList;
    private List<Rect> mBounds;
    /**
     * 节点圆的半径
     */
    private int radius;
    /**
     * 文字和节点进度条的top
     */
    private int marginTop;
    /**
     * 两个节点之间的距离
     */
    private int dividWidth;
    /**
     * 选中位置
     */
    private int selectIndex;
然后在构造方法中初始化这些数据
private void init(Context context) {
        radius = SizeUtils.dp2px(context,4);
        defaultHeight = SizeUtils.dp2px(context,30);
        marginTop = SizeUtils.dp2px(context,5);

        bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bgPaint.setColor(bgColor);
        bgPaint.setStyle(Paint.Style.FILL);

        forePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        forePaint.setColor(foreColor);
        forePaint.setStyle(Paint.Style.FILL);

        unselectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        unselectPaint.setColor(bgColor);
        unselectPaint.setTextSize(SizeUtils.sp2px(context,10));

        selectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        selectPaint.setColor(foreColor);
        selectPaint.setTextSize(SizeUtils.sp2px(context,10));
    }
设置节点文字
/**
     * 设置数据
     * @param nodeDatas
     */
    public void setNodeList(List<String> nodeDatas){
        if(nodeDatas != null){
            nodeList = nodeDatas;
        }
        //测量文字所占用的空间
        measureText();
    }

    /**
     * 设置选中位置
     * @param selectIndex
     */
    public void setSelectIndex(int selectIndex){
        this.selectIndex = selectIndex;
        invalidate();
    }
/**
     * 测量文字的长宽
     */
    private void measureText(){
        mBounds = new ArrayList<>();
        for (int i = 0; i < nodeList.size(); i++) {
            Rect mBound = new Rect();
            unselectPaint.getTextBounds(nodeList.get(i),0,nodeList.get(i).length(),mBound);
            mBounds.add(mBound);
        }
    }
最后重要的步骤,开始在onDraw中绘制节点进度条和绘制文字

1、绘制灰色背景线条

if(nodeList == null || nodeList.isEmpty()){
            return;
        }
        bgPaint.setStrokeWidth(radius/2);
        //绘制灰色的背景线条
        canvas.drawLine(radius,radius,getWidth()-radius,radius,bgPaint);

2、绘制节点上的圆和两个节点之间的间隔线条

//画节点圆
        //每个圆相隔的距离
        dividWidth = (getWidth()-radius*2)/(nodeList.size() - 1);
        forePaint.setStrokeWidth(radius/2);
        for (int i = 0; i < nodeList.size(); i++) {
            if(i == selectIndex){
                for (int j = 0; j <= i; j++) {
                    canvas.drawCircle(radius+ j * dividWidth, radius, radius , forePaint);
                    canvas.drawLine(radius,radius,j*dividWidth,radius,forePaint);
                }
            }else if(i>selectIndex){
                canvas.drawCircle(radius + i * dividWidth, radius, radius, bgPaint);
            }
        }

3、绘制节点上的文字

for (int i = 0; i < nodeList.size(); i++) {
            int currentTextWidth=mBounds.get(i).width();
            if (i==0){
                if (i==selectIndex){
                    canvas.drawText(nodeList.get(i), 0, radius*2 + marginTop + mBounds.get(i).height()/2, selectPaint);
                }else if(i>selectIndex) {
                    canvas.drawText(nodeList.get(i), 0, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
                }
            }else if (i==nodeList.size()-1){
                if (i==selectIndex){
                    for (int j = 0; j <= i; j++) {
                        if(j == 0){
                            canvas.drawText(nodeList.get(j), 0, radius*2 + marginTop + mBounds.get(j).height()/2, selectPaint);
                        }else if(j == i){
                            canvas.drawText(nodeList.get(j), getWidth() - currentTextWidth, radius*2 + marginTop + mBounds.get(j).height()/2, selectPaint);
                        }else{
                            canvas.drawText(nodeList.get(j), radius + j * dividWidth - currentTextWidth / 2, radius*2 + marginTop + mBounds.get(j).height()/2, selectPaint);
                        }
                    }
                }else if(i>selectIndex) {
                    canvas.drawText(nodeList.get(i), getWidth() - currentTextWidth, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
                }
            }else {
                if (i==selectIndex){
                    for (int j = 0; j <= i; j++) {
                        if(j>0){
                            canvas.drawText(nodeList.get(j), radius + j * dividWidth - currentTextWidth / 2, radius*2 + marginTop + mBounds.get(j).height()/2, selectPaint);
                        }else{
                            canvas.drawText(nodeList.get(j), 0, radius*2 + marginTop + mBounds.get(j).height()/2, selectPaint);
                        }
                    }
                }else if(i>selectIndex) {
                    canvas.drawText(nodeList.get(i), radius + i * dividWidth - currentTextWidth / 2, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
                }
            }
        }

有时候可能需要的是下面这种进度条

这里写图片描述

只需要修改onDraw中绘制节点圆和文字的方法就可以了
for (int i=0; i < nodeList.size();i++) {
            if (i==selectIndex){
                canvas.drawCircle(radius+ i * dividWidth, radius, radius , forePaint);
            }else {
                canvas.drawCircle(radius + i * dividWidth, radius, radius, bgPaint);
            }
        }

        for (int i=0; i<nodeList.size();i++){
            int currentTextWidth=mBounds.get(i).width();
            if (i==0){
                if (i==selectIndex){
                    canvas.drawText(nodeList.get(i), 0, radius*2 + marginTop + mBounds.get(i).height()/2, selectPaint);
                }else {
                    canvas.drawText(nodeList.get(i), 0, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
                }
            }else if (i==nodeList.size()-1){
                if (i==selectIndex){
                    canvas.drawText(nodeList.get(i), getWidth() - currentTextWidth, radius*2 + marginTop + mBounds.get(i).height()/2, selectPaint);
                }else {
                    canvas.drawText(nodeList.get(i), getWidth() - currentTextWidth, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
                }
            }else {
                if (i==selectIndex){
                    canvas.drawText(nodeList.get(i), radius + i * dividWidth - currentTextWidth / 2, radius*2 + marginTop + mBounds.get(i).height()/2, selectPaint);
                }else {
                    canvas.drawText(nodeList.get(i), radius + i * dividWidth - currentTextWidth / 2, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
                }
            }
        }

点击节点进行节点切换

// 实现节点切换,把注释打开就行了
@Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX;
        float eventY;
        int i = event.getAction();
        if (i == MotionEvent.ACTION_DOWN) {
        } else if (i == MotionEvent.ACTION_MOVE) {
        } else if (i == MotionEvent.ACTION_UP) {
            eventX = event.getX();
            eventY = event.getY();
            //计算选中的index
            float select = eventX / dividWidth;
            float xs = select - (int) (select);
            //selectIndex = (int) select + (xs > 0.5 ? 1 : 0);
        }
        //invalidate();
        return true;
    }
2017-02-05 17:25:27 a49220824 阅读数 5203

带节点进度条的实现方法不止一个,但是如果要实现图中这种效果的,初步看好像还不简单。进度条的形状不规则、背景是渐变颜色、节点上面还有个勾。这里提供一个很简单的思路,简单的都不好意思说了……

为了实现这个效果,我们需要三张图片,就是下面这三张。

看到这里可能你已经知道要怎么做了,三张图片分别是:

1.进度条填充部分的背景,带渐变颜色带节点小勾
2.进度条未填充部分的背景,纯色
3.进度条外框背景,是一张镂空图,颜色和进度条外的颜色一致

注意这三张图片的大小必须一致,将这三张图片叠加到一起就是我们的进度条了。至于进度条的动画,我们只要对第二层的那张图片实现一个属性动画就可以了:


public void setProgress(float stage) {
    int progressWidth = ivProgress.getWidth();
    ObjectAnimator animator = ObjectAnimator.ofFloat(ivProgress, "translationX", stage * progressWidth);
    animator.setDuration((int) (Math.abs(stage - currentStage) * 1000));
    animator.start();
    currentStage = stage;
}

至于如何控制到哪一个节点,这个可以自己拿尺子量一下,多试几次,上面代码中的stage就是这个作用,它从这里取值:


//不同阶段进度条所在位置
public static final float STAGE0 = 0f;
public static final float STAGE1 = 0.046f;
public static final float STAGE2 = 0.285f;
public static final float STAGE3 = 0.523f;
public static final float STAGE4 = 0.761f;
public static final float STAGE5 = 1f;

最后看一下效果。

这样就已经实现了一个看上去有点复杂的进度条了,妥妥的。

源码地址

2019-10-31 16:28:51 qq_42191634 阅读数 7

                                  自定义view节点进度条

 

效果先看看

1.话不多就喜欢上代码(直接上自定义view代码)

package cn.kaitong.com.module.acceptcheck.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.kaitong.com.module.acceptcheck.utils.SizeUtils;

public class Jiedian extends  View {

    /**
     * 背景画笔
     */
    private Paint bgPaint;
    /**
     * 前景画笔
     */
    private Paint forePaint;
    /**
     * 选中画笔
     */
    private Paint selectPaint;
    /**
     * 未选中画笔
     */
    private Paint unselectPaint;
    /**
     * 背景颜色
     */
    private int bgColor = Color.parseColor("#CCCCCC");
    /**
     * 前景颜色
     */
    private int foreColor = Color.parseColor("#009587");

    /**
     * 默认高度
     */
    private int defaultHeight;
    /**
     * 节点文字
     */
    private List<String> nodeList;
    private List<Rect> mBounds;
    /**
     * 节点圆的半径
     */
    private int radius;
    /**
     * 文字和节点进度条的top
     */
    private int marginTop;
    /**
     * 两个节点之间的距离
     */
    private int dividWidth;
    /**
     * 选中位置
     */
    private int selectIndex;

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

    private void init(Context context) {
        radius = SizeUtils.dp2px(context,8);
        defaultHeight = SizeUtils.dp2px(context,30);
        marginTop = SizeUtils.dp2px(context,10);

        bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bgPaint.setColor(bgColor);
        bgPaint.setStyle(Paint.Style.FILL);

        forePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        forePaint.setColor(foreColor);
        forePaint.setStyle(Paint.Style.FILL);

        unselectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        unselectPaint.setColor(bgColor);
        unselectPaint.setTextSize(SizeUtils.sp2px(context,10));

        selectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        selectPaint.setColor(foreColor);
        selectPaint.setTextSize(SizeUtils.sp2px(context,10));
    }


    /**
     * 设置数据
     * @param nodeDatas
     */
    public void setNodeList(List<String> nodeDatas){
        if(nodeDatas != null){
            nodeList = nodeDatas;
        }
        //测量文字所占用的空间
        measureText();
    }

    /**
     * 设置选中位置
     * @param selectIndex
     */
    public void setSelectIndex(int selectIndex){
        this.selectIndex = selectIndex;
        invalidate();
    }
    /**
     * 测量文字的长宽
     */
    private void measureText(){
        mBounds = new ArrayList<>();
        for (int i = 0; i < nodeList.size(); i++) {
            Rect mBound = new Rect();
            unselectPaint.getTextBounds(nodeList.get(i),0,nodeList.get(i).length(),mBound);
            mBounds.add(mBound);
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(nodeList == null || nodeList.isEmpty()){
            return;
        }
        bgPaint.setStrokeWidth(radius/3);
        //绘制灰色的背景线条
        canvas.drawLine(radius,getHeight()/2,getWidth()-radius,getHeight()/2,bgPaint);

        //画节点圆
        //每个圆相隔的距离
        dividWidth = (getWidth()-radius*2)/(nodeList.size() - 1);
        forePaint.setStrokeWidth(radius/2);
        for (int i=0; i < nodeList.size();i++) {
            if (i==selectIndex){
                canvas.drawCircle(radius+ i * dividWidth, getHeight()/2, radius , forePaint);
            }else {
                canvas.drawCircle(radius + i * dividWidth, getHeight()/2, radius, bgPaint);
            }
        }

        for (int i=0; i<nodeList.size();i++){
            int currentTextWidth=mBounds.get(i).width();
            if (i==0){
                if (i==selectIndex){
                    canvas.drawText(nodeList.get(i), radius-currentTextWidth/2, getHeight()/2-mBounds.get(i).height()/2-radius, selectPaint);
                }else {
                    canvas.drawText(nodeList.get(i), radius-currentTextWidth/2, getHeight()/2-mBounds.get(i).height()/2-radius, unselectPaint);
                }
            }else if (i==nodeList.size()-1){
                if (i==selectIndex){
                    canvas.drawText(nodeList.get(i), getWidth()-radius-currentTextWidth/2, getHeight()/2-mBounds.get(i).height()/2-radius, selectPaint);
                }else {
                    canvas.drawText(nodeList.get(i), getWidth()-radius-currentTextWidth/2, getHeight()/2-mBounds.get(i).height()/2-radius , unselectPaint);
                }
            }else {
                if (i==selectIndex){
                    canvas.drawText(nodeList.get(i), radius + i * dividWidth - currentTextWidth / 2, getHeight()/2-mBounds.get(i).height()/2-radius  , selectPaint);
                }else {
                    canvas.drawText(nodeList.get(i), radius + i * dividWidth - currentTextWidth / 2, getHeight()/2-mBounds.get(i).height()/2-radius , unselectPaint);
                }
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX;
        float eventY;
        int i = event.getAction();
        Log.e("onTouchEvent1", String.valueOf(i));
        if (i == MotionEvent.ACTION_DOWN) {
            Log.e("onTouchEvent", "ACTION_DOWN");
        } else if (i == MotionEvent.ACTION_MOVE) {
            Log.e("onTouchEvent", "ACTION_MOVE");
        } else if (i == MotionEvent.ACTION_UP) {
            Log.e("onTouchEvent", "ACTION_UP");
            eventX = event.getX();
            eventY = event.getY();
            float select = eventX / dividWidth; //计算选中的index
            float xs = select - (int) (select);
            selectIndex = (int) select + (xs > 0.5 ? 1 : 0);
            onClickListener.onCircleClick(selectIndex);
        }
        invalidate();
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        if(widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){//宽高都设置为wrap_content
            setMeasuredDimension(widthSpecSize,defaultHeight);
        }else if(widthSpecMode == MeasureSpec.AT_MOST){//宽设置为wrap_content
            setMeasuredDimension(widthSpecSize,heightSpecSize);
        }else if(heightSpecMode == MeasureSpec.AT_MOST){//高设置为wrap_content
            setMeasuredDimension(widthSpecSize, defaultHeight);
        }else{//宽高都设置为match_parenth或具体的dp值
            setMeasuredDimension(widthSpecSize, heightSpecSize);
        }
    }
    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener){
        this.onClickListener=onClickListener;
    }

    public interface  OnClickListener{
        void onCircleClick(int postion);
    }

}

2.调用的时候非常简单

                    ls_shape.setNodeList(itmes);//设置文字集合
                    ls_shape.setSelectIndex(0);//设置默认小蓝点在哪里
      
                     //条目点击事件监听
                    ls_shape.setOnClickListener(new Jiedian.OnClickListener() {
                        @Override
                        public void onCircleClick(int postion) {
                            preid = prejictcontent.get(postion).getId();
                            //toast(itmes.get(postion) + "我是ID:" + preid);
                            Log.e("我是id", preid +"");
                        
                        }
                    });

 

2017-07-17 16:56:38 lihu511210952 阅读数 259

根据公司的要求可以在进度条上添加节点,并点击节点跳到相应的位置上,在节点下方显示节点信息。

直接上代码,自定义进度条,可以多提提建议。

package com.ankoninc.esdiagnose.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

import com.ankoninc.data.MarkType;
import com.ankoninc.esdiagnose.AppContext;
import com.ankoninc.esdiagnose.R;
import com.ankoninc.utils.DensityUtils;
import com.ankoninc.utils.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/22.
 */
public class NodeSeekBar extends SeekBar {

    private static final String LOG_TAG = "NodeSeekBar";
    private static final float NODE_RADIUS = DensityUtils.dp2px(6);
    private static final float NODE_TEXT_OFFSET = NODE_RADIUS + DensityUtils.dp2px(9);

    private List<Node> mNodes;
    private Paint mPaint;
    private SeekBarDataProvider mProvider;

    public NodeSeekBar(Context context) {
        super(context);
        init();
    }

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

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

    private void init() {

        mNodes = new ArrayList<>();
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(DensityUtils.sp2px(12));
    }

    /**
     * SeekBar长度对应的数据来源实现类
     * @param provider
     */
    public void setDataProvider(SeekBarDataProvider provider) {
        mProvider = provider;
        invalidate();
    }

    /**
     * 添加某个节点
     * @param position
     * @param type
     */
    public void addNode(int position, MarkType type) {
        Node node = new Node(position, type);
        int index = findNodeIndex(position);
        if (index >= 0 && index < mNodes.size()) {
            mNodes.set(index, node);
        } else {
            mNodes.add(node);
        }
        invalidate();
    }

    /**
     * 删除某个节点
     * @param position
     */
    public void removeNode(int position) {
        int index = findNodeIndex(position);
        if (index >= 0 && index < mNodes.size()) {
            mNodes.remove(index);
            invalidate();
        }
    }

    private int findNodeIndex(int position) {

        for (int i=0; i<mNodes.size(); i++) {
            Node node = mNodes.get(i);
            if (node.getImageIndex() == position) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 查找node,返回node的名称
     * @param position
     * @return
     */
    public String getNodeName(int position) {
        Node node = findNodeByPos(position);
        if (node != null) {
            return node.getName();
        }
        return null;
    }

    private Node findNodeByPos(int position) {

        int index = findNodeIndex(position);
        if (index >= 0 && index < mNodes.size()) {
            return mNodes.get(index);
        }
        return null;
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mProvider == null) {
            // don't draw nodes when SeekBarProvider is not set
            return;
        }

        float height = getHeight();

        for (Node node : mNodes) {

            int imageIndex = node.getImageIndex();
            int position = findPositionInSeekBar(imageIndex);
            if (position != -1) {
                float circle_center = adjustNodeCenter(position);
                String name = node.getName();
                if (!TextUtils.isEmpty(name)) {
                    canvas.drawText(name, circle_center - NODE_TEXT_OFFSET, height - 3, mPaint);
                }
                canvas.drawCircle(circle_center, height / 2 - 3, NODE_RADIUS, mPaint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (mProvider == null) {
            return super.onTouchEvent(event);
        } else {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                float x = event.getX();
                float y = event.getY();
                double height = getHeight();
                for (Node node : mNodes) {
                    int imageIndex = node.getImageIndex();
                    int position = findPositionInSeekBar(imageIndex);
                    if (position != -1) {
                        float circle_center = adjustNodeCenter(position);
                        if (Math.sqrt((circle_center - x) * (circle_center - x)
                                + (height / 2 - y) * (height / 2 - y)) <= NODE_RADIUS + 20) {
                            //在圆心中点击节点
                            onItemClickNodeListener.onClickNode(position);
                            return true;
                        }
                    }
                }
            }

            return super.onTouchEvent(event);
        }
    }

    /**
     * 查找image对应的位置
     * @param imageIndex
     * @return
     */
    private int findPositionInSeekBar(int imageIndex) {

        if (mProvider == null) {
            return -1;
        }

        int max = getMax();
        int position = mProvider.findImagePosition(imageIndex);
        Log.d(LOG_TAG, "imageIndex: %s, position: %s", imageIndex, position);
        if (position < 0 || position >= max) {
            if (position == -1) {
                Log.e(LOG_TAG, "Cannot find item index=%s in data provider!", imageIndex);
            } else {
                Log.e(LOG_TAG, "SeekBar max length: %s not equal with total image size from data provider!", max);
            }
            return -1;
        }

        return position;
    }

    /**
     * 调整Node中心点的位置,返回位置的x值
     * @param position
     * @return
     */
    private float adjustNodeCenter(int position) {
        int width = getWidth();
        int max = getMax();
        if (max == 0) {
            return 0;
        }

        float circle_center = width * (position + 1) / max;
        int padding = 2;
        if (circle_center <= NODE_TEXT_OFFSET) {
            circle_center = NODE_TEXT_OFFSET + padding;
        } else if (circle_center >= width - NODE_TEXT_OFFSET) {
            circle_center = width - NODE_TEXT_OFFSET - padding;
        }
        return circle_center;
    }

    @Override
    public synchronized void setMax(int max) {
        int lastMax = getMax();
        Log.d(LOG_TAG, "max: %s, lastMax: %s", max, lastMax);

        super.setMax(max);

        if (lastMax != max && !mNodes.isEmpty()) {
            invalidate();
        }
    }

    public interface OnItemClickNodeListener{
        void onClickNode(int imageIndex);
    }

    private OnItemClickNodeListener onItemClickNodeListener;

    public void setOnItemClickNodeListener(OnItemClickNodeListener onItemClickNodeListener) {
        this.onItemClickNodeListener = onItemClickNodeListener;
    }

    /**
     * Node对应的实体类
     */
    static class Node {
        private int imageIndex;
        private MarkType type;

        public Node(int imageIndex, MarkType type) {
            this.imageIndex = imageIndex;
            this.type = type;
        }

        public MarkType getType() {
            return this.type;
        }

        public int getImageIndex() {
            return this.imageIndex;
        }

        public String getName() {
            Context context = AppContext.getInstance();
            String name = null;
            switch (type){
                case ESOPHAGUS:
                    name = context.getString(R.string.seekbar_esophagus);
                    break;
                case STOMACH:
                    name = context.getString(R.string.seekbar_stomach);
                    break;
                case SMALL_INTESTINE:
                    name = context.getString(R.string.seekbar_small_intestine);
                    break;
                case COLON:
                    name = context.getString(R.string.seekbar_colon);
                    break;
            }
            return name;
        }

    }

    public interface SeekBarDataProvider {
        int findImagePosition(int imageIndex);
    }

}

2018-10-13 15:13:46 anhldd 阅读数 236

java文件写法

package com.example.l.myappli;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private int code1=1;//操作进度条
private int code2=2;//操作其他
private ProgressBar pb_main_pro1;
private  Button bu_main_but1;
private TextView tv_main_text1;

private  int number=0;

private Handler myHandler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        int code=msg.what;
        if(code==1) {
            tv_main_text1.setText(number + "%");
            pb_main_pro1.setProgress(number);

        }

    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bu_main_but1=findViewById(R.id.bu_main_but1);
    pb_main_pro1=findViewById(R.id.pb_main_pro1);
    tv_main_text1=findViewById(R.id.tv_main_text1);

    //设置按钮的点击事件
    bu_main_but1.setOnClickListener(this);


}

//点击事件
@Override
public void onClick(View v) {
    new MyThread().start();
}

//内部类
//子线程
private class MyThread extends Thread{
    @Override
    public void run() {
        super.run();
        while(true){
            number++;
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(number==100){

                number=0;
                break;
            }

            myHandler.sendEmptyMessage(code1);
        }
    }
}

}

activity_main.xml配置
1使用LinearLayout布局

2设置属性
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
android:padding=“20dp”
android:columnCount=“5”
android:rowCount=“10”

3把下面代码放在布局里面

    <ProgressBar
        android:id="@+id/pb_main_pro1"
        style="?android:attr/progressBarStyleHorizontal"
        android:progress="0"
        android:max="100"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_main_text1"
        android:text="10%"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>
<Button
    android:id="@+id/bu_main_but1"
    android:text="下载按钮"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

实现效果
在这里插入图片描述

点击按钮就可以了查看效果了
喜欢的话可以转发,本人对博客的编码方式不太习惯,有不足的地方还请各位可以指出来,我一定改正

android 节点进度条

阅读数 5387

没有更多推荐了,返回首页