一款非常簡單酷炫的LoadingView動畫效果
今天看到一個銀行的APP上面的loadingview 挺好的,就嘗試著自己實現(xiàn),覺得很簡單,但自己實現(xiàn)起來還是發(fā)現(xiàn)了一些問題。
LoadingView和下圖類似:

實現(xiàn)的代碼也不是很復(fù)雜,就是小球的運動軌跡需要計算,我自己手畫了個計算的圖,很簡單的就是三角函數(shù)的使用。

然后代碼就是代碼實現(xiàn)了,主要的內(nèi)容都有注釋,代碼如下:
public class LoadingView extends View {
private final static String TAG = "LoadingView";
private final static int LEFT_BALL_DOWN = 1;
private final static int LEFT_BALL_UP = 2;
private final static int RIGHT_BALL_DOWN = 3;
private final static int RIGHT_BALL_UP = 4;
private Paint paint1, paint2, paint3, paint4, paint5;
private int mCurrentAnimatorValue;
private int circleRadius = 10; //小球的半徑
private int distance = 60; //小球開始下落到最低點的距離
private int mCurrentState = LEFT_BALL_DOWN;
public LoadingView(Context context) {
super(context);
init(context);
}
public LoadingView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
paint1 = getPaint(Color.RED);
paint2 = getPaint(Color.YELLOW);
paint3 = getPaint(Color.GREEN);
paint4 = getPaint(Color.BLUE);
paint5 = getPaint(Color.CYAN);
ValueAnimator animator = ValueAnimator.ofInt(0, 90);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mCurrentAnimatorValue = (int) animation.getAnimatedValue();
Log.e(TAG, "onAnimationUpdate : mCurrentAnimatorValue = " + mCurrentAnimatorValue);
invalidate();
}
});
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.e(TAG, "onAnimationRepeat : mCurrentAnimatorValue = " + mCurrentAnimatorValue);
switch (mCurrentState) {
case LEFT_BALL_DOWN:
mCurrentState = RIGHT_BALL_UP;
break;
case RIGHT_BALL_UP:
mCurrentState = RIGHT_BALL_DOWN;
break;
case RIGHT_BALL_DOWN:
mCurrentState = LEFT_BALL_UP;
break;
case LEFT_BALL_UP:
mCurrentState = LEFT_BALL_DOWN;
break;
}
}
});
animator.setStartDelay(500);
animator.setDuration(600);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new DecelerateInterpolator());
animator.start();
}
private Paint getPaint(int color) {
Paint paint = new Paint();
paint.setColor(color);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
return paint;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x, y;
double cosValue = Math.cos(PI * mCurrentAnimatorValue / 180);
double sinValue = Math.sin(PI * mCurrentAnimatorValue / 180);
drawFourBall(canvas);
switch (mCurrentState) {
case LEFT_BALL_DOWN://最左邊小球往下撞擊
x = circleRadius + (int) ((distance - circleRadius) * (1 - cosValue));
y = getHeight() - distance + (int) ((distance - circleRadius) * sinValue);
canvas.drawCircle(x, y, circleRadius, paint1);
break;
case RIGHT_BALL_UP://最右邊小球往上撞擊
x = distance + 8 * circleRadius + (int) ((distance - circleRadius) * sinValue);
y = getHeight() - distance + (int) (cosValue * (distance - circleRadius));
canvas.drawCircle(x, y, circleRadius, paint5);
break;
case RIGHT_BALL_DOWN://最右邊小球往下撞擊
x = distance + 8 * circleRadius + (int) ((distance - circleRadius) * (cosValue));
y = (getHeight() - distance) + (int) ((distance - circleRadius) * (sinValue));
canvas.drawCircle(x, y, circleRadius, paint5);
break;
case LEFT_BALL_UP://最左邊小球往上撞擊
x = distance - (int) ((distance - circleRadius) * sinValue);
y = getHeight() - distance + (int) ((distance - circleRadius) * cosValue);
canvas.drawCircle(x, y, circleRadius, paint1);
break;
}
}
private void drawFourBall(Canvas canvas) {
int y = getHeight() - circleRadius;
canvas.drawCircle(distance + 2 * circleRadius, y, circleRadius, paint2);
canvas.drawCircle(distance + 4 * circleRadius, y, circleRadius, paint3);
canvas.drawCircle(distance + 6 * circleRadius, y, circleRadius, paint4);
if (mCurrentState == LEFT_BALL_DOWN || mCurrentState == LEFT_BALL_UP) {//最左邊球運動的時候,要繪制最右邊的球
canvas.drawCircle(distance + 8 * circleRadius, y, circleRadius, paint5);
} else if (mCurrentState == RIGHT_BALL_UP || mCurrentState == RIGHT_BALL_DOWN) {//最右邊球運動的時候,要繪制最左邊的球
canvas.drawCircle(distance, y, circleRadius, paint1);
}
}
}
實現(xiàn)的效果如圖一,有問題的話互相討論。最后貼上想xml文件,后續(xù)會完善設(shè)置loadingview的大小和顏色之類的參數(shù)。
xml如下:
<com.define_view.LoadingView
android:layout_marginTop="20px"
android:background="#999999"
android:layout_width="200px"
android:layout_height="200px" />
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android布局加載之LayoutInflater示例詳解
這篇文章主要介紹了Android布局加載之LayoutInflater的相關(guān)資料,文中介紹的非常詳細,對大家具有一定的參考借鑒價值,需要的朋友們下面來一起看看吧。2017-03-03
flutter實現(xiàn)更新彈窗內(nèi)容例子(親測有效)
Flutter是一款移動應(yīng)用程序SDK,包含框架、widget和工具,這篇文章給大家介紹flutter實現(xiàn)更新彈窗內(nèi)容例子,親測可以使用,需要的朋友參考下吧2021-04-04
舉例講解Android應(yīng)用開發(fā)中OTTO框架的基本使用
這篇文章主要介紹了Android應(yīng)用開發(fā)中OTTO框架的基本使用講解,文中舉了創(chuàng)建一個單例模式的應(yīng)用例子,需要的朋友可以參考下2016-02-02
Android實現(xiàn)簡潔的APP更新dialog數(shù)字進度條
這篇文章主要為大家詳細介紹了Android實現(xiàn)簡潔的APP更新dialog數(shù)字進度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
Android SDK 百度地圖通過poi城市內(nèi)檢索簡介接口的使用
這篇文章主要介紹了Android SDK 百度地圖通過poi城市內(nèi)檢索簡介接口的使用的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android編程實現(xiàn)EditText字數(shù)監(jiān)聽并顯示的方法
這篇文章主要介紹了Android編程實現(xiàn)EditText字數(shù)監(jiān)聽并顯示的方法,涉及Android EditText文本框事件監(jiān)聽與響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-02-02

