使用ViewFlipper实现公告播放

来源:互联网 发布:godaddy域名push教程 编辑:程序博客网 时间:2024/06/05 16:07

效果图如下:
这里写图片描述
我设定的是3秒钟滚动一次。
废话不多说了, 上代码了:

公告滚动的动画效果

in_bottomtop.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="500"        android:fromYDelta="100.0%p"        android:toYDelta="0.0" />    <alpha        android:duration="500"        android:fromAlpha="0.0"        android:toAlpha="1.0" /></set>

out_bottomtop.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:duration="500"        android:fromYDelta="0.0"        android:toYDelta="-100.0%p" />    <alpha        android:duration="500"        android:fromAlpha="1.0"        android:toAlpha="0.0" /></set>

下面是在布局中的代码
layout_main.xml

  <com.zhy.autolayout.AutoLinearLayout            android:id="@+id/homepage_notice_All"            android:layout_width="match_parent"            android:layout_height="35dp"            android:layout_below="@+id/ad_view"            android:layout_marginTop="@dimen/spaceing_5px"            android:orientation="horizontal"            android:background="@drawable/affiche_shadow" >            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_gravity="center_vertical"                android:paddingBottom="@dimen/spaceing_20px"                android:paddingLeft="@dimen/spaceing_20px"                android:paddingRight="@dimen/spaceing_20px"                android:paddingTop="@dimen/spaceing_20px"                android:src="@mipmap/notice" />            <ViewFlipper                android:id="@+id/homepage_notice_vf"                android:layout_width="match_parent"                android:layout_height="match_parent" />        </com.zhy.autolayout.AutoLinearLayout>

在Java 中的代码如下

  private ViewFlipper notice_vf;  private TimerTask task;    private void initRollNotice() {        notice_vf = (ViewFlipper) findViewById(R.id.homepage_notice_vf);        task = new TimerTask() {            @Override            public void run() {                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        moveNext();                        LogUtils.i(TAG, "下一个");                    }                });            }        };    }

initRollNotice 方法在onCreate里面调用

private int mCurrPos;    private void moveNext() {        setView(this.mCurrPos, this.mCurrPos + 1);        this.notice_vf.setInAnimation(this, R.anim.in_bottomtop);        this.notice_vf.setOutAnimation(this, R.anim.out_bottomtop);        this.notice_vf.showNext();    }    private void setView(int curr, int next) {        View noticeView = getLayoutInflater().inflate(R.layout.notice_item,                null);        TextView notice_tv = (TextView) noticeView.findViewById(R.id.notice_tv);        if ((curr < next) && (next > (luyaninfos.size() - 1))) {            next = 0;        } else if ((curr > next) && (next < 0)) {            next = luyaninfos.size() - 1;        }        notice_tv.setText(luyaninfos.get(next).getTitle());        notice_tv.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View arg0) {                Bundle bundle = new Bundle();                bundle.putString("url", luyaninfos.get(mCurrPos).getDescription());                bundle.putString("title", luyaninfos.get(mCurrPos).getTitle());                Intent intent = new Intent(MainActivity.this,                        LYInfoActivity.class);                intent.putExtras(bundle);                startActivity(intent);            }        });        if (notice_vf.getChildCount() > 1) {            notice_vf.removeViewAt(0);        }        notice_vf.addView(noticeView, notice_vf.getChildCount());        mCurrPos = next;    }

luyaninfos 这是一个数据集合,你可以自行修改。

这里用到了一个布局文件 R.layout.notice_item.xml 这个布局文件里面就只有一个TextView,代码如下:

<?xml version="1.0" encoding="utf-8"?><com.zhy.autolayout.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:gravity="center" >    <TextView        android:id="@+id/notice_tv"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:gravity="center_vertical" /></com.zhy.autolayout.AutoLinearLayout>

因为我是在获取到网络数据的时候才开始显示公告,所以开始执行定时器的代码写在了, 请求数据成功的方法里面了:

 Timer timer = new Timer(); timer.schedule(task, 0, 3000);
1 0
原创粉丝点击