国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android自定義View實現(xiàn)廣告信息上下滾動效果

2019-12-12 06:18:57
字體:
供稿:網(wǎng)友

先看看效果:

實現(xiàn)代碼:

public class ScrollBanner extends LinearLayout {  private TextView mBannerTV1;  private TextView mBannerTV2;  private Handler handler;  private boolean isShow;  private int startY1, endY1, startY2, endY2;  private Runnable runnable;  private List<String> list;  private int position = 0;  private int offsetY = 100;  public ScrollBanner(Context context) {    this(context, null);  }  public ScrollBanner(Context context, AttributeSet attrs) {    this(context, attrs, 0);  }  public ScrollBanner(Context context, AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    View view = LayoutInflater.from(context).inflate(R.layout.view_scroll_banner, this);    mBannerTV1 = (TextView) view.findViewById(R.id.tv_banner1);    mBannerTV2 = (TextView) view.findViewById(R.id.tv_banner2);    handler = new Handler();    runnable = new Runnable() {      @Override      public void run() {        isShow = !isShow;        if (position == list.size())          position = 0;        if (isShow) {          mBannerTV1.setText(list.get(position++));        } else {          mBannerTV2.setText(list.get(position++));        }        startY1 = isShow ? 0 : offsetY;        endY1 = isShow ? -offsetY : 0;        ObjectAnimator.ofFloat(mBannerTV1, "translationY", startY1, endY1).setDuration(300).start();        startY2 = isShow ? offsetY : 0;        endY2 = isShow ? 0 : -offsetY;        ObjectAnimator.ofFloat(mBannerTV2, "translationY", startY2, endY2).setDuration(300).start();        handler.postDelayed(runnable, 3000);      }    };  }  public List<String> getList() {    return list;  }  public void setList(List<String> list) {    this.list = list;  }  public void startScroll() {    handler.post(runnable);  }  public void stopScroll() {    handler.removeCallbacks(runnable);  }}

view_scroll_banner.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="horizontal"  android:padding="10dp">  <TextView    android:id="@+id/tv_title"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerVertical="true"    android:text="公告"    android:textColor="#e76712"    android:textSize="16sp" />  <View    android:id="@+id/view_divider"    android:layout_width="1dp"    android:layout_height="match_parent"    android:layout_marginLeft="10dp"    android:layout_marginRight="10dp"    android:layout_toRightOf="@id/tv_title"    android:background="#DDDDDD" />  <TextView    android:id="@+id/tv_banner1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_centerVertical="true"    android:layout_toRightOf="@+id/view_divider"    android:ellipsize="end"    android:singleLine="true"    android:text="熱烈歡迎領(lǐng)導(dǎo)光臨" />  <TextView    android:id="@+id/tv_banner2"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_centerVertical="true"    android:layout_toRightOf="@+id/view_divider"    android:ellipsize="end"    android:singleLine="true"    android:text="喜慶國慶歡樂大酬賓" /></RelativeLayout>

使用方法:

scrollBanner.setList(list);scrollBanner.startScroll();

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)Android軟件編程有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 越西县| 磐安县| 万全县| 佳木斯市| 神农架林区| 云梦县| 灵石县| 修水县| 夹江县| 八宿县| 阆中市| 嘉荫县| 松桃| 磐石市| 崇文区| 儋州市| 门头沟区| 大余县| 洛川县| 白城市| 绥宁县| 延庆县| 高清| 淳化县| 汉阴县| 镇远县| 潞西市| 静宁县| 砚山县| 渝北区| 灯塔市| 平阳县| 高邑县| 江油市| 原平市| 团风县| 九寨沟县| 哈尔滨市| 乌拉特前旗| 昭平县| 永安市|