无意中看见一个炫酷的加载库:SpinKit (地址:http://ybq.github.io/Android-SpinKit/)
效果如下:
那么我想要一个类似IOS的加载呢?
看上面的效果FadingCircle跟IOS的效果相似,那么我们就基于FadingCircle改成IOS加载的效果
首先:在sprite目录下创建 RectLayoutContainer.java
public abstract class RectLayoutContainer extends SpriteContainer { @Override public void drawChild(Canvas canvas) { for (int i = 0; i < getChildCount(); i++) { Sprite sprite = getChildAt(i); int count = canvas.save(); canvas.rotate(i * 360 / getChildCount(), getBounds().centerX(), getBounds().centerY()); sprite.draw(canvas); canvas.restoreToCount(count); } } @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); bounds = clipSquare(bounds); int radius = (int) (bounds.width() / getChildCount() / 2); int left = bounds.centerX() - radius; int right = bounds.centerX() + radius; for (int i = 0; i < getChildCount(); i++) { Sprite sprite = getChildAt(i); sprite.setDrawBounds(left, bounds.top, right, bounds.top + radius * 5); } } }
在style目录下创建FadingRect.java ,注意,我们是要画一个矩形,所以继承的是RectSprite
public class FadingRect extends RectLayoutContainer { @Override public Sprite[] onCreateChild() { RectItem[] dots = new RectItem[12]; for (int i = 0; i < dots.length; i++) { dots[i] = new RectItem(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { dots[i].setAnimationDelay(1200 / 12 * i); } else { dots[i].setAnimationDelay(1200 / 12 * i + -1200); } } return dots; } private class RectItem extends RectSprite { RectItem() { setAlpha(0); } @Override public ValueAnimator onCreateAnimation() { float fractions[] = new float[]{0f, 0.39f, 0.4f, 1f}; return new SpriteAnimatorBuilder(this). alpha(fractions, 50, 50, 255, 50). duration(1200). easeInOut(fractions).build(); } } }
然后在枚举类Style.java中添加 :
FADING_RECT(15);
在SpriteFactory.Java增加:
case FADING_RECT:
sprite = new FadingRect();
break;
在然后向XML文件attr、style添加相对应的就可以了!
demo : 点击打开链接