今天就跟大家聊聊有關(guān)怎么在Android中自定義一個圖文跑馬燈效果,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
十年建站經(jīng)驗, 成都做網(wǎng)站、成都網(wǎng)站制作客戶的見證與正確選擇。成都創(chuàng)新互聯(lián)公司提供完善的營銷型網(wǎng)頁建站明細(xì)報價表。后期開發(fā)更加便捷高效,我們致力于追求更美、更快、更規(guī)范。
(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(); // 在這里進(jìn)行解析 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" /> <ImageView android:id="@+id/name_iv1" /> <TextView android:id="@+id/name_tv2" /> <ImageView android:id="@+id/name_iv2" /> <TextView android:id="@+id/name_tv3" /> <ImageView android:id="@+id/name_iv3" /> <TextView android:id="@+id/name_tv4" /> <ImageView android:id="@+id/name_iv4" /> <TextView android:id="@+id/name_tv5" /> <ImageView android:id="@+id/name_iv5" /> <TextView android:id="@+id/name_tv6" /> <ImageView android:id="@+id/name_iv6" /> </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); } }
看完上述內(nèi)容,你們對怎么在Android中自定義一個圖文跑馬燈效果有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前文章:怎么在Android中自定義一個圖文跑馬燈效果
轉(zhuǎn)載來源:http://jinyejixie.com/article12/gceedc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、微信公眾號、域名注冊、企業(yè)網(wǎng)站制作、用戶體驗、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)