Skip to content

Commit

Permalink
fix setColor not work
Browse files Browse the repository at this point in the history
  • Loading branch information
JinJieGu committed Dec 28, 2017
1 parent 71c6648 commit a5a28f3
Showing 1 changed file with 177 additions and 173 deletions.
350 changes: 177 additions & 173 deletions library/src/main/java/co/mobiwise/library/ProgressLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,180 +29,184 @@

public class ProgressLayout extends View implements Animatable {

private static final int COLOR_EMPTY_DEFAULT = 0x00000000;
private static final int COLOR_LOADED_DEFAULT = 0x11FFFFFF;
private static final int PROGRESS_SECOND_MS = 1000;

private Paint paintProgressLoaded;
private Paint paintProgressEmpty;
private int loadedColor;
private int emptyColor;
private boolean isPlaying = false;
private boolean isAutoProgress;

private int mHeight;
private int mWidth;
private int maxProgress;
private int currentProgress = 0;

private Handler handlerProgress;

private ProgressLayoutListener progressLayoutListener;


public ProgressLayout(Context context) {
this(context, null);
}

public ProgressLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

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

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ProgressLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}

@Override public boolean isRunning() {
return isPlaying;
}

@Override public void start() {
if (isAutoProgress) {
isPlaying = true;
handlerProgress.removeCallbacksAndMessages(null);
handlerProgress.postDelayed(mRunnableProgress, 0);
}
}

@Override public void stop() {
isPlaying = false;
handlerProgress.removeCallbacks(mRunnableProgress);
postInvalidate();
}

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
}

@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0, 0, mWidth, mHeight, paintProgressEmpty);
canvas.drawRect(0, 0, calculatePositionIndex(currentProgress), mHeight, paintProgressLoaded);
}

private void init(Context context, AttributeSet attrs) {
setWillNotDraw(false);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.progressLayout);
isAutoProgress = a.getBoolean(R.styleable.progressLayout_autoProgress, true);
maxProgress = a.getInt(R.styleable.progressLayout_maxProgress, 0);
maxProgress = maxProgress * 10;
loadedColor = a.getColor(R.styleable.progressLayout_loadedColor, COLOR_LOADED_DEFAULT);
emptyColor = a.getColor(R.styleable.progressLayout_emptyColor, COLOR_EMPTY_DEFAULT);
a.recycle();

paintProgressEmpty = new Paint();
paintProgressEmpty.setColor(emptyColor);
paintProgressEmpty.setStyle(Paint.Style.FILL);
paintProgressEmpty.setAntiAlias(true);

paintProgressLoaded = new Paint();
paintProgressLoaded.setColor(loadedColor);
paintProgressLoaded.setStyle(Paint.Style.FILL);
paintProgressLoaded.setAntiAlias(true);

handlerProgress = new Handler();
}

private int calculatePositionIndex(int currentProgress) {
return (currentProgress * mWidth) / maxProgress;
}

public boolean isPlaying() {
return isPlaying;
}

public void cancel() {
isPlaying = false;
currentProgress = 0;
handlerProgress.removeCallbacks(mRunnableProgress);
postInvalidate();
}

public void setEmptyColor(int emptyColor){
if (paintProgressEmpty != null) {
paintProgressEmpty.setColor(emptyColor);
}
}

public void setLoadedColor(int loadedColor){
if (paintProgressLoaded != null) {
paintProgressLoaded.setColor(loadedColor);
}
}

public void setCurrentProgress(int currentProgress) {
this.currentProgress = currentProgress * 10;
postInvalidate();
}

public void setMaxProgress(int maxProgress) {
this.maxProgress = maxProgress * 10;
postInvalidate();
}

public void setAutoProgress(boolean isAutoProgress) {
this.isAutoProgress = isAutoProgress;
}

public void setProgressLayoutListener(ProgressLayoutListener progressLayoutListener) {
this.progressLayoutListener = progressLayoutListener;
}

public int getLoadedColor() {
return loadedColor;
}

public int getEmptyColor() {
return emptyColor;
}

public int getMaxProgress() {
return maxProgress;
}

public int getCurrentProgress() {
return currentProgress;
}

