用过手机淘宝的人,应该都有看到淘宝首页,中间的位置,有一个上下滚动的消息条幅。之前因为公司项目中有需要用到这种效果,所以就找了类似控件,发现了viewswitcher这个控件,主要是用于两个不同视图间的切换,附带相应的动画效果。比如:淘宝首页,淘宝头条那里的滚动滑出的特效等等
首先添加控件到xml文件中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/hsview" android:scrollbars="none" android:layout_marginTop="10dp">
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical">
<View android:id="@+id/view" android:layout_width="60dp" android:layout_height="30dp"
android:background="@drawable/tag_shape" android:visibility="visible"
android:tag="0"/>
<LinearLayout android:id="@+id/tag_lly" android:layout_width="match_parent"
android:layout_height="30dp" android:orientation="horizontal"
android:gravity="center_vertical"></LinearLayout>
</RelativeLayout>
</HorizontalScrollView>
<ViewSwitcher android:id="@+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"></ViewSwitcher>
</LinearLayout>
Java代码中:
//设置视图工厂
viewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
return getLayoutInflater().inflate(R.layout.itemview,null);
}
});
//设置切入动画
TranslateAnimation animationTop=new TranslateAnimation(0,0,-15,0);
animationTop.setFillAfter(true);
animationTop.setDuration(200);
viewSwitcher.setInAnimation(animationTop);
//设置切出动画
TranslateAnimation animationBottom=new TranslateAnimation(0,0,0,60);
animationBottom.setFillAfter(true);
animationBottom.setDuration(200);
viewSwitcher.setOutAnimation(animationBottom);
第一步:设置视图工厂
第二步:设置切入和切出动画,我的动画是上下滚动的,所以看到的X都是0
第三步: 由于我的项目中是需要一直不停的进行切换的,所以这时候就需要调用Android中另一个工具类ScheduledExecutorService,此类是实现了和定时器相同的功能,但是比定时器要好,具体哪里好,由于时间关系,还没研究,有兴趣的可以参考:Java 并发专题 : Timer的缺陷 用ScheduledExecutorService替代好继续上我们的代码:
@Override
protected void onStart() {
super.onStart();
/*schedule(task,initDelay):安排所提交的Callable或Runnable任务在initDelay指定的时间后执行。
scheduleAtFixedRate():安排所提交的Runnable任务按指定的间隔重复执行
scheduleWithFixedDelay():安排所提交的Runnable任务在每次执行完后,等待delay所指定的时间后重复执行。*/
scheduledExecutorService= Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
handler.obtainMessage().sendToTarget();
}
},1,5, TimeUnit.SECONDS);
}
第四步:也就是最后一步,当然是调用handler,更新界面啦:
private android.os.Handler handler=new android.os.Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
viewSwitcher.getNextView().findViewById(R.id.textview).setVisibility(View.VISIBLE);
String tag=tags[indexSel%tags.length];
((TextView)viewSwitcher.getNextView().findViewById(R.id.textview)).setText(tag);
viewSwitcher.showNext();
indexSel++;
}
};
暂时上班不方便先写这么多,更详细的,请移步:
Android 中文API (61) —— ViewSwitcher