一、最近在写商城方面的业务,需求在商品加如购物车过程中,实现一个抛物线加入的动画。
先看我写的效果图:
}
}
3.单个小球的初始化,新建小球类LittleBall,构造:
public LittleBall(int endX, int endY, int w, int h, Paint paint, int color) {
this.mView_endX = endX;
this.mView_endY = endY;
this.mView_w = w;
this.mView_h = h;
this.mPaint = paint;
this.mColor = color;
init();
}
根据ParabolaView传递过来的的宽高和颜色进行计算ball的起始位置和控制点,核心计算:
public void init() {
this.mStart_X = Utils.randomIntPositive(mView_endX + mView_w, mView_endX);
this.mStart_Y = Utils.randomIntPositive(mView_endY + mView_h, mView_endY);
this.mEnd_X = Utils.randomIntPositive(mView_endX + mView_w, mView_endX - mView_w);
this.mEnd_Y = mView_endY + mView_h;
this.mRadius = Utils.randomIntPositive(12, 4);
mControl_X = (mStart_X + mEnd_X) / 2;
mControl_Y = mStart_Y - 300;
}
4.多个小球的爆炸BoomView:
这里就比较简单了,和之前的抛物线动画思路一样
public void drawBall(Canvas canvas) {
if (null &#61;&#61; ballList || ballList.size() <&#61; 0) {
return;
}
for (int i &#61; 0; i ballList.get(i).drawBall(canvas); if (mAnimValue !&#61; 0) { ballList.get(i).setXY(mAnimValue); this.setAlpha(1 - mAnimValue); } } } 5.ParabolaView和BoomView之间的通讯&#xff1a; 因为要根据商品图片传递小球颜色和个数&#xff0c;所以避免不了它们之间的通讯&#xff0c;解决策略是通过当前Activity为桥梁进行传递数据&#xff0c;Activity实现ParabolaView中的接口AnimEndInterface并重写: &#64;Override public void onDrawBall(List littleBalls) { boomView.startAnim(littleBalls); } 四、用法简介(栗子)&#xff1a; ParabolaView和BoomView放在同一个布局之中&#xff0c;设置OnTouchListener事件&#xff0c;将手指点击处作为终点坐标传入&#xff0c;开启动画效果。 &#64;Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() &#61;&#61; MotionEvent.ACTION_UP) { parabolaView.startAnim((int) event.getX(), (int) event.getY()); } return true; } 当前Activity必须实现ParabolaView中的AnimEndInterface接口(已改) Activity实现AnimEndInterface太过于局限&#xff0c;更改成聚合的设计模式&#xff0c;适用于Activity/ViewGroup/Dialog等.. &#64;Override public void onDrawBall(List littleBalls) { boomView.startAnim(littleBalls); 五、遗留问题 配置文件attrs等相关细节懒得去做了&#xff0c;有时间再搞.. 六、源码地址&#xff1a; 自定义View-抛物线爆炸