• Android开发笔记(一百八十七)利用估值器实现弹幕动画


    如今上网看电影电视越发流行了,追剧的时候经常看到视频上方数行评论向左边飘去,犹如子弹那样飞快掠过,这些评论文字因此得名“弹幕”。弹幕评论由正在观看的网友们即兴发表,故而连绵不绝从画面右端不断涌现,直到漂至画面左端才隐没消失。
    虽然弹幕效果可使用平移动画实现,但平移动画比较单调,只能控制位移,不能控制速率、文字大小、文字颜色等要素。若想同时操纵视图的多种属性要素,需要采用属性动画加以实现。
    然而视图的位移大小由间距属性margin控制,该属性又分为上下左右四个方向,更要命的是,这几个margin并非视图View类的属性,而是布局参数LayoutParams的属性,意味着无法通过margin***直接构造属性动画对象。为了动态调整margin这种非常规属性,就要引入估值器实时计算当前的属性值,再据此设置自定义控件的状态参数。
    以间距属性为例,它的动画步骤说明如下:
    1、定义一个间距估值器,它实现了接口TypeEvaluator的evaluate方法,并在该方法中返回指定时间点的间距数值;
    2、调用ValueAnimator类的ofObject方法,根据间距估值器、开始位置和结束位置构建属性动画对象;
    3、调用属性动画对象的addUpdateListener方法设置刷新监听器,在监听器内部获取当前的间距数值,并调整视图此时的布局参数;
    具体到编码实现上,需要自定义弹幕视图,其内部在垂直方向排列,每行放置一个相对布局。发表弹幕评论时,先随机挑选某行相对布局,在该布局右侧添加文本视图,再通过前述的间距动画向左渐次滑动。弹幕视图的定义代码示例如下:

    1. public class BarrageView extends LinearLayout {
    2.     private Context mContext; // 声明一个上下文对象
    3.     private int mRowCount = 5; // 弹幕行数
    4.     private int mTextSize = 15; // 文字大小
    5.     private List<RelativeLayout> mLayoutList = new ArrayList<>(); // 每行的相对布局列表
    6.     private int mWidth; // 视图宽度
    7.     private int mLastPos1 = -1, mLastPos2 = -1; // 最近两次的弹幕位置
    8.     public BarrageView(Context context, AttributeSet attrs) {
    9.         super(context, attrs);
    10.         intView(context); // 初始化视图
    11.     }
    12.     // 初始化视图
    13.     private void intView(Context context) {
    14.         mContext = context;
    15.         setOrientation(LinearLayout.VERTICAL); // 设置垂直方向
    16.         for (int i=0; i<mRowCount; i++) {
    17.             RelativeLayout layout = new RelativeLayout(mContext);
    18.             RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    19.                     LayoutParams.MATCH_PARENT, Utils.dip2px(mContext, 40));
    20.             layout.setLayoutParams(params);
    21.             mLayoutList.add(layout);
    22.             addView(layout); // 添加至当前视图
    23.         }
    24.     }
    25.     @Override
    26.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    27.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    28.         mWidth = getMeasuredWidth(); // 获取视图的实际宽度
    29.     }
    30.     // 获取本次弹幕的位置。不跟最近两次在同一行,避免挨得太近
    31.     private int getPos() {
    32.         int pos;
    33.         do {
    34.             pos = new Random().nextInt(mRowCount);
    35.         } while (pos==mLastPos1 || pos==mLastPos2);
    36.         mLastPos2 = mLastPos1;
    37.         mLastPos1 = pos;
    38.         return pos;
    39.     }
    40.     // 给弹幕视图添加评论
    41.     public void addComment(String comment) {
    42.         RelativeLayout layout = mLayoutList.get(getPos()); // 获取随机位置的相对布局
    43.         TextView tv_comment = getCommentView(comment); // 获取评论文字的文本视图
    44.         float textWidth = MeasureUtil.getTextWidth(comment, Utils.dip2px(mContext, mTextSize));
    45.         layout.addView(tv_comment); // 添加至当前视图
    46.         // 根据估值器和起止位置创建一个属性动画
    47.         ValueAnimator anim = ValueAnimator.ofObject(new MarginEvaluator(), (int) -textWidth, mWidth);
    48.         // 添加属性动画的刷新监听器
    49.         anim.addUpdateListener(animation -> {
    50.             int margin = (int) animation.getAnimatedValue(); // 获取动画的当前值
    51.             RelativeLayout.LayoutParams tv_params = (RelativeLayout.LayoutParams) tv_comment.getLayoutParams();
    52.             tv_params.rightMargin = margin;
    53.             if (margin > mWidth-textWidth) { // 左滑到顶了
    54.                 tv_params.leftMargin = (int) (mWidth-textWidth - margin);
    55.             }
    56.             tv_comment.setLayoutParams(tv_params); // 设置文本视图的布局参数
    57.         });
    58.         anim.setTarget(tv_comment); // 设置动画的播放目标
    59.         anim.setDuration(5000); // 设置动画的播放时长
    60.         anim.setInterpolator(new LinearInterpolator()); // 设置属性动画的插值器
    61.         anim.start(); // 属性动画开始播放
    62.     }
    63.     // 获取评论内容的文本视图
    64.     private TextView getCommentView(String content) {
    65.         TextView tv = new TextView(mContext);
    66.         tv.setText(content);
    67.         tv.setTextSize(mTextSize);
    68.         tv.setSingleLine(true);
    69.         RelativeLayout.LayoutParams tv_params = new RelativeLayout.LayoutParams(
    70.                 ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    71.         tv_params.addRule(RelativeLayout.CENTER_VERTICAL); // 垂直方向居中
    72.         tv_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // 与上级布局右对齐
    73.         tv.setLayoutParams(tv_params); // 设置文本视图的布局参数
    74.         return tv;
    75.     }
    76.     // 定义一个间距估值器,计算动画播放期间的间距大小
    77.     public static class MarginEvaluator implements TypeEvaluator<Integer> {
    78.         @Override
    79.         public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
    80.             return (int) (startValue*(1-fraction) + endValue*fraction);
    81.         }
    82.     }
    83. }

    然后在布局文件中添加BarrageView节点,且活动代码调用弹幕视图的addComment方法发表评论。详细的调用代码如下所示:

    1. BarrageView bv_comment = findViewById(R.id.bv_comment);
    2. findViewById(R.id.btn_comment).setOnClickListener(v -> {
    3.     String comment = mCommentArray[new Random().nextInt(20)];
    4.     bv_comment.addComment(comment); // 给弹幕视图添加评论
    5. });

    运行测试App,数次点击添加按钮后,观察到弹幕效果如下图所示;

      继续点击几次添加按钮,此时弹幕效果如下图所示,可见每条弹幕评论都在往左漂去。


    点此查看Android开发笔记的完整目录

  • 相关阅读:
    Electron实战之入门
    Python-Flask入门,静态文件、页面跳转、错误信息、动态网页模板
    Nginx之动静分离
    STM32-CAN
    PyCLIPS的安装
    【Linux:环境变量】
    vscode hide menu bar
    移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——13.map&&set
    YOLOv8独家改进:逐元素乘法(star operation)二次创新 | 微软新作StarNet:超强轻量级Backbone CVPR 2024
    YOLO V8训练自己的数据集并测试
  • 原文地址:https://blog.csdn.net/aqi00/article/details/126957405