Android:简单的弹幕效果达到

Android:简单的弹幕效果达到

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

首先,效果图。分类似至360检测到的骚扰电话页面:

Android:简单的弹幕效果达到

布局非常easy,上面是一个RelativeLayout,以下一个Button.

功能:

(1)弹幕生成后自己主动从右側往左側滚动(TranslateAnimation)。弹幕消失后立马被移除。

(2)弹幕位置随机出现。而且不反复(防止文字重叠)。

(3)字体大小在一定范围内随机改变。字体颜色也能够设置。

(4)自己定义先减速,后加速的Interpolator,弹幕加速进入、减速停留、然后加速出去。

1.Activity代码:

/**
 * 简易弹幕效果实现
 * Created by admin on 15-6-4.
 */
public class MainActivity extends ActionBarActivity {
    private MyHandler handler;

    //弹幕内容
    private TanmuBean tanmuBean;
    //放置弹幕内容的父组件
    private RelativeLayout containerVG;

    //父组件的高度
    private int validHeightSpace;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        containerVG = (RelativeLayout) findViewById(R.id.tanmu_container);
        tanmuBean = new TanmuBean();
        tanmuBean.setItems(new String[]{"測试一下", "弹幕这东西真不好做啊", "总是出现各种问题~~", "也不知道都是为什么?麻烦!", "哪位大神能够帮帮我啊?", "I need your help.",
                "測试一下", "弹幕这东西真不好做啊", "总是出现各种问题~~", "也不知道都是为什么?麻烦!

", "哪位大神能够帮帮我啊?", "I need your help.", "測试一下", "弹幕这东西真不好做啊", "总是出现各种问题~~", "也不知道都是为什么?麻烦!

", "哪位大神能够帮帮我啊?", "I need your help."}); handler = new MyHandler(this); //開始弹幕 View startTanmuView = findViewById(R.id.startTanmu); startTanmuView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (containerVG.getChildCount() > 0) { return; } existMarginValues.clear(); new Thread(new CreateTanmuThread()).start(); } }); } //每2s自己主动加入一条弹幕 private class CreateTanmuThread implements Runnable { @Override public void run() { int N = tanmuBean.getItems().length; for (int i = 0; i < N; i++) { handler.obtainMessage(1, i, 0).sendToTarget(); SystemClock.sleep(2000); } } } //须要在主线城中加入组件 private static class MyHandler extends Handler { private WeakReference<MainActivity> ref; MyHandler(MainActivity ac) { ref = new WeakReference<>(ac); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { MainActivity ac = ref.get(); if (ac != null && ac.tanmuBean != null) { int index = msg.arg1; String content = ac.tanmuBean.getItems()[index]; float textSize = (float) (ac.tanmuBean.getMinTextSize() * (1 + Math.random() * ac.tanmuBean.getRange())); int textColor = ac.tanmuBean.getColor(); ac.showTanmu(content, textSize, textColor); } } } } private void showTanmu(String content, float textSize, int textColor) { final TextView textView = new TextView(this); textView.setTextSize(textSize); textView.setText(content);// textView.setSingleLine(); textView.setTextColor(textColor); int leftMargin = containerVG.getRight() - containerVG.getLeft() - containerVG.getPaddingLeft(); //计算本条弹幕的topMargin(随机值,可是与屏幕中已有的不反复) int verticalMargin = getRandomTopMargin(); textView.setTag(verticalMargin); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_TOP); params.topMargin = verticalMargin; textView.setLayoutParams(params); Animation anim = AnimationHelper.createTranslateAnim(this, leftMargin, -ScreenUtils.getScreenW(this)); anim.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { //移除该组件 containerVG.removeView(textView); //移除占位 int verticalMargin = (int) textView.getTag(); existMarginValues.remove(verticalMargin); } @Override public void onAnimationRepeat(Animation animation) { } }); textView.startAnimation(anim); containerVG.addView(textView); } //记录当前仍在显示状态的弹幕的位置(避免反复) private Set<Integer> existMarginValues = new HashSet<>(); private int linesCount; private int getRandomTopMargin() { //计算用于弹幕显示的空间高度 if (validHeightSpace == 0) { validHeightSpace = containerVG.getBottom() - containerVG.getTop() - containerVG.getPaddingTop() - containerVG.getPaddingBottom(); } //计算可用的行数 if (linesCount == 0) { linesCount = validHeightSpace / ScreenUtils.dp2px(this, tanmuBean.getMinTextSize() * (1 + tanmuBean.getRange())); if (linesCount == 0) { throw new RuntimeException("Not enough space to show text."); } } //检查重叠 while (true) { int randomIndex = (int) (Math.random() * linesCount); int marginValue = randomIndex * (validHeightSpace / linesCount); if (!existMarginValues.contains(marginValue)) { existMarginValues.add(marginValue); return marginValue; } } }}

2.平移动画生成工具:

public class AnimationHelper {
    /**
     * 创建平移动画
     */
    public static Animation createTranslateAnim(Context context, int fromX, int toX) {
        TranslateAnimation tlAnim = new TranslateAnimation(fromX, toX, 0, 0);
        //自己主动计算时间
        long duration = (long) (Math.abs(toX - fromX) * 1.0f / ScreenUtils.getScreenW(context) * 4000);
        tlAnim.setDuration(duration);
        tlAnim.setInterpolator(new DecelerateAccelerateInterpolator());
        tlAnim.setFillAfter(true);

        return tlAnim;
    }
}

ScreenUtils是用来获取屏幕宽高、dp与px之间互转的工具类。

3.自己定义的Interpolator。事实上仅仅有一行代码

public class DecelerateAccelerateInterpolator implements Interpolator {

    //input从0~1,返回值也从0~1.返回值的曲线表征速度加减趋势
    @Override
    public float getInterpolation(float input) {
        return (float) (Math.tan((input * 2 - 1) / 4 * Math.PI)) / 2.0f + 0.5f;
    }
}

4.TanmuBean是一个实体类

public class TanmuBean {
    private String[] items;
    private int color;
    private int minTextSize;
    private float range;

    public TanmuBean() {
        //init default value
        color = Color.parseColor("#eeeeee");
        minTextSize = 16;
        range = 0.5f;
    }

    public String[] getItems() {
        return items;
    }

    public void setItems(String[] items) {
        this.items = items;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    /**
     * min textSize, in dp.
     */
    public int getMinTextSize() {
        return minTextSize;
    }

    public void setMinTextSize(int minTextSize) {
        this.minTextSize = minTextSize;
    }

    public float getRange() {
        return range;
    }

    public void setRange(float range) {
        this.range = range;
    }
}

==========

源代码下载:http://download.csdn.net/detail/books1958/9005279 

版权声明:本文博主原创文章,博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116834.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 少儿编程网站有哪些(少儿编程哪家好)

    20个热门少儿编程网站【2019】转载之本文链接:https://blog.csdn.net/shebao3333/article/details/85317936少儿编程是新的文化潮流,它涵盖了儿童学习的方方面面:逻辑思维训练、系统化思考训练、问题解决能力训练、团队协作、创造性思维培养…你可以利用我们整理的这些得到广泛认可的少儿编程网站教孩子学会编程,例如code.org、tynker.c…

    2022年4月18日
    38
  • python把局部变量赋值给全局变量_局部变量不赋初值

    python把局部变量赋值给全局变量_局部变量不赋初值理解的都没问题。但我想聊聊出现这种情况的原因。在讲原因之前,需要先知道python中变量的搜索顺序,这个顺序是LGB(不考虑闭包情况)即local本地,global全局,builtin内建。比如:a=1deftest():a=3print(a)test()函数内声明了局部变量a,在打印中使用,在本地环境中命中,因此使用的是3。也许你会问这个知识点我早就知道了,这和本问题有什么关…

    2022年10月24日
    0
  • C++不确定行为

    C++不确定行为

    2022年1月22日
    36
  • bindservice startservice_函数调用流程

    bindservice startservice_函数调用流程bindService流程

    2022年9月17日
    0
  • java中输出数组的语句_java定义数组的三种类型

    java中输出数组的语句_java定义数组的三种类型2022_02_19学习输出数组元素的三种方法以及c语言和java中数组的不同点方法一:for循环输出数组元素方法二:foreach循环语句方法三:Arrays类中的toString方法c语言和java中数组的区别方法一:for循环输出数组元素publicstaticvoidmain(String[]args){int[]array={1,2,3,4,5,6,7,8,9};for(inti=0;i<ar

    2022年10月11日
    0
  • Django(67)drf搜索过滤和排序过滤

    Django(67)drf搜索过滤和排序过滤前言当我们需要对后台的数据进行过滤的时候,drf有两种,搜索过滤和排序过滤。搜索过滤:比如我们想返回sex=1的,那么我们就可以从所有数据中进行筛选排序过滤:比如我们想对价格进行升序排列,就可以

    2022年7月30日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号