老司機(jī)吴旋,不存在的损肛,其實(shí)我還算不上厢破,哈哈。治拿。摩泪。
最近公司產(chǎn)品突然有一個類似支付寶螞蟻森林的功能,大致功能跟支付寶螞蟻森林相像劫谅,在看了一下支付寶螞蟻森林的效果之后见坑,打算先擼一個控件出來,等公司效果圖出來之后就可以放上去直接使用捏检。
首先我們先大致看下支付寶的螞蟻森林效果圖:
這是目前我實(shí)現(xiàn)的效果圖:
當(dāng)我們拿到這個需求時先分析一波鳄梅,不要忙著就動手開干,不然容易平地翻車未檩。戴尸。
需要實(shí)現(xiàn)的功能有:
1、自定小圓球冤狡,圓球內(nèi)文字孙蒙、上下浮動、消失動畫悲雳;
2挎峦、根據(jù)數(shù)據(jù)動態(tài)添加小球,并且位置隨機(jī)分布在小樹周圍合瓢,不能重疊坦胶。這點(diǎn)是最重要的,涉及到一個隨機(jī)位置生成算法的設(shè)計晴楔。
好了顿苇,當(dāng)我們確定了我們要實(shí)現(xiàn)的功能之后就可以逐步開始擼代碼了。
自定義圓球
這個比較容易實(shí)現(xiàn)税弃,繪制一個圓纪岁,再在園內(nèi)繪制文字,動畫實(shí)現(xiàn)統(tǒng)一采用的是屬性動畫來實(shí)現(xiàn)则果,代碼如下幔翰,注釋寫的比較詳細(xì)就不一一解釋了,懶...
/**
* @author: xiaohaibin.
* @time: 2018/1/5
* @mail:xhb_199409@163.com
* @github:https://github.com/xiaohaibin
* @describe: 自定義仿支付寶螞蟻森林水滴View
*/
public class WaterView extends View {
private Paint paint;
private ObjectAnimator mAnimator;
/**
* 文字顏色
*/
private int textColor = Color.parseColor("#69c78e");
/**
* 水滴填充顏色
*/
private int waterColor = Color.parseColor("#c3f593");
/**
* 球描邊顏色
*/
private int storkeColor = Color.parseColor("#69c78e");
/**
* 描邊線條寬度
*/
private float strokeWidth = 0.5f;
/**
* 文字字體大小
*/
private float textSize = 36;
/**
* 根據(jù)遠(yuǎn)近距離的不同計算得到的應(yīng)該占的半徑比例
*/
private float proportion;
/**
* 水滴球半徑
*/
private int mRadius = 30;
/**
* 圓球文字內(nèi)容
*/
private String textContent="3g";
public WaterView(Context context) {
super(context);
init();
}
public WaterView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public WaterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setAntiAlias(true);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
drawCircleView(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(Utils.dp2px(getContext(), (int) (2 * (mRadius+strokeWidth))),Utils.dp2px(getContext(), (int) (2 * (mRadius+strokeWidth))));
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
start();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
stop();
}
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == VISIBLE) {
start();
} else {
stop();
}
}
private void drawCircleView(Canvas canvas){
//圓球
paint.setColor(waterColor);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(Utils.dp2px(getContext(), mRadius), Utils.dp2px(getContext(), mRadius), Utils.dp2px(getContext(), mRadius), paint);
//描邊
paint.setColor(storkeColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(Utils.dp2px(getContext(), (int) strokeWidth));
canvas.drawCircle(Utils.dp2px(getContext(), mRadius), Utils.dp2px(getContext(), mRadius), Utils.dp2px(getContext(), (int) (mRadius+strokeWidth)) , paint);
//圓球文字
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setStyle(Paint.Style.FILL);
drawVerticalText(canvas, Utils.dp2px(getContext(), mRadius), Utils.dp2px(getContext(), mRadius), textContent);
}
private void drawVerticalText(Canvas canvas, float centerX, float centerY, String text) {
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
float baseLine = -(fontMetrics.ascent + fontMetrics.descent) / 2;
float textWidth = paint.measureText(text);
float startX = centerX - textWidth / 2;
float endY = centerY + baseLine;
canvas.drawText(text, startX, endY, paint);
}
public void start() {
if (mAnimator == null) {
mAnimator = ObjectAnimator.ofFloat(this, "translationY", -6.0f, 6.0f, -6.0f);
mAnimator.setDuration(3500);
mAnimator.setInterpolator(new LinearInterpolator());
mAnimator.setRepeatMode(ValueAnimator.RESTART);
mAnimator.setRepeatCount(ValueAnimator.INFINITE);
mAnimator.start();
} else if (!mAnimator.isStarted()) {
mAnimator.start();
}
}
public void stop() {
if (mAnimator != null) {
mAnimator.cancel();
mAnimator = null;
}
}
public float getProportion() {
return proportion;
}
public void setProportion(float proportion) {
this.proportion = proportion;
}
}
動態(tài)隨機(jī)添加小球
這里我采用的是集成FrameLayout 通過設(shè)置小球數(shù)據(jù)西壮,動態(tài)將小球add進(jìn)去遗增,比較簡便,在這里最重要是動態(tài)隨機(jī)添加小球的算法款青,解決了這個算法就好辦了做修。通過仔細(xì)觀察支付寶螞蟻森林的效果實(shí)現(xiàn),我們可以發(fā)現(xiàn)一般小球都是在樹的正上方隨機(jī)分布的。所以我想以小樹的根為中心缓待,小樹的高度為半徑為一個扇形,在這個扇形上方隨機(jī)擺放小球渠牲。
公式:坐標(biāo) = 旋轉(zhuǎn)角度 * 半徑 * 根據(jù)遠(yuǎn)近距離的不同計算得到的應(yīng)該占的半徑比例
圓上任一點(diǎn)(x1,y1)坐標(biāo)的計算公式:
x1 = x0 + r * cos(ao * 3.14 /180 )
y1 = y0 + r * sin(ao * 3.14 /180 )
具體實(shí)現(xiàn)代碼如下:
/**
* @author: xiaohaibin.
* @time: 2018/1/5
* @mail:xhb_199409@163.com
* @github:https://github.com/xiaohaibin
* @describe: 支付寶螞蟻森林水滴能量
*/
public class WaterFlake extends FrameLayout {
private OnWaterItemListener mOnWaterItemListener;
/**
* 小樹坐標(biāo)X
*/
private float treeCenterX = 0;
/**
* 小樹坐標(biāo)Y
*/
private float treeCenterY = 0;
/**
* 小樹高度
*/
private int radius = 80;
/**
* 開始角度
*/
private double mStartAngle = 0;
/**
* 是否正在收集能量
*/
private boolean isCollect = false;
public WaterFlake(@NonNull Context context) {
super(context);
}
public WaterFlake(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public WaterFlake(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
Rect rect = new Rect();
for (int i = 0; i < getChildCount(); i++) {
getChildAt(i).getHitRect(rect);
if (rect.contains(x, y)) {
if (mOnWaterItemListener != null) {
getChildAt(i).performClick();
mOnWaterItemListener.onItemClick(i);
startAnimator(getChildAt(i));
return true;
}
}
}
}
return super.onTouchEvent(event);
}
@Override
public boolean performClick() {
return super.performClick();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
if (childCount==0){
return;
}
int left, top;
// 根據(jù)tem的個數(shù)旋炒,計算角度
float angleDelay = -180 / childCount;
for (int i = 0; i < childCount; i++) {
WaterView child = (WaterView) getChildAt(i);
mStartAngle %= 180;
//設(shè)置CircleView小圓點(diǎn)的坐標(biāo)信息
//坐標(biāo) = 旋轉(zhuǎn)角度 * 半徑 * 根據(jù)遠(yuǎn)近距離的不同計算得到的應(yīng)該占的半徑比例
// 則圓上任一點(diǎn)為:(x1,y1)
// x1 = x0 + r * cos(ao * 3.14 /180 )
// y1 = y0 + r * sin(ao * 3.14 /180 )
if (child.getVisibility() != GONE) {
left = (int) (getTreeCenterX() + radius * Math.cos(mStartAngle * 3.14 / 180) * (child.getProportion() / radius * 2));
top = (int) (getTreeCenterY() + radius * Math.sin(mStartAngle * 3.14 / 180) * (child.getProportion() / radius * 2));
child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredWidth());
}
mStartAngle += angleDelay;
}
}
/**
* 設(shè)置小球數(shù)據(jù),根據(jù)數(shù)據(jù)集合創(chuàng)建小球數(shù)量
*
* @param modelList 數(shù)據(jù)集合
*/
public void setModelList(List<WaterModel> modelList, float treeCenterX, float treeCenterY) {
this.treeCenterX = treeCenterX;
this.treeCenterY = treeCenterY;
for (int i = 0; i < modelList.size(); i++) {
WaterView waterView = new WaterView(getContext(),(i+1)+"g");
waterView.setProportion(Utils.getRandom(radius, radius + 80));
addView(waterView);
}
}
/**
* 設(shè)置小球點(diǎn)擊事件
*
* @param onWaterItemListener
*/
public void setOnWaterItemListener(OnWaterItemListener onWaterItemListener) {
mOnWaterItemListener = onWaterItemListener;
}
public interface OnWaterItemListener {
void onItemClick(int pos);
}
private void startAnimator(final View view) {
if (isCollect) {
return;
}
isCollect = true;
ObjectAnimator translatAnimatorY = ObjectAnimator.ofFloat(view, "translationY", getTreeCenterY());
translatAnimatorY.start();
ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f);
alphaAnimator.start();
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(translatAnimatorY).with(alphaAnimator);
animatorSet.setDuration(3000);
animatorSet.start();
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
removeViewInLayout(view);
isCollect = false;
}
});
}
public float getTreeCenterX() {
return treeCenterX;
}
public float getTreeCenterY() {
return treeCenterY;
}
}
小球擺放隨機(jī)算法有多種實(shí)現(xiàn)方式签杈,這只是其中一種瘫镇,寫的不好的地方,還望各位指正答姥,歡迎大家一起交流學(xué)習(xí)铣除。
目前代碼還有一些地方需要完善的,逐步更新鹦付。尚粘。。
TODO
優(yōu)化小球隨機(jī)生成算法敲长,保證每個小球盡量不會重疊
最后附上源碼鏈接:https://github.com/xiaohaibin/CustomWaterView 喜歡的話郎嫁,麻煩給個Star支持一下,謝謝~~~