本例子是从网上找到的,项目中改改就可以用,翻出来的以前保存的,找不到原网址了
效果图:
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;