TextView 上下广告翻滚 和 左右滚动
来源:互联网 发布:苹果mac系统更新 编辑:程序博客网 时间:2024/04/30 00:11
左右滚动相对简单一些...,,系统内置有一些值可以设置
<TextView android:id="@+id/textview" android:layout_width="100px"//此处为文本显示区域的宽度此值必须比你的文本宽度要小否则是没有效果的 android:layout_height="wrap_content" android:text="@string/str" android:textColor="@drawable/red" android:layout_x="61px" android:layout_y="69px" android:scrollX="2px" android:singleLine="true" //设置只有一行android:scrollHorizontally="true"//水平滚动为trueandroid:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" >
而上下翻滚网上模版很多....需要自定义一下了....在这里....我建议使用gethub上开元的项目...原因为什么?因为好用啊,不用引入什么,,,就一个自定义类加进去...想怎么改就怎么改...对字符串的字体修改也简单,因为在loopertextview类里面,你就可以进行修改了
一个LooperTextView类...这个是在github上看到别人写的,地址:https://github.com/LeeYawei/Android-TipView 感谢作者,在这把代码贴下
/** * Created by Administrator on 2017/2/24. */import android.content.Context;import android.graphics.Color;import android.graphics.drawable.Drawable;import android.text.Html;import android.text.TextUtils;import android.util.AttributeSet;import android.util.TypedValue;import android.view.Gravity;import android.view.animation.Animation;import android.view.animation.DecelerateInterpolator;import android.view.animation.TranslateAnimation;import android.widget.FrameLayout;import android.widget.TextView;import java.util.List;import java.util.Random;/** * Created by Adminis on 2016/8/14. */public class LooperTextView extends FrameLayout{ private List<String> tipList; private int curTipIndex = 0; private long lastTimeMillis ; private static final int ANIM_DELAYED_MILLIONS = 3 * 1000; /** 动画持续时长 */ private static final int ANIM_DURATION = 1* 1000; private static final String DEFAULT_TEXT_COLOR = "#2F4F4F"; private static final int DEFAULT_TEXT_SIZE = 16;// private Drawable head_boy,head_girl; private TextView tv_tip_out,tv_tip_in; private Animation anim_out, anim_in; public LooperTextView(Context context) { super(context); initTipFrame(); initAnimation(); } public LooperTextView(Context context, AttributeSet attrs) { super(context, attrs); initTipFrame(); initAnimation(); } public LooperTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initTipFrame(); initAnimation(); } private void initTipFrame() {// head_boy = loadDrawable(R.mipmap.ic_launcher);// head_girl = loadDrawable(R.mipmap.ic_launcher); tv_tip_out = newTextView(); tv_tip_in = newTextView(); addView(tv_tip_in); addView(tv_tip_out); } private TextView newTextView(){ TextView textView = new TextView(getContext()); LayoutParams lp = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL); textView.setLayoutParams(lp); textView.setCompoundDrawablePadding(10); textView.setGravity(Gravity.CENTER_VERTICAL); textView.setLines(2); textView.setEllipsize(TextUtils.TruncateAt.END); textView.setTextColor(Color.parseColor(DEFAULT_TEXT_COLOR)); textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_TEXT_SIZE); return textView; } /** * 将资源图片转换为Drawable对象 * @param ResId * @return */ private Drawable loadDrawable(int ResId) { Drawable drawable = getResources().getDrawable(ResId); drawable.setBounds(0, 0, drawable.getMinimumWidth() - 10, drawable.getMinimumHeight() - 10); return drawable; } private void initAnimation() { anim_out = newAnimation(0, -1); anim_in = newAnimation(1, 0); anim_in.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { updateTipAndPlayAnimationWithCheck(); } }); } private Animation newAnimation(float fromYValue, float toYValue) { Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0, Animation.RELATIVE_TO_SELF,fromYValue,Animation.RELATIVE_TO_SELF, toYValue); anim.setDuration(ANIM_DURATION); anim.setStartOffset(ANIM_DELAYED_MILLIONS); anim.setInterpolator(new DecelerateInterpolator()); return anim; } private void updateTipAndPlayAnimationWithCheck() { if (System.currentTimeMillis() - lastTimeMillis < 1000 ) { return ; } lastTimeMillis = System.currentTimeMillis(); updateTipAndPlayAnimation(); } private void updateTipAndPlayAnimation() { if (curTipIndex % 2 == 0) { updateTip(tv_tip_out); tv_tip_in.startAnimation(anim_out); tv_tip_out.startAnimation(anim_in); this.bringChildToFront(tv_tip_in); } else { updateTip(tv_tip_in); tv_tip_out.startAnimation(anim_out); tv_tip_in.startAnimation(anim_in); this.bringChildToFront(tv_tip_out); } } private void updateTip(TextView tipView) {// if (new Random().nextBoolean()) {//前面的图片.我不需要了就注释掉..图片跟着动看起来体验也一般// tipView.setCompoundDrawables(head_boy, null, null, null);// } else {// tipView.setCompoundDrawables(head_girl, null, null, null);// } String tip = getNextTip(); if(!TextUtils.isEmpty(tip)) {//修改字体..... //Html.fromHtml("<font color=\'#858585\'>欢迎</font><font color=\'#f02387\'><U>购物愉快</U></font>") String start = tip.substring(0 , tip.indexOf(":")+1);//注意注意这里...这里需要自己搞一下...不然报错别说我没提示,根据自己给的字符串搞 String substring = tip.substring(tip.indexOf(":")+1, tip.length()); tipView.setText(Html.fromHtml(start+"<font color=\'#ff0000\'> "+substring+"</font>")); } } /** * 获取下一条消息 * @return */ private String getNextTip() { if (isListEmpty(tipList)) return null; return tipList.get(curTipIndex++ % tipList.size()); } public static boolean isListEmpty(List list) { return list == null || list.isEmpty(); } public void setTipList(List<String> tipList) { this.tipList = tipList; curTipIndex = 0; updateTip(tv_tip_out); updateTipAndPlayAnimation(); }}
在xml里面也简单
<com.blzx.view.LooperTextView //改好你的包名 android:id="@+id/loopertv" android:layout_width="match_parent" android:layout_height="wrap_content"> </com.blzx.view.LooperTextView>
使用代码如下
mLooperTextView = (LooperTextView) view.findViewById(R.id.loopertv); mLooperTextView.setTipList(generateTips());
private List<String> generateTips() { List<String> tips = new ArrayList<>(); tips.add("恭喜188******24获得:老婆杨幂一枚); tips.add("恭喜148******86获得:一念成佛"); tips.add("恭喜152******45获得:得到七龙珠"); return tips; }
你问传一个字符串 为什么前后颜色不一样....请你翻看前面的文章...谢谢...可以随心所欲改变你的TextView内的任何东西...
0 0
- TextView 上下广告翻滚 和 左右滚动
- jquery图片上下翻滚、图片左右翻滚、图片淡隐淡现3种图片滚动特效
- javascript 左右 或者上下翻滚效果
- ViewPager 左右 上下滚动
- 基于jquery的循环左右滚动和上下滚动效果
- 左右滚动公告代码和上下滚动公告代码
- 单行上下滚动广告
- android textView 上下滚动
- Android:TextView的垂直滚动效果,和上下滚动效果
- Android:TextView的垂直滚动效果,和上下滚动效果
- Android:TextView的垂直滚动效果,和上下滚动效果
- textview左右滚动
- js控制图片左右滚动,自动翻滚,图片滚动展示
- Android 用ScrollView和HorizontalScrollView同时实现上下、左右滚动
- js左右上下滚动代码
- android实现左右上下滚动
- TextSwticher 与 TextView 实现上下滚动和跑马灯效果
- 实现上下滚动的TextView
- Oracle PL-SQL Developer集成TFS进行团队脚本文件版本管理
- IDEA快捷键之for循环
- MySQL高可用架构之MHA
- hdu2955 01背包 Robberies
- CodeForces 363D Renting Bikes (二分+贪心)
- TextView 上下广告翻滚 和 左右滚动
- 九度OJ-1447:最短路(Floyd)
- hdu2639 Bone Collector II(01背包第k优解)
- qt的环境搭建(一) Visual Studio
- 浅析java内存模型(JMM)
- HDU1171 Big Event in HDU
- IDEA 2016.3.4 创建Maven项目及部署发布
- 蓝桥杯 国王的烦恼(并查集)
- Fragment之间的数据互传