时间:2021-05-20
之前的需求是用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 , 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); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了Android自定义跑马灯文字的具体代码,供大家参考,具体内容如下Android跑马灯效果文字:效果图(真实动画很流畅,这个转gif有问题,
本文实例为大家分享了Android自定义textview实现跑马灯效果的具体代码,供大家参考,具体内容如下xml布局自定义MarqueeLayoutpublic
Android自带的跑马灯效果不太好控制,还必须要满足条件才能有效果,而且速度不受控制。前面我的博客中有一篇就是用Android自带的跑马灯效果的,但是基于不同
一直想实现一个竖直跑马灯的效果,今天接触到了ViewFlipper这个控件,是做安卓视图切换的,对其用自定义控件进行包装;实现其点击回调和自定义视图等功能不多说
Android自带的跑马灯效果不太好控制,不能控制速度,不能即时停止和启动,而且还受焦点的影响蛋疼不已。由于项目需求需要用的可控制性高的跑马灯效果,所以自己写了