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

布局非常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

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

Android:简单的弹幕效果达到的更多相关文章

  1. JavaScript实现简单的弹幕效果实例分析

    不知大家有没有感受到,弹幕又是另一出好戏!! 不过我个人还是比较排斥看电视的时候被出来的弹幕打扰.今天我们来写一个简单的弹幕.简单到什么程度呢?看下效果: 由图可以看出,我们的呆毛html结构确实是非 ...

  2. Android Studio 直播弹幕

    我只是搬运:https://blog.csdn.net/HighForehead/article/details/55520199 写的很好很详细,挺有参考价值的 demo直通车:https://do ...

  3. android 弹幕效果demo

    记得之前有位朋友在我的公众号里问过我,像直播的那种弹幕功能该如何实现?如今直播行业确实是非常火爆啊,大大小小的公司都要涉足一下直播的领域,用斗鱼的话来讲,现在就是千播之战.而弹幕则无疑是直播功能当中最 ...

  4. Android弹幕功能实现,模仿斗鱼直播的弹幕效果

    转载出处:http://blog.csdn.net/sinyu890807/article/details/51933728 本文同步发表于我的微信公众号,扫一扫文章底部的二维码或在微信搜索 郭霖 即 ...

  5. Android 自定义View修炼-自定义弹幕效果View

    一.概述 现在有个很流行的效果就是弹幕效果,满屏幕的文字从右到左飘来飘去.看的眼花缭乱,看起来还蛮cool的 现在就是来实现这一的一个效果,大部分的都是从右向左移动漂移,本文的效果中也支持从左向右的漂 ...

  6. Android 弹幕效果开发案例

    概述 现在有个很流行的效果就是弹幕效果,满屏幕的文字从右到左飘来飘去.看的眼花缭乱,看起来还蛮cool的 现在就是来实现这一的一个效果,大部分的都是从右向左移动漂移,本文的效果中也支持从左向右的漂移移 ...

  7. android 的闪屏效果

    android的闪屏效果,就是我们刚开始启动应用的时候弹出的界面或者动画,过2秒之后自动的跳转到主界面. 其实,实现这个效果很简单,使用Handler对象的postDelayed方法就可以实现.在这个 ...

  8. 【转】(转)【Android】Paint的效果研究

    转自:http://wpf814533631.iteye.com/blog/1847661 (转)[Android]Paint的效果研究 博客分类: android   在Paint中有很多的属性可以 ...

  9. Android实现左右滑动效果

    本示例演示在Android中实现图片左右滑动效果.   关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现.接下来 ...

随机推荐

  1. 看来IT技术与军事技术都是相通的——都是对新事物极为敏感的领域

    这是读到这段时候的感想: 和海军中那些狂热的相信“皇军不可战胜”的大舰巨炮主义者们不同,山口对于与美国开战的主张是持坚定的反对态度的,和山本五十六都做过日本驻美武官的山口都认为一旦与美开战,日本或许能 ...

  2. php编码

    原文:php编码 PHP 页面编码声明与用header或meta实现PHP页面编码的区别     php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header ...

  3. 14.4.3.2 Configuring Multiple Buffer Pool Instances 配置多个buffer pool 实例:

    14.4.3.2 Configuring Multiple Buffer Pool Instances 配置多个buffer pool 实例: 对于系统 buffer pool 有多个G的范围, 把b ...

  4. HBase总结(二十)HBase经常使用shell命令具体说明

    进入hbase shell console $HBASE_HOME/bin/hbase shell 假设有kerberos认证,须要事先使用对应的keytab进行一下认证(使用kinit命令),认证成 ...

  5. set与map容器

    首先来看看set集合容器: set集合容器实现了红黑树的平衡二叉树数据结构,在插入元素时它会自动调整二叉树的排列,把该元素放到适当的位置,并且 保证左右子树平衡.平衡二叉检索树采用中序遍历算法. 对于 ...

  6. POJ 1724 ROADS(bfs最短路)

    n个点m条边的有向图,每条边有距离跟花费两个参数,求1->n花费在K以内的最短路. 直接优先队列bfs暴力搞就行了,100*10000个状态而已.节点扩充的时候,dp[i][j]表示到达第i点花 ...

  7. CCEditBox/CCEditBoxImplIOS

    #ifndef __CCEditBoxIMPLIOS_H__ #define __CCEditBoxIMPLIOS_H__ #include "cocos2d.h" #if (CC ...

  8. 黑马程序猿————OC在Foundation框架结构和字符串

    ------<a href="http://www.itheima.com" target="blank">Java火车.Android火车.iOS ...

  9. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...

  10. SVN与TortoiseSVN实战:补丁详解(转)

    硬广:<SVN与TortoiseSVN实战>系列已经写了五篇,第二篇<SVN与TortoiseSVN实战:标签与分支>和第三篇<SVN与TortoiseSVN实战:Tor ...