private final Runnable mRunnableProgress = new Runnable() {
@Override public void run() {
if (isPlaying) {
if (currentProgress == maxProgress) {
if (progressLayoutListener != null) {
progressLayoutListener.onProgressCompleted();
}
currentProgress = 0;
setCurrentProgress(currentProgress);
stop();
} else {
postInvalidate();
currentProgress += 1;
if (progressLayoutListener != null) {
progressLayoutListener.onProgressChanged(currentProgress / 10);
}
handlerProgress.postDelayed(mRunnableProgress, PROGRESS_SECOND_MS / 10);
private static final int COLOR_EMPTY_DEFAULT = 0x00000000;
private static final int COLOR_LOADED_DEFAULT = 0x11FFFFFF;
private static final int PROGRESS_SECOND_MS = 1000;

private Paint paintProgressLoaded;
private Paint paintProgressEmpty;
private int loadedColor;
private int emptyColor;
private boolean isPlaying = false;
private boolean isAutoProgress;

private int mHeight;
private int mWidth;
private int maxProgress;
private int currentProgress = 0;

private Handler handlerProgress;

private ProgressLayoutListener progressLayoutListener;


public ProgressLayout(Context context) {
this(context, null);
}

public ProgressLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

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

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ProgressLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}

@Override public boolean isRunning() {
return isPlaying;
}

@Override public void start() {
if (isAutoProgress) {
isPlaying = true;
handlerProgress.removeCallbacksAndMessages(null);
handlerProgress.postDelayed(mRunnableProgress, 0);
}
}

@Override public void stop() {
isPlaying = false;
handlerProgress.removeCallbacks(mRunnableProgress);
postInvalidate();
}

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
}

@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(0, 0, mWidth, mHeight, paintProgressEmpty);
canvas.drawRect(0, 0, calculatePositionIndex(currentProgress), mHeight, paintProgressLoaded);
}

private void init(Context context, AttributeSet attrs) {
setWillNotDraw(false);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.progressLayout);
isAutoProgress = a.getBoolean(R.styleable.progressLayout_autoProgress, true);
maxProgress = a.getInt(R.styleable.progressLayout_maxProgress, 0);
maxProgress = maxProgress * 10;
loadedColor = a.getColor(R.styleable.progressLayout_loadedColor, COLOR_LOADED_DEFAULT);
emptyColor = a.getColor(R.styleable.progressLayout_emptyColor, COLOR_EMPTY_DEFAULT);
a.recycle();

paintProgressEmpty = new Paint();
paintProgressEmpty.setColor(emptyColor);
paintProgressEmpty.setStyle(Paint.Style.FILL);
paintProgressEmpty.setAntiAlias(true);

paintProgressLoaded = new Paint();
paintProgressLoaded.setColor(loadedColor);
paintProgressLoaded.setStyle(Paint.Style.FILL);
paintProgressLoaded.setAntiAlias(true);

handlerProgress = new Handler();
}

private int calculatePositionIndex(int currentProgress) {
return (currentProgress * mWidth) / maxProgress;
}

public boolean isPlaying() {
return isPlaying;
}

public void cancel() {
isPlaying = false;
currentProgress = 0;
handlerProgress.removeCallbacks(mRunnableProgress);
postInvalidate();
}

public void setEmptyColor(int emptyColor){
if (paintProgressEmpty != null) {
paintProgressEmpty.setColor(emptyColor);
this.emptyColor = emptyColor;
postInvalidate();
}
}

public void setLoadedColor(int loadedColor){
if (paintProgressLoaded != null) {
paintProgressLoaded.setColor(loadedColor);
this.loadedColor = loadedColor;
postInvalidate();
}
}
}
};

public void setCurrentProgress(int currentProgress) {
this.currentProgress = currentProgress * 10;
postInvalidate();
}

public void setMaxProgress(int maxProgress) {
this.maxProgress = maxProgress * 10;
postInvalidate();
}

public void setAutoProgress(boolean isAutoProgress) {
this.isAutoProgress = isAutoProgress;
}

public void setProgressLayoutListener(ProgressLayoutListener progressLayoutListener) {
this.progressLayoutListener = progressLayoutListener;
}

public int getLoadedColor() {
return loadedColor;
}

public int getEmptyColor() {
return emptyColor;
}

public int getMaxProgress() {
return maxProgress;
}

public int getCurrentProgress() {
return currentProgress;
}

private final Runnable mRunnableProgress = new Runnable() {
@Override public void run() {
if (isPlaying) {
if (currentProgress == maxProgress) {
if (progressLayoutListener != null) {
progressLayoutListener.onProgressCompleted();
}
currentProgress = 0;
setCurrentProgress(currentProgress);
stop();
} else {
postInvalidate();
currentProgress += 1;
if (progressLayoutListener != null) {
progressLayoutListener.onProgressChanged(currentProgress / 10);
}
handlerProgress.postDelayed(mRunnableProgress, PROGRESS_SECOND_MS / 10);
}
}
}
};

}

0 comments on commit a5a28f3

Please sign in to comment.