最近看到IOS支付宝9.0芝麻信用分效果,感觉很酷,恰好这段时间学习安卓动画,就先拿它小试牛刀,如有雷同,纯属巧合
先看支付宝芝麻信用分图:
抱歉没有做GIF图,大家可以去支付宝看看效果,当打开该界面时,默认展现的灰色正五边形,绿色区域也是与平行于外五边形,不过是半径小于外无边行而已,然后一秒内结合后台数据,计算用户的各项指标,动态刷新绿色区域
现在来看看我们的效果图:
抱歉没有相关图片,只能找相关图片替代了,GIF效果也不是很好,后边会给大家提供下载链接提供下载,大家可以运行看看效果
说说思路:
1.首先是五边形的计算方式,大家都知道,五边形每两条边形成的角度是108度,则中心点形成的角度是72度,问为什么,大家自行百度五边形
2.怎么确定各个点,中心点不用说,View的width 于height的一半,半径则为各个width与height较小的(为了适配个屏幕,我们取小的那个)减去最大图片的宽高
3.怎么绘制五边形,我们现在有了半径,有了中心点,则用path可以确定各个点的坐标了吧,绿色那块,完全可以以半径的百分比确定,默认我们取半径的一半
4.绿色区域效果,可以根据当前的百分比与默认的百分比,这里边用到了ValueAnimator,则可以拿到当前的变化的值,然后动态刷新View即可
5.1秒之后执行效果,则可以开启一个线程,一秒之后执行Value Animator
好了,不说了,上代码:
package wh.sesamecreditview.view;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Region;
import android.os.Looper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import java.math.BigDecimal;
import java.util.ArrayList;
import wh.sesamecreditview.R;
import wh.sesamecreditview.util.DisplayUtil;
/**
* Created by Administrator on 2015/8/5.
*/
public class SesameCreditView extends View {
private Point mCenterPoint = new Point();
private int mViewWidth;
private int mViewHeight;
private int mCenterX;
private int mCenterY;
private int mRadius;
private Paint mDefaultPaint = new Paint();
private Paint mLineDefaultPaint = new Paint();
private Paint mVisivlePaint = new Paint();
private Paint mTextPaint = new Paint();
private Paint mBitmapPaint = new Paint();
private double mIdentityValue;
private double mCreditValue;
private double mCreditHistoryValue;
private double mPepoleRelativeValue;
private double mActionFavoriteValue;
private String mDefaultText = "未知";
private Rect mTextRect = new Rect();
private float mDefaultRatio = 0.5f;
private int mPicAndViewSpacing;
private ArrayList<Region> mPicAreas = new ArrayList<Region>();
priv