Untitled
unknown
plain_text
a year ago
2.3 kB
4
Indexable
package com.example.customprogressbar;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ProgressBar;
public class CustomProgressBar extends ProgressBar {
private Paint paint;
private int[] anchorPoints = new int[4]; // 4 điểm neo
public CustomProgressBar(Context context) {
super(context);
init();
}
public CustomProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
}
// Phương thức để đặt 4 điểm neo
public void setAnchorPoints(int[] points) {
if (points.length == 4) {
this.anchorPoints = points;
invalidate(); // Yêu cầu vẽ lại
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Lấy chiều rộng của ProgressBar
int width = getWidth();
int height = getHeight();
// Xác định các điểm neo trên thanh
int startX = 0;
int endX = width;
// Chia các đoạn dựa trên các điểm neo
int section1 = anchorPoints[0];
int section2 = anchorPoints[1];
int section3 = anchorPoints[2];
int section4 = anchorPoints[3];
// Vẽ các đoạn màu khác nhau cho mỗi khoảng giữa các điểm neo
paint.setColor(Color.RED); // Màu đoạn 1
canvas.drawRect(startX, 0, section1, height, paint);
paint.setColor(Color.YELLOW); // Màu đoạn 2
canvas.drawRect(section1, 0, section2, height, paint);
paint.setColor(Color.GREEN); // Màu đoạn 3
canvas.drawRect(section2, 0, section3, height, paint);
paint.setColor(Color.BLUE); // Màu đoạn 4
canvas.drawRect(section3, 0, section4, height, paint);
// Vẽ phần còn lại nếu có
paint.setColor(Color.GRAY);
canvas.drawRect(section4, 0, endX, height, paint);
}
}
Editor is loading...
Leave a Comment