AndroidはiOSの菊の花をまねることを実現して輪のアニメーションの効果をロードします。
一般的な実現方式カット、回転動画を作る カスタムView、描画効果 gif図 1、カット図は体積を増加させますが、比較的簡単ですが、肌の色を変えるシーンでは、色が違っています。複数の図面を準備する必要があり、柔軟性が足りません。
2、カスタムのメリットで、色によってはユーザー定義の属性を提供するだけで、肌を替える時に属性設定を切り替えることができます。比較的に柔軟です。
3、gif図は一般的に比較的大きいです。そして、gifをロードするには元のサポートがなく、第三者の倉庫を導入する必要があります。
効果図:
完全コード
カスタム属性:
レイアウトにコントロールを追加します。適切な時に表示、非表示にしてもいいです。幅、高さ、色、線の幅を設定したほうがいいです。標準の効果は比較的に使えません。
以上はAndroidがiOSの菊の花をまねることを実現して輪のアニメーションの効果の詳しい内容をロードします。
2、カスタムのメリットで、色によってはユーザー定義の属性を提供するだけで、肌を替える時に属性設定を切り替えることができます。比較的に柔軟です。
3、gif図は一般的に比較的大きいです。そして、gifをロードするには元のサポートがなく、第三者の倉庫を導入する必要があります。
効果図:
完全コード
カスタム属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ActivityIndicatorView">
<!-- -->
<attr name="aiv_color" format="color" />
<!-- -->
<attr name="aiv_startAngle" format="integer" />
<!-- -->
<attr name="aiv_strokeWidth" format="dimension" />
<!-- -->
<attr name="aiv_auto_start" format="boolean" />
</declare-styleable>
</resources>
Javaコードの実現過程:
public class ActivityIndicatorView extends View {
/**
*
*/
private int mColor = Color.argb(255, 255, 255, 255);
/**
*
*/
private int mStartAngle = 0;
/**
*
*/
private float mStrokeWidth = 0;
/**
*
*/
private boolean isAutoStart;
/**
*
*/
private final int mLineCount = 12;
private final int minAlpha = 0;
/**
*
*/
private final int mAngleGradient = 360 / mLineCount;
/**
*
*/
private int[] mColors = new int[mLineCount];
/**
*
*/
private Paint mPaint;
/**
* Handler
*/
private Handler mAnimHandler = new Handler(Looper.getMainLooper());
/**
*
*/
private Runnable mAnimRunnable = new Runnable() {
@Override
public void run() {
mStartAngle += mAngleGradient;
invalidate();
mAnimHandler.postDelayed(mAnimRunnable, 50);
}
};
public ActivityIndicatorView(Context context) {
this(context, null);
}
public ActivityIndicatorView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ActivityIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setup(context, attrs, defStyleAttr, 0);
}
private void setup(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ActivityIndicatorView, defStyleAttr, defStyleRes);
mColor = typedArray.getColor(R.styleable.ActivityIndicatorView_aiv_color, mColor);
mStartAngle = typedArray.getInt(R.styleable.ActivityIndicatorView_aiv_startAngle, mStartAngle);
mStrokeWidth = typedArray.getDimension(R.styleable.ActivityIndicatorView_aiv_strokeWidth, mStrokeWidth);
isAutoStart = typedArray.getBoolean(R.styleable.ActivityIndicatorView_aiv_auto_start, true);
typedArray.recycle();
initialize();
}
private void initialize() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
int alpha = Color.alpha(mColor);
int red = Color.red(mColor);
int green = Color.green(mColor);
int blue = Color.blue(mColor);
int alphaGradient = Math.abs(alpha - minAlpha) / mLineCount;
for (int i = 0; i < mColors.length; i++) {
mColors[i] = Color.argb(alpha - alphaGradient * i, red, green, blue);
}
mPaint.setStrokeCap(Paint.Cap.ROUND);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
float radius = Math.min(getWidth() - getPaddingLeft() - getPaddingRight(), getHeight() - getPaddingTop() - getPaddingBottom()) * 0.5f;
if (mStrokeWidth == 0) {
mStrokeWidth = pointX(mAngleGradient / 2, radius / 2) / 2;
}
mPaint.setStrokeWidth(mStrokeWidth);
for (int i = 0; i < mColors.length; i++) {
mPaint.setColor(mColors[i]);
canvas.drawLine(
centerX + pointX(-mAngleGradient * i + mStartAngle, radius / 2),
centerY + pointY(-mAngleGradient * i + mStartAngle, radius / 2),
// Y , /2, Padding , View
centerX + pointX(-mAngleGradient * i + mStartAngle, radius - mStrokeWidth / 2),
// Y , /2, Padding , View
centerY + pointY(-mAngleGradient * i + mStartAngle, radius - mStrokeWidth / 2),
mPaint);
}
}
private float pointX(int angle, float radius) {
return (float) (radius * Math.cos(angle * Math.PI / 180));
}
private float pointY(int angle, float radius) {
return (float) (radius * Math.sin(angle * Math.PI / 180));
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (isAutoStart) {
start();
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mAnimHandler != null) {
stop();
}
}
/**
*
*/
public void start() {
mAnimHandler.post(mAnimRunnable);
}
/**
*
*/
public void stop() {
mAnimHandler.removeCallbacks(mAnimRunnable);
}
/**
*
*/
public void setColor(int color) {
this.mColor = color;
}
/**
*
*/
public void setStrokeWidth(float strokeWidth) {
this.mStrokeWidth = strokeWidth;
}
/**
*
*/
public void setStartAngle(int startAngle) {
this.mStartAngle = startAngle;
}
}
レイアウトコードレイアウトにコントロールを追加します。適切な時に表示、非表示にしてもいいです。幅、高さ、色、線の幅を設定したほうがいいです。標準の効果は比較的に使えません。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.tongwei.bootrunsample.base.widget.ActivityIndicatorView
android:id="@+id/activity_indicator"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center"
android:layout_centerInParent="true"
app:aiv_color="#A2A3B0"
app:aiv_strokeWidth="2.5dp" />
</RelativeLayout>
ここで終わります。以上はAndroidがiOSの菊の花をまねることを実現して輪のアニメーションの効果の詳しい内容をロードします。