添加购物车效果

简介: 做一个商品加入购物车的效果,之前一直想尝试去实现,无奈啊,没机会。致谢 nianxin 徐宜生001.gif怎么用?public void addAction(View view) {new DriftingTextView(this).

做一个商品加入购物车的效果,之前一直想尝试去实现,无奈啊,没机会。致谢 nianxin 徐宜生

001.gif

怎么用?

<pre>
public void addAction(View view) {
new DriftingTextView(this)
.startView(view)
.rootView(getWindow().getDecorView())
.endView(btCart)
.startBesselAnim();
}
</pre>

代码很少

<pre>
package github.alex.jumpview;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.TextView;

/**
* 漂移的文本控件
* 作者:alex
* 时间:2016/7/28 10:54
* 博客地址:http://www.jianshu.com/users/c3c4ea133871/subscriptions
*/
public class DriftingTextView extends TextView {
private GradientDrawable gradientDrawable;
private static final int VIEW_SIZE = 20;

/\*\*
 \* 控件的半径
 \*/
protected int radius;
/\*\*
 \* 起点
 \*/
protected Point startPoint;
/\*\*
 \* 终点
 \*/
protected Point endPoint;
/\*\*
 \* 漂移的时间
 \*/
private long driftingDuration;

public DriftingTextView(Context context) {
    super(context);
    initView();
}

public DriftingTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView();
}

private void initView() {
    driftingDuration = 500;
    gradientDrawable = new GradientDrawable();
    gradientDrawable.setShape(GradientDrawable.OVAL);
    gradientDrawable.setColor(Color.parseColor("#FF0000"));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        setBackground(gradientDrawable);
    } else {
        setBackgroundDrawable(gradientDrawable);
    }
    setGravity(Gravity.CENTER);
    setText("1");
    setTextColor(Color.WHITE);
    setTextSize(10);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int size = (int) dp2Px(VIEW_SIZE);
    setMeasuredDimension(size, size);
    radius = size / 2;
}

/\*\*
 \* 设置  起始点
 \*/
public DriftingTextView startPoint(Point startPoint) {
    startPoint.y -= 10;
    this.startPoint = startPoint;
    return this;
}

/\*\*
 \* 设置 起始点的 控件
 \*/
public DriftingTextView startView(View view) {
    int position[] = new int[2];
    view.getLocationInWindow(position);
    startPoint(new Point(position[0], position[1]));
    return this;
}

/\*\*
 \* 设置 结束点
 \*/
public DriftingTextView endPoint(Point endPoint) {
    this.endPoint = endPoint;
    return this;
}

/\*\*
 \* 设置 结束点的 控件
 \*/
public DriftingTextView endView(View view) {
    int position[] = new int[2];
    view.getLocationInWindow(position);
    endPoint(new Point(position[0], position[1]));
    return this;
}

/\*\*
 \* 设置漂移文本控件的 父控件
 \*/
public DriftingTextView rootView(ViewGroup viewGroup) {
    viewGroup.addView(this);
    return this;
}
/\*\*
 \* 设置漂移文本控件的 父控件
 \*/
public DriftingTextView rootView(View view) {
    if(view instanceof ViewGroup){
        ViewGroup viewGroup = (ViewGroup) view;
        viewGroup.addView(this);
    }
    return this;
}

/\*\*
 \* 设置 漂移时间
 \*
 \* @param driftingDuration 漂移时间,单位 ms
 \*                         如果小于 20ms,或者大于 5000ms,使用默认时间500ms
 \*/
public DriftingTextView driftingDuration(long driftingDuration) {
    if ((driftingDuration < 20) || (driftingDuration > 5000)) {
        driftingDuration = 500;
    }
    this.driftingDuration = driftingDuration;
    return this;
}

