java DualCustomised Progress Bar(用户无法与UI交互,直到任务完成)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java DualCustomised Progress Bar(用户无法与UI交互,直到任务完成)相关的知识,希望对你有一定的参考价值。
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="default_thickness">3dp</dimen>
<dimen name="default_inner_padding">8dp</dimen>
<integer name="default_anim_duration">5000</integer>
<integer name="default_anim_step">3</integer>
<integer name="default_start_angle">90</integer>
</resources>
// for tab w820dp
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
// add this attribute file in order to customise
<resources>
<declare-styleable name="DualProgressView">
<attr name="dpv_anim_duration" format="integer" />
<attr name="dpv_outer_color" format="color" />
<attr name="dpv_inner_color" format="color" />
<attr name="dpv_thickness" format="dimension" />
<attr name="dpv_inner_padding" format="dimension" />
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.custom.progress.MainActivity">
<com.custom.progress.DualProgressView
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_centerInParent="true"
app:dpv_thickness="@dimen/default_thickness"
android:id="@+id/progressBar"
android:indeterminateDrawable="@drawable/progress_rotational_background"
app:dpv_inner_padding="@dimen/default_inner_padding"
app:dpv_inner_color="@android:color/transparent" // user ur different color
app:dpv_outer_color="#FFFA5610" />
</RelativeLayout>
public class MainActivity extends AppCompatActivity implements DualProgressView.cancelBack {
DualProgressView progressView;
boolean flag = false;
private boolean infinteBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressView = (DualProgressView) findViewById(R.id.progressBar);
}
@Override
public void setCancelable(boolean flag) {
this.flag = flag;
if(flag){
onBackPressed();
}
else {
finish();
}
}
@Override
public void onBackPressed() {
dontGoBack();
}
private void dontGoBack() {
if (infinteBack) {
finish();
return;
}
/*this.infinteBack = true;*/
Toast.makeText(this, "can't go back while loading", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
infinteBack = false;
}
},0);
}
}
public class DualProgressView extends View {
private static final float INDETERMINANT_MIN_SWEEP = 15f;
/**
* Draw outer progress
*/
private Paint mOuterCirclePaint;
/**
* Draw inner progress
*/
private Paint mInnerCirclePaint;
/**
* Thickness of the progress
*/
private float mThickness;
/**
* Padding between the two circles
*/
private float mInnerPadding;
/**
* Animation duration
*/
private int mAnimDuration;
/**
* Rect for drawing outer circle
*/
private RectF mOuterCircleRect;
/**
* Rect for drawing inner circle
*/
private RectF mInnerCircleRect;
/**
* Outer Circle Color
*/
@ColorInt
private int mOuterCircleColor;
/**
* Inner Circle Color
*/
@ColorInt
private int mInnerCircleColor;
/**
* Number of step in the Animation
**/
private int mSteps;
/**
* Actual size of the complete circle.
**/
private int mSize;
/**
* Starting Angle to start the progress Animation.
*/
private float mStartAngle;
/***
* Sweep Angle
*/
private float mIndeterminateSweep;
/**
* Rotation offset
*/
private float mIndeterminateRotateOffset;
/**
* Progress Animation set
*/
private AnimatorSet mIndeterminateAnimator;
public DualProgressView(Context context) {
super(context);
init(null, 0);
}
public DualProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public DualProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr);
}
/**
* Initialize all drawing parameters from the custom Attributes.
*
* @param attrs
* @param defStyle
*/
protected void init(AttributeSet attrs, int defStyle) {
mOuterCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mInnerCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mOuterCircleRect = new RectF();
mInnerCircleRect = new RectF();
Resources resources = getResources();
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.DualProgressView, defStyle, 0);
mThickness = a.getDimensionPixelSize(R.styleable.DualProgressView_dpv_thickness,
resources.getDimensionPixelSize(R.dimen.default_thickness));
mInnerPadding = a.getDimensionPixelSize(R.styleable.DualProgressView_dpv_inner_padding,
resources.getDimensionPixelSize(R.dimen.default_inner_padding));
mOuterCircleColor = a.getColor(R.styleable.DualProgressView_dpv_outer_color,
ContextCompat.getColor(getContext(), R.color.colorPrimary));
mInnerCircleColor = a.getColor(R.styleable.DualProgressView_dpv_inner_color,
ContextCompat.getColor(getContext(), R.color.colorAccent));
mAnimDuration = a.getInteger(R.styleable.DualProgressView_dpv_anim_duration,
resources.getInteger(R.integer.default_anim_duration));
mSteps = resources.getInteger(R.integer.default_anim_step);
mStartAngle = resources.getInteger(R.integer.default_start_angle);
a.recycle();
setPaint();
}
/**
* Set the two paint object with
* supplied color for drawing.
*/
private void setPaint() {
mOuterCirclePaint.setColor(mOuterCircleColor);
mOuterCirclePaint.setStyle(Paint.Style.STROKE);
mOuterCirclePaint.setStrokeWidth(mThickness);
mOuterCirclePaint.setStrokeCap(Paint.Cap.BUTT);
mInnerCirclePaint.setColor(mInnerCircleColor);
mInnerCirclePaint.setStyle(Paint.Style.STROKE);
mInnerCirclePaint.setStrokeWidth(mThickness);
mInnerCirclePaint.setStrokeCap(Paint.Cap.BUTT);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(mOuterCircleRect, mStartAngle + mIndeterminateRotateOffset, mIndeterminateSweep, false, mOuterCirclePaint);
canvas.drawArc(mInnerCircleRect, mStartAngle + mIndeterminateRotateOffset + 180f, mIndeterminateSweep, false, mInnerCirclePaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int xPad = getPaddingLeft() + getPaddingRight();
int yPad = getPaddingTop() + getPaddingBottom();
int width = getMeasuredWidth() - xPad;
int height = getMeasuredHeight() - yPad;
mSize = (width < height) ? width : height;
setMeasuredDimension(mSize + xPad, mSize + yPad);
updateRectAngleBounds();
}
/**
* Set two rectangle bounds for drawing two circles.
*/
private void updateRectAngleBounds() {
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();
mOuterCircleRect.set(paddingLeft + mThickness, paddingTop + mThickness,
mSize - paddingLeft - mThickness, mSize - paddingTop - mThickness);
mInnerCircleRect.set(paddingLeft + mThickness + mInnerPadding,
paddingTop + mThickness + mInnerPadding, mSize - paddingLeft - mThickness - mInnerPadding,
mSize - paddingTop - mThickness - mInnerPadding);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mSize = (w < h) ? w : h;
updateRectAngleBounds();
}
/**
* Create the Circle Progress Animation sequence
*
* @param step
* @return
*/
private AnimatorSet createIndeterminateAnimator(float step) {
final float maxSweep = 360f * (mSteps - 1) / mSteps + INDETERMINANT_MIN_SWEEP;
final float start = -90f + step * (maxSweep - INDETERMINANT_MIN_SWEEP);
// Extending the front of the arc
ValueAnimator frontEndExtend = ValueAnimator.ofFloat(INDETERMINANT_MIN_SWEEP, maxSweep);
frontEndExtend.setDuration(mAnimDuration / mSteps / 2);
frontEndExtend.setInterpolator(new DecelerateInterpolator(1));
frontEndExtend.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mIndeterminateSweep = (Float) animation.getAnimatedValue();
invalidate();
}
});
// Overall rotation
ValueAnimator rotateAnimator1 = ValueAnimator.ofFloat(step * 720f / mSteps, (step + .5f) * 720f / mSteps);
rotateAnimator1.setDuration(mAnimDuration / mSteps / 2);
rotateAnimator1.setInterpolator(new LinearInterpolator());
rotateAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mIndeterminateRotateOffset = (Float) animation.getAnimatedValue();
}
});
// Followed by...
// Retracting the back end of the arc
ValueAnimator backEndRetract = ValueAnimator.ofFloat(start, start + maxSweep - INDETERMINANT_MIN_SWEEP);
backEndRetract.setDuration(mAnimDuration / mSteps / 2);
backEndRetract.setInterpolator(new DecelerateInterpolator(1));
backEndRetract.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mStartAngle = (Float) animation.getAnimatedValue();
mIndeterminateSweep = maxSweep - mStartAngle + start;
invalidate();
}
});
// More overall rotation
ValueAnimator rotateAnimator2 = ValueAnimator.ofFloat((step + .5f) * 720f / mSteps, (step + 1) * 720f / mSteps);
rotateAnimator2.setDuration(mAnimDuration / mSteps / 2);
rotateAnimator2.setInterpolator(new LinearInterpolator());
rotateAnimator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mIndeterminateRotateOffset = (Float) animation.getAnimatedValue();
}
});
AnimatorSet set = new AnimatorSet();
set.play(frontEndExtend).with(rotateAnimator1);
set.play(backEndRetract).with(rotateAnimator2).after(rotateAnimator1);
return set;
}
public void resetAnimation() {
if (mIndeterminateAnimator != null && mIndeterminateAnimator.isRunning())
mIndeterminateAnimator.cancel();
mIndeterminateSweep = INDETERMINANT_MIN_SWEEP;
// Build the whole AnimatorSet
mIndeterminateAnimator = new AnimatorSet();
AnimatorSet prevSet = null, nextSet;
for (int k = 0; k < mSteps; k++) {
nextSet = createIndeterminateAnimator(k);
AnimatorSet.Builder builder = mIndeterminateAnimator.play(nextSet);
if (prevSet != null)
builder.after(prevSet);
prevSet = nextSet;
}
// Listen to end of animation so we can infinitely loop
mIndeterminateAnimator.addListener(new AnimatorListenerAdapter() {
boolean wasCancelled = false;
@Override
public void onAnimationCancel(Animator animation) {
wasCancelled = true;
}
@Override
public void onAnimationEnd(Animator animation) {
if (!wasCancelled)
resetAnimation();
}
});
mIndeterminateAnimator.start();
}
/**
* Starts the progress bar animation.
* (This is an alias of resetAnimation() so it does the same thing.)
*/
public void startAnimation() {
resetAnimation();
}
/**
* Stops the animation
*/
public void stopAnimation() {
if (mIndeterminateAnimator != null) {
mIndeterminateAnimator.cancel();
mIndeterminateAnimator = null;
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
startAnimation();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
stopAnimation();
}
@Override
public void setVisibility(int visibility) {
int currentVisibility = getVisibility();
super.setVisibility(visibility);
if (visibility != currentVisibility) {
if (visibility == View.VISIBLE) {
resetAnimation();
} else if (visibility == View.GONE || visibility == View.INVISIBLE) {
stopAnimation();
}
}
}
// set cancelable false user cant go back untill the fucntioanlity is completed.....
public interface cancelBack{
void setCancelable(boolean flag);
}
}
以上是关于java DualCustomised Progress Bar(用户无法与UI交互,直到任务完成)的主要内容,如果未能解决你的问题,请参考以下文章