前几天看了一篇文章,讲的是ImageSwitcher和TextSwitcher的用法,自己实现了下,看了看效果就没有再管了,感觉这样不好,一点都没有自己总结,毕业也一年了,感觉自己的技术没有一点进步, 反而后退了很多,哎!不能这样了,要好好努力啊,今天就把这个demo重新好好写了下,总结一下,有需要的朋友们可以看一下:
ImageSwitcher和TextSwitcher用起来很简单,先看下面的xml文件
<ImageSwitcher android:layout_gravity="center" android:id="@+id/imageSwitcher" android:layout_width="400dp" android:layout_height="400dp"></ImageSwitcher> 在java文件中imageSwitcher需要设置setFactory,实现ViewFactory接口,重写其中makeView方法,这个方法返回View对象, 就是屏幕展示的view,@Override public View makeView() { ImageView imageView = new ImageView(getApplicationContext()); imageView.setScaleType(ImageView.ScaleType.CENTER); //设置显示方式 imageView.setLayoutParams(new android.widget.ImageSwitcher.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));//定义组建 return imageView; }当然,ImageSwitcher切换的时候还是得设置切换动画的,ImageSwitcher提供有进入和退出设置动画的方法,imageSwitcher.setInAnimation(getAnimationTopIn()); imageSwitcher.setOutAnimation(getAnimationBottomOut());这里的动画效果就是自己写的了,根据自己的项目需要来写。下面是我自己写的由上到下的动画:
/** * 从顶部进入 * * @return */ public Animation getAnimationTopIn() { AnimationSet animup = new AnimationSet(true); TranslateAnimation mytranslateanimup0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 0f); mytranslateanimup0.setDuration(1000); animup.addAnimation(mytranslateanimup0); return animup; } /** * 从底部退出 * * @return */ public Animation getAnimationBottomOut() { AnimationSet animdn = new AnimationSet(true); TranslateAnimation mytranslateanimdn1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f); mytranslateanimdn1.setDuration(1000); animdn.addAnimation(mytranslateanimdn1); return animdn; }
ImageSwitcher的用法大致就是这么多,TextSwitcher的用法类似,这里就不详细说了,想看详细代码的可以点击下面链接下载:
demo里面包括ImageSwitcher和TextSwitcher的用法,简单易懂,你值得拥有!
(ps:童鞋们赏点积分呗,本着分享的态度,不应该要积分的,奈何楼主实在是没积分了,等有积分了一定免费分享)
资源下载