
在github上找的一个有点酷炫的loading动画https://github.com/Fichardu/CircleProgress
我写写使用步骤
自定义view(CircleProgress )的代码
package com.hysmarthotel.vIEw;import com.hysmarthotel.roomcontrol.R;import com.hysmarthotel.util.EaseInOutCubicInterpolator;import androID.animation.TimeInterpolator;import androID.content.Context;import androID.content.res.TypedArray;import androID.graphics.Canvas;import androID.graphics.Paint;import androID.graphics.Point;import androID.util.AttributeSet;import androID.vIEw.VIEw;import androID.vIEw.animation.AnimationUtils;public class CircleProgress extends VIEw { private static final int RED = 0xFFE5282C; private static final int YELLOW = 0xFF1F909A; private static final int BLUE = 0xFFFC9E12; private static final int color_NUM = 3; private int[] colorS; private TimeInterpolator mInterpolator = new EaseInOutCubicInterpolator(); private final double DEGREE = Math.PI / 180; private Paint mPaint; private int mVIEwSize; private int mPoinTradius; private long mStartTime; private long mPlayTime; private boolean mStartAnim = false; private Point mCenter = new Point(); private ArcPoint[] marcPoint; private static final int POINT_NUM = 15; private static final int DELTA_ANGLE = 360 / POINT_NUM; private long mDuration = 3600; public CircleProgress(Context context) { super(context); init(null,0); } public CircleProgress(Context context,AttributeSet attrs) { super(context,attrs); init(attrs,AttributeSet attrs,int defStyle) { super(context,attrs,defStyle); init(attrs,defStyle); } private voID init(AttributeSet attrs,int defStyle) { marcPoint = new ArcPoint[POINT_NUM]; mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.CircleProgress,defStyle,0); int color1 = a.getcolor(R.styleable.CircleProgress_color1,RED); int color2 = a.getcolor(R.styleable.CircleProgress_color2,YELLOW); int color3 = a.getcolor(R.styleable.CircleProgress_color3,BLUE); a.recycle(); colorS = new int[]{color1,color2,color3}; } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { int defaultSize = getResources().getDimensionPixelSize(R.dimen.default_circle_vIEw_size); int wIDth = getDefaultSize(defaultSize,wIDthMeasureSpec); int height = getDefaultSize(defaultSize,heightmeasureSpec); mVIEwSize = Math.min(wIDth,height); setMeasuredDimension(mVIEwSize,mVIEwSize); mCenter.set(mVIEwSize / 2,mVIEwSize / 2); calPoints(1.0f); } @OverrIDe protected voID onDraw(Canvas canvas) { canvas.save(); canvas.translate(mCenter.x,mCenter.y); float factor = getFactor(); canvas.rotate(36 * factor); float x,y; for (int i = 0; i < POINT_NUM; ++i) { mPaint.setcolor(marcPoint[i].color); float itemFactor = getItemFactor(i,factor); x = marcPoint[i].x - 2 * marcPoint[i].x * itemFactor; y = marcPoint[i].y - 2 * marcPoint[i].y * itemFactor; canvas.drawCircle(x,y,mPoinTradius,mPaint); } canvas.restore(); if (mStartAnim) { postInvalIDate(); } } private voID calPoints(float factor) { int radius = (int) (mVIEwSize / 3 * factor); mPoinTradius = radius / 12; for (int i = 0; i < POINT_NUM; ++i) { float x = radius * -(float) Math.sin(DEGREE * DELTA_ANGLE * i); float y = radius * -(float) Math.cos(DEGREE * DELTA_ANGLE * i); ArcPoint point = new ArcPoint(x,colorS[i % color_NUM]); marcPoint[i] = point; } } private float getFactor() { if (mStartAnim) { mPlayTime = AnimationUtils.currentAnimationTimeMillis() - mStartTime; } float factor = mPlayTime / (float) mDuration; return factor % 1f; } private float getItemFactor(int index,float factor) { float itemFactor = (factor - 0.66f / POINT_NUM * index) * 3; if (itemFactor < 0f) { itemFactor = 0f; } else if (itemFactor > 1f) { itemFactor = 1f; } return mInterpolator.getInterpolation(itemFactor); } public voID startAnim() { mPlayTime = mPlayTime % mDuration; mStartTime = AnimationUtils.currentAnimationTimeMillis() - mPlayTime; mStartAnim = true; postInvalIDate(); } public voID reset() { stopAnim(); mPlayTime = 0; postInvalIDate(); } public voID stopAnim() { mStartAnim = false; } public voID setInterpolator(TimeInterpolator interpolator) { mInterpolator = interpolator; } public voID setDuration(long duration) { mDuration = duration; } public voID seTradius(float factor) { stopAnim(); calPoints(factor); startAnim(); } static class ArcPoint { float x; float y; int color; ArcPoint(float x,float y,int color) { this.x = x; this.y = y; this.color = color; } }}EaseInOutCubicInterpolator是自定义view(CircleProgress )中要是用的一个工具
package com.hysmarthotel.util;import androID.animation.TimeInterpolator;public class EaseInOutCubicInterpolator implements TimeInterpolator { @OverrIDe public float getInterpolation(float input) { if ((input *= 2) < 1.0f) { return 0.5f * input * input * input; } input -= 2; return 0.5f * input * input * input + 1; }}在activity中的调用(还有一些其他用法可以自己看看github上的源代码)
mProgressVIEw = (CircleProgress)findVIEwByID(R.ID.progress_vIE);mProgressVIEw.startAnim(); //开始mProgressVIEw.stopAnim(); //结束mProgressVIEw.seTradius(factor); //半径mProgressVIEw.reset(); //复原
在xml文件中的布局
<?xml version="1.0" enCoding="utf-8"?><absoluteLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:circleprogress="http://schemas.androID.com/apk/res/com.hysmarthotel.roomcontrol" //这个地方记得要加 //包名 androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@drawable/bg1" > <com.hysmarthotel.vIEw.CircleProgress //类名 androID:ID="@+ID/progress_vIE" androID:layout_x="350.5px" androID:layout_y="150.0px" androID:layout_wIDth="1140.0px" androID:layout_height="700.0px" circleprogress:color1="@androID:color/holo_red_light" //这些参数就是通过xmlns:circleprogress,和attrs文件相关联的 circleprogress:color2="@androID:color/holo_green_light" circleprogress:color3="@androID:color/holo_blue_light" />
自己在values目录中新建的attrs文件,这是与自定义view中自定义参数相关的
<declare-styleable name="CircleProgress"> <attr name="color1" format="reference|color"/> <attr name="color2" format="reference|color"/> <attr name="color3" format="reference|color"/> </declare-styleable>
自己在values目录中新建的dimens文件,这个只是几个颜色参数
<?xml version="1.0" enCoding="utf-8"?><resources> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="default_circle_vIEw_size">200dp</dimen></resources>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android自定义加载loading view动画组件全部内容,希望文章能够帮你解决Android自定义加载loading view动画组件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)