Android自定义图文跑马灯效果
程序员文章站
2022-07-06 16:51:25
之前的需求是用flipperview来实现上下翻动效果,但是发现数据有点长会造成一屏幕放不下三条数据,后来改为跑马灯,但是只有文字的跑马灯textview自己就有,但是要求...
之前的需求是用flipperview来实现上下翻动效果,但是发现数据有点长会造成一屏幕放不下三条数据,后来改为跑马灯,但是只有文字的跑马灯textview自己就有,但是要求文字后面带一个小图标怎们办呢?
(1).mainactivity.java:
public class homefragment extends basefragment { private marqueescroll mmarqueescroll; private int[] name_tv = {r.id.name_tv1, r.id.name_tv2, r.id.name_tv3, r.id.name_tv4, r.id.name_tv5, r.id.name_tv6}; private textview[] name_tvs = new textview[6]; private int[] name_iv = {r.id.name_iv1, r.id.name_iv2, r.id.name_iv3, r.id.name_iv4, r.id.name_iv5, r.id.name_iv6}; private imageview[] name_ivs = new imageview[6]; private arraylist<homeflipperbean> mflipperlist = new arraylist<homeflipperbean>(); @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_homenews, null); mmarqueescroll = (marqueescroll) view.findviewbyid(r.id.marqueescroll); for (int i = 0; i < name_tv.length; i++) { name_tvs[i] = (textview) view.findviewbyid(name_tv[i]); name_ivs[i] = (imageview) view.findviewbyid(name_iv[i]); } mmarqueescroll.run(); // 在这里进行解析 responsebasebean<arraylist<homeflipperbean>> responsbasebean = json.parseobject(mflipperdata, new typereference<responsebasebean<arraylist<homeflipperbean>>>() { }); mflipperlist = responsbasebean.getresult(); if (mflipperlist != null && mflipperlist.size() > 0) { // foregroundcolorspan 为文字前景色,backgroundcolorspan为文字背景色 foregroundcolorspan span1 = new foregroundcolorspan(0xff535353); foregroundcolorspan span2 = new foregroundcolorspan(0xff346699); for (int i = 0; i < name_tvs.length; i++) { homeflipperbean bean = mflipperlist.get(i); name_tvs[i].settext(bean.getname() + " " + bean.getprice()); if ("up".equals(bean.gettrend())) { name_ivs[i].setbackgroundresource(r.drawable.in_img3); } else { name_ivs[i].setbackgroundresource(r.drawable.in_img4); } int length = bean.getname().length(); spannablestringbuilder builder = new spannablestringbuilder(name_tvs[i].gettext().tostring()); builder.setspan(span1, 0, length, spannable.span_exclusive_exclusive); builder.setspan(span2, length + 1, name_tvs[i].gettext().tostring().length(), spannable.span_exclusive_exclusive); name_tvs[i].settext(builder); } } } }
(2).fragment_homenews.xml:布局文件引用控件
<com.monkey.mushroom.ui.view.marqueescroll android:id="@+id/marqueescroll" android:layout_width="match_parent" android:layout_height="@dimen/kx_nonet_h" > <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e1f4ff" android:orientation="horizontal" > <textview android:id="@+id/name_tv1" style="@style/filpper_text_style" /> <imageview android:id="@+id/name_iv1" style="@style/filpper_image_style" /> <textview android:id="@+id/name_tv2" style="@style/filpper_text_style" /> <imageview android:id="@+id/name_iv2" style="@style/filpper_image_style" /> <textview android:id="@+id/name_tv3" style="@style/filpper_text_style" /> <imageview android:id="@+id/name_iv3" style="@style/filpper_image_style" /> <textview android:id="@+id/name_tv4" style="@style/filpper_text_style" /> <imageview android:id="@+id/name_iv4" style="@style/filpper_image_style" /> <textview android:id="@+id/name_tv5" style="@style/filpper_text_style" /> <imageview android:id="@+id/name_iv5" style="@style/filpper_image_style" /> <textview android:id="@+id/name_tv6" style="@style/filpper_text_style" /> <imageview android:id="@+id/name_iv6" style="@style/filpper_image_style" /> </linearlayout> </com.jyd.jyddz.ui.view.marqueescroll>
(3).marqueescroll.java:自定义horizontalscrollview
public class marqueescroll extends horizontalscrollview implements runnable { private view inner; private bitmap bitmap = null; /** * 滚动步长 */ private int step = 1; private int x; private int width; private int pwidth; private int pheight; public marqueescroll(context context, attributeset attrs) { super(context, attrs); setbackgroundcolor(0xffe1f4ff); } @override protected void onfinishinflate() { if (getchildcount() == 1) { inner = getchildat(0); } } @override protected void ondetachedfromwindow() { super.ondetachedfromwindow(); handler.removecallbacks(this); } @override protected void ondraw(canvas canvas) { if (getwidth() == 0) { android.view.viewgroup.layoutparams lp = getlayoutparams(); lp.width = pwidth; lp.height = pheight; setlayoutparams(lp); } if (bitmap == null && inner != null) { width = inner.getmeasuredwidth(); bitmap = bitmap.createbitmap(width, inner.getheight(), config.rgb_565); canvas canvas1 = new canvas(bitmap); inner.draw(canvas1); pwidth = getwidth(); pheight = getheight(); if (inner != null) { removeviewinlayout(inner); inner = null; } run(); } if (bitmap != null) { int nowx = x; nowx -= step; canvas.drawbitmap(bitmap, nowx, 0, null); if (nowx < 0) { canvas.drawbitmap(bitmap, width + nowx /* + space */, 0, null); } if (nowx <= -width) { nowx = 0; } x = nowx; } super.ondraw(canvas); } private handler handler = new handler() { @override public void handlemessage(message msg) { super.handlemessage(msg); } }; @override public void run() { invalidate(); handler.postdelayed(this, 1); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。