Skip to content

Commit

Permalink
修改FailedView
Browse files Browse the repository at this point in the history
  • Loading branch information
imurluck committed Jun 11, 2018
1 parent 888aede commit 0692ac9
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 80 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added READ.ME
Empty file.
Original file line number Diff line number Diff line change
@@ -1,15 +1,82 @@
package com.example.zzx.progressloadingview;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.library.ProgressLoadingView;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

Button startAnimationBtn;
Button successBtn;
Button failedBtn;
Button addBtn;
ProgressLoadingView progressLoadingView;
LinearLayout rootLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**successTickView = (SuccessTickView) findViewById(R.id.success_tick);
startAnimationBtn = (Button) findViewById(R.id.animate_btn);
startAnimationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
successTickView.startTickAnim();
}
});*/
rootLayout = (LinearLayout) findViewById(R.id.root_layout);
progressLoadingView = (ProgressLoadingView) findViewById(R.id.loading_view);
startAnimationBtn = (Button) findViewById(R.id.start_btn);
failedBtn = (Button) findViewById(R.id.failed_btn);
successBtn = (Button) findViewById(R.id.success_btn);
addBtn = (Button) findViewById(R.id.add_btn);
startAnimationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "onClick: " + Thread.currentThread());
progressLoadingView.startAnimation();
}
});
successBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//ProgressLoadingView.startAnimation(2);
progressLoadingView.setResult(ProgressLoadingView.RESULT_SUCCESS);
}
});
failedBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressLoadingView.setResult(ProgressLoadingView.RESULT_FAILED);
}
});


addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressLoadingView view = new ProgressLoadingView.Builder().with(MainActivity.this)
.setColor(Color.BLUE)
.build();
rootLayout.addView(view, 0, new LinearLayout.LayoutParams(200, 200));
view.post(new Runnable() {
@Override
public void run() {
view.startAnimation();;
}
});
}
});

}
}
59 changes: 50 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout 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/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
<!--<com.example.library.SuccessTickView-->
<!--android:id="@+id/success_tick"-->
<!--android:layout_width="100dp"-->
<!--android:layout_height="100dp"-->
<!--android:layout_centerInParent="true"/>-->

<!--<Button-->
<!--android:id="@+id/animate_btn"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="start animation"-->
<!--android:layout_below="@id/success_tick"-->
<!--android:layout_centerHorizontal="true"/>-->

<com.example.library.ProgressLoadingView
android:id="@+id/loading_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#88ffffff"
app:styleName="arcRotate"
app:color="#15daec"/>

<Button
android:id="@+id/start_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="StartAnimation" />

<Button
android:id="@+id/success_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Success"
/>

<Button
android:id="@+id/failed_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Failed"
/>

<Button
android:id="@+id/add_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:text="Add new LoadingView"/>

</android.support.constraint.ConstraintLayout>
</LinearLayout>
74 changes: 19 additions & 55 deletions library/src/main/java/com/example/library/FailedView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
Expand All @@ -14,15 +14,12 @@ public class FailedView extends ResultView {

private Paint mPaint;

private boolean mIsLeftFinished;

private Animation mFailedAnimation;

private float mRightStartX;
private float mRightStartY;

private int mErrorTextAlpha;
private String mErrorContent;

private int mStyle;

Expand All @@ -38,28 +35,26 @@ public void init() {
mPaint.setAntiAlias(true);
}

private void initOption() {
mIsLeftFinished = false;
mLeftStartX = mLeftIntersectingX;
protected void initOptions() {
Log.e(TAG, "initOptions: ");
mLeftStartX = mLeftIntersectingX * 0.5f;
mLeftStartY = calculateLeftY(mLeftStartX);
mLeftEndX = mLeftStartX;
mLeftEndY = mLeftStartY;

mRightRatio = 1.0f;
mRightStartX = mRightIntersectingX * mRightRatio - 1.0f / 4.0f * mRightIntersectingX;
mRightStartX = mRightIntersectingX * 0.5f;
mRightStartY = calculateRightY(mRightStartX);
mRightEndX = mRightStartX;
mRightEndY = calculateRightY(mRightEndY);
mRightEndY = calculateRightY(mRightEndX);

mErrorTextAlpha = 0x00;
}

@Override
protected void config(ProgressLoadingView.Builder.Config config) {
mStrokeColor = config.color;
mStrokeBackgroundColor = config.backgroundColor;
mErrorContent = config.errorContent;
mStyle = config.style;
mOffsetYRatio = 0.0f;
}

@Override
Expand All @@ -68,6 +63,7 @@ protected void onDraw(Canvas canvas) {
}

private void drawFailed(Canvas canvas) {
Log.e(TAG, "drawFailed: ");
if (mStyle == ProgressLoadingView.Builder.STYLE_ARC_ROTATE) {
mPaint.setStyle(Paint.Style.STROKE);
} else {
Expand All @@ -80,60 +76,28 @@ private void drawFailed(Canvas canvas) {
mPaint.setColor(mStrokeColor);
canvas.drawLine(realX(mLeftStartX), realY(mLeftStartY),
realX(mLeftEndX), realY(mLeftEndY), mPaint);
if (mIsLeftFinished) {
canvas.drawLine(realX(mRightStartX), realY(mRightStartY),
realX(mRightEndX), realY(mRightEndY), mPaint);
drawErrorText(canvas);
}
}

private void drawErrorText(Canvas canvas) {
mPaint.setTextSize(3 * mStrokeWidth);
String content = mErrorContent;
Rect bounds = new Rect();
mPaint.getTextBounds(content, 0, content.length(), bounds);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setStrokeWidth(3);
mPaint.setAlpha(mErrorTextAlpha);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawText(content, mHalfWidth + mOffsetYRatio,
mHalfHeight - mOffsetYRatio * mHalfHeight, mPaint);
canvas.drawLine(realX(mRightStartX), realY(mRightStartY),
realX(mRightEndX), realY(mRightEndY), mPaint);
}

@Override
protected Animation getAnimation() {
initOption();
initOptions();
mFailedAnimation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (0.0f < interpolatedTime && interpolatedTime <= 0.5f) {
mLeftEndX = (-1.0f * interpolatedTime * 3.0f + 1.0f)
* mLeftIntersectingX;//最后加上误差
mLeftEndY = calculateLeftY(mLeftEndX);
//Log.e(TAG, "applyTransformation: interpolatedTime = " + interpolatedTime);
//Log.e(TAG, "applyTransformation: mLeftStartX = " + mLeftStartX + " mRightStartX = " + mRightStartX);
//Log.e(TAG, "applyTransformation: mLeftEndX = " + mLeftEndX + " mRightEndX = " + mRightEndX);
mView.invalidate();
} else if (0.5f < interpolatedTime && interpolatedTime <= 1.0f) {
mIsLeftFinished = true;

mLeftStartX = (1.0f - (interpolatedTime - 0.5f) / 2.0f)
* mLeftIntersectingX;
mLeftStartY = calculateLeftY(mLeftStartX);

mRightEndX = (-1.0f * (interpolatedTime - 0.5f) * 5.0f / 2.0f + 3.0f / 4.0f)
* mRightIntersectingX;//最后加上误差
mRightEndY = calculateRightY(mRightEndX);
//Log.e(TAG, "applyTransformation: interpolatedTime = " + interpolatedTime);
//Log.e(TAG, "applyTransformation: mLeftStartX = " + mLeftStartX + " mRightStartX = " + mRightStartX);
//Log.e(TAG, "applyTransformation: mLeftEndX = " + mLeftEndX + " mRightEndX = " + mRightEndX);
mErrorTextAlpha = ((int) ((interpolatedTime - 0.5f) * 2.0f)) * 0xFF;
mView.invalidate();
}

mLeftEndX = mLeftIntersectingX * 0.5f - interpolatedTime * mLeftIntersectingX;
mLeftEndY = calculateLeftY(mLeftEndX);

mRightEndX = mRightIntersectingX * 0.5f - interpolatedTime * mRightIntersectingX;
mRightEndY = calculateRightY(mRightEndX);
mView.invalidate();
}
};
mFailedAnimation.setDuration(1000);
mFailedAnimation.setDuration(700);
mFailedAnimation.setStartOffset(100);
return mFailedAnimation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public ProgressLoadingView(Context context, AttributeSet attrs, int defStyle) {
config.color = array.getColor(R.styleable.LoadingView_color, Builder.DEFAULT_COLOR);
config.backgroundColor = array.getColor(R.styleable.LoadingView_backgroundColor,
Builder.getBackgroundColor(config.color));
String error = array.getString(R.styleable.LoadingView_errorContent);
config.errorContent = error == null ? Builder.DEFAULT_ERROR_CONTENT : error;
config.style = array.getInt(R.styleable.LoadingView_styleName, Builder.DEFAULT_STYLE);
config(config);
}
Expand Down Expand Up @@ -140,7 +138,6 @@ public static class Builder {
private static final int DEFAULT_COLOR = 0xFF00FF00;
private static final int DEFAULT_BACKGROUND_COLOR = 0x1100FF00;
private static final int DEFAULT_STYLE = STYLE_WATER_ROTATE;
private static final String DEFAULT_ERROR_CONTENT = "error";

private Config mConfig = new Config();

Expand All @@ -164,11 +161,6 @@ public Builder setStyle(int style) {
return this;
}

public Builder setErrorContent(String errorContent) {
mConfig.errorContent = errorContent;
return this;
}

public ProgressLoadingView build() {
if (mConfig.context == null) {
throw new NullPointerException("there is no context, please create the LoadingView " +
Expand All @@ -193,7 +185,6 @@ static class Config {
int color = DEFAULT_COLOR;
int backgroundColor = DEFAULT_BACKGROUND_COLOR;
int style = DEFAULT_STYLE;
String errorContent = DEFAULT_ERROR_CONTENT;
}
}

Expand Down
5 changes: 1 addition & 4 deletions library/src/main/java/com/example/library/ResultView.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,9 @@ public void onSizeChanged(int w, int h, int oldw, int oldh) {
calculateRightIntersectingPoint();
calculateCenterIntersectingPoint();
calculateStartAngle();
/**mLeftX = mLeftIntersectingX * mLeftRatio;
mLeftY = calculateLeftY(mLeftX);
mRightX = mRightIntersectingX * mRightRatio;
mRightY = calculateRightY(mRightX);*/
}


protected void calculateStartAngle() {
float leftIntersectingY = calculateLeftY(mLeftIntersectingX);
mStartAngle = (float) (-1 * Math.atan(mLeftIntersectingX / leftIntersectingY)
Expand Down
3 changes: 0 additions & 3 deletions library/src/main/java/com/example/library/SuccessView.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ protected void applyTransformation(float interpolatedTime, Transformation t) {
mLeftEndY = calculateLeftY(mLeftEndX);
mView.invalidate();
} else if (0.2 < interpolatedTime && 0.8 >= interpolatedTime) {
/**mLeftRatio = 1.0f - (0.5f + (interpolatedTime - 0.2f) * 5 / 12);
mLeftX = mLeftIntersectingX * mLeftRatio;
mLeftY = calculateLeftY(mLeftX);*/
mLeftRatio = 1.0f - (interpolatedTime - 0.2f) * 5 / 3;
mLeftStartX = mLeftIntersectingX * mLeftRatio;
mLeftStartY = calculateLeftY(mLeftStartX);
Expand Down

0 comments on commit 0692ac9

Please sign in to comment.