android实现滚动文本效果
程序员文章站
2022-09-06 12:29:45
本文实例为大家分享了android实现滚动文本效果的具体代码,供大家参考,具体内容如下效果图实现方法直接上代码首先是一个自定义layout,继承自framelayoutpublic class ani...
本文实例为大家分享了android实现滚动文本效果的具体代码,供大家参考,具体内容如下
效果图
实现方法
直接上代码
首先是一个自定义layout,继承自framelayout
public class animationtextlayout extends framelayout { private static final string tag = "animationtextlayout"; private list<string> tiplist; private list<integer> displaylist; private list<textview> viewlist; private list<virtualpos> virtualposlist; private double deviantangle = 0; public animationtextlayout(@nonnull context context) { super(context); initview(); } public animationtextlayout(@nonnull context context, @nullable attributeset attrs) { super(context, attrs); initview(); } public animationtextlayout(@nonnull context context, @nullable attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); initview(); } private void initview() { viewlist = new arraylist<>(); displaylist = new arraylist<>(); virtualposlist = new arraylist<>(); } /** * 设置需要显示的数据 * * @param data 需要显示的数据 */ public void setdata(list<string> data) { this.tiplist = data; inittips(); } private void inittips() { while (tiplist.size() > viewlist.size()) { addtipsview(); } refreshtips(); initvirpos(); post(new runnable() { @override public void run() { initposition(); requestlayout(); } }); } /** * 计算虚拟位置 */ private void initvirpos() { virtualposlist.clear(); for (int i = 0; i < viewlist.size(); i++) { double angle = (math.pi * ((double) i / viewlist.size())*2)+(deviantangle*math.pi*2); if (angle>math.pi*2){ angle-=math.pi*2; } virtualpos virtualpos = new virtualpos(); virtualpos.text = tiplist.get(i); virtualpos.z = 100 * math.sin(angle); virtualpos.y = 100 * math.cos(angle); virtualposlist.add(virtualpos); } } /** * 将虚拟位置转化为实际高度和位置 */ private void initposition() { for (int i = 0; i < viewlist.size(); i++) { textview textview = viewlist.get(i); virtualpos virtualpos=virtualposlist.get(i); int realy = (int) ((100 - virtualpos.y)/200 *getmeasuredheight()); framelayout.layoutparams layoutparams = (layoutparams) textview.getlayoutparams(); layoutparams.topmargin = realy; if (virtualpos.z>=0){ float textsize= (float) (( virtualpos.z)/100)*20+5; textview.settextsize(textsize); textview.setvisibility(visible); }else { textview.setvisibility(gone); } log.d(tag, "initposition: y=="+realy); } } private void refreshtips() { for (int i = 0; i < viewlist.size(); i++) { textview tip = viewlist.get(i); if (i < tiplist.size()) { tip.setvisibility(visible); tip.settext(tiplist.get(i)); continue; } tip.setvisibility(gone); } } private textview addtipsview() { textview textview = new textview(getcontext()); textview.settextsize(complex_unit_dip, 12); textview.settextcolor(color.parsecolor("#444444")); textview.setpadding(convertutil.dp2px(5), convertutil.dp2px(3), convertutil.dp2px(5), convertutil.dp2px(3)); layoutparams layoutparams = new layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); layoutparams.gravity=gravity.center_horizontal; layoutparams.rightmargin = convertutil.dp2px(6); addview(textview, layoutparams); viewlist.add(textview); return textview; } /** * 虚拟位置,最大x,y,z 最大值为100,最小值为-100 */ public static class virtualpos { public double x; public double y; public double z; public string text; } /** * 滚动的偏移值 * @param deviantangle 最大为1 */ public void setdeviantangle(float deviantangle) { this.deviantangle = deviantangle; initvirpos(); initposition(); } public double getdeviantangle() { return deviantangle; } }
调用方布局
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".animationtextactivity"> <com.lanlengran.test.view.animationtextlayout android:id="@+id/anim_text_layout" android:layout_width="match_parent" android:background="@color/coloraccent" android:layout_height="400dp"/> </framelayout>
调用方代码
public class animationtextactivity extends activity { private animationtextlayout manimtextlayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_animation_text); manimtextlayout = findviewbyid(r.id.anim_text_layout); list<string> testdata = new arraylist<>(); for (int i = 0; i < 30; i++) { testdata.add("测试数据" + i); } manimtextlayout.setdata(testdata); objectanimator animator = objectanimator.offloat(manimtextlayout, "deviantangle", 0f, 1f); animator.setduration(5000); animator.setrepeatcount(-1); animator.start(); } }
注意事项
此处的动画只是为了演示。可以根据需要改变自定view的改变滚动值的方法,就可以使滚轮滚动。例如将滚动的角度和手指拖动相结合啥的
/** * 滚动的偏移值 * @param deviantangle 最大为1 */ public void setdeviantangle(float deviantangle)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android 实现ViewPager边界回弹效果实例代码
-
Android中RecyclerView实现多级折叠列表效果(二)
-
Android继承ViewGroup实现Scroll滑动效果的方法示例
-
Android用过TextView实现跑马灯效果的示例
-
android实现上下滚动的TextView
-
Android Tween动画之RotateAnimation实现图片不停旋转效果实例介绍
-
android中图片翻页效果简单的实现方法
-
Android实现两个ScrollView互相联动的同步滚动效果代码
-
Android控件RecyclerView实现混排效果仿网易云音乐
-
vue.js2.0 实现better-scroll的滚动效果实例详解