public void startBesselAnim() {
    if (startPoint == null || endPoint == null) return;
    int pointX = (startPoint.x + endPoint.x) / 2;
    int pointY = (int) (startPoint.y - dp2Px(100));
    Point driftingPoint = new Point(pointX, pointY);
    BesselEvaluator bezierEvaluator = new BesselEvaluator(driftingPoint);
    ValueAnimator anim = ValueAnimator.ofObject(bezierEvaluator, startPoint, endPoint);
    anim.addUpdateListener(new MyAnimatorUpdateListener());
    anim.setDuration(driftingDuration);
    anim.addListener(new MyAnimatorListenerAdapter());
    anim.setInterpolator(new AccelerateDecelerateInterpolator());
    anim.start();
}

private final class MyAnimatorListenerAdapter extends AnimatorListenerAdapter {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        ViewGroup viewGroup = (ViewGroup) getParent();
        viewGroup.removeView(DriftingTextView.this);
    }
}

private final class MyAnimatorUpdateListener implements ValueAnimator.AnimatorUpdateListener {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        Point point = (Point) valueAnimator.getAnimatedValue();
        setX(point.x);
        setY(point.y);
        invalidate();
    }
}

public class BesselEvaluator implements TypeEvaluator<Point> {

    private Point driftingPoint;

    public BesselEvaluator(Point driftingPoint) {
        this.driftingPoint = driftingPoint;
    }

    @Override
    public Point evaluate(float t, Point startValue, Point endValue) {
        int x = (int) ((1 - t) \* (1 - t) \* startValue.x + 2 \* t \* (1 - t) \* driftingPoint.x + t \* t \* endValue.x);
        int y = (int) ((1 - t) \* (1 - t) \* startValue.y + 2 \* t \* (1 - t) \* driftingPoint.y + t \* t \* endValue.y);
        return new Point(x, y);
    }
}

/\*\*
 \* 数据转换: dp---->px
 \*/
private float dp2Px(float dp) {
    return dp \* getContext().getResources().getDisplayMetrics().density;
}

/\*\*
 \* sp转px
 \*/
private float sp2px(float sp) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getContext().getResources().getDisplayMetrics());
}

}

public class BezierUtil {

/\*\*
 \* B(t) = (1 - t)^2 \* P0 + 2t \* (1 - t) \* P1 + t^2 \* P2, t ∈ [0,1]
 \*
 \* @param t  曲线长度比例
 \* @param p0 起始点
 \* @param p1 控制点
 \* @param p2 终止点
 \* @return t对应的点
 \*/
public static PointF CalculateBezierPointForQuadratic(float t, PointF p0, PointF p1, PointF p2) {
    PointF point = new PointF();
    float temp = 1 - t;
    point.x = temp \* temp \* p0.x + 2 \* t \* temp \* p1.x + t \* t \* p2.x;
    point.y = temp \* temp \* p0.y + 2 \* t \* temp \* p1.y + t \* t \* p2.y;
    return point;
}

/\*\*
 \* B(t) = P0 \* (1-t)^3 + 3 \* P1 \* t \* (1-t)^2 + 3 \* P2 \* t^2 \* (1-t) + P3 \* t^3, t ∈ [0,1]
 \*
 \* @param t  曲线长度比例
 \* @param p0 起始点
 \* @param p1 控制点1
 \* @param p2 控制点2
 \* @param p3 终止点
 \* @return t对应的点
 \*/
public static PointF CalculateBezierPointForCubic(float t, PointF p0, PointF p1, PointF p2, PointF p3) {
    PointF point = new PointF();
    float temp = 1 - t;
    point.x = p0.x \* temp \* temp \* temp + 3 \* p1.x \* t \* temp \* temp + 3 \* p2.x \* t \* t \* temp + p3.x \* t \* t \* t;
    point.y = p0.y \* temp \* temp \* temp + 3 \* p1.y \* t \* temp \* temp + 3 \* p2.y \* t \* t \* temp + p3.y \* t \* t \* t;
    return point;
}

}
</pre>

目录
相关文章
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17038 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
6天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1262 3
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2347 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3821 22
|
19天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。