android 炫酷的載入庫SpinKit
阿新 • • 發佈:2019-02-05
效果如下:
那麼我想要一個類似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新增相對應的就可以了!