ColorAnimationView 实现了滑动Viewpager 时背景色动态变化的过渡效果
来源:互联网 发布:js显示时间日期代码 编辑:程序博客网 时间:2024/04/26 17:01
用法在注释中:
import android.animation.Animator;import android.animation.ArgbEvaluator;import android.animation.ObjectAnimator;import android.animation.ValueAnimator;import android.content.Context;import android.support.v4.view.ViewPager;import android.util.AttributeSet;import android.view.View;/* * ColorAnimationView 实现了滑动 Viewpager 的时候背景色动态变化的过渡效果 * * ColorAnimationView colorAnimationView = (ColorAnimationView) findViewById(R.id.ColorAnimationView);ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);viewPager.setAdapter(adpter);colorAnimationView.setmViewPager(viewPager, resource.length); colorAnimationView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { Log.e("TAG","onPageScrolled"); } @Override public void onPageSelected(int position) { Log.e("TAG","onPageSelected"); } @Override public void onPageScrollStateChanged(int state) { Log.e("TAG","onPageScrollStateChanged"); } }); <FrameLayout 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" tools:context="cn.taurusxi.guidebackgroundcoloranimation.sample.SampleActivity"> <com.*.ColorAnimationView android:id="@+id/ColorAnimationView" android:layout_width="match_parent" android:layout_height="match_parent"/> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:padding="30dp" android:layout_width="match_parent" android:layout_height="match_parent"/></FrameLayout> * @author Administrator * */public class ColorAnimationView extends Viewimplements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {private static final int RED = 0xffFF8080;private static final int BLUE = 0xff8080FF;private static final int WHITE = 0xffffffff;private static final int GREEN = 0xff80ff80;private static final int DURATION = 3000;ValueAnimator colorAnim = null;private PageChangeListener mPageChangeListener;ViewPager.OnPageChangeListener onPageChangeListener;public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {this.onPageChangeListener = onPageChangeListener;}/** * 这是你唯一需要关心的方法 * @param mViewPager 你必须在设置 Viewpager 的 Adapter 这后,才能调用这个方法。 * @param count ,viewpager孩子的数量 * @param colors int... colors ,你需要设置的颜色变化值~~ 如何你传人 空,那么触发默认设置的颜色动画 * *//** * This is the only method you need care about. * @param mViewPager ,you need set the adpater before you call this. * @param count ,this param set the count of the viewpaper's child * @param colors ,this param set the change color use (int... colors), * so,you could set any length if you want.And by default. * if you set nothing , don't worry i have already creat * a default good change color! * */public void setmViewPager(ViewPager mViewPager, int count, int... colors) {//this.mViewPager = mViewPager;if (mViewPager.getAdapter() == null) {throw new IllegalStateException("ViewPager does not have adapter instance.");}mPageChangeListener.setViewPagerChildCount(count);mViewPager.setOnPageChangeListener(mPageChangeListener);if (colors.length == 0) {createDefaultAnimation();} else {createAnimation(colors);}}public ColorAnimationView(Context context) {this(context, null, 0);}public ColorAnimationView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ColorAnimationView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mPageChangeListener = new PageChangeListener();}private void seek(long seekTime) {if (colorAnim == null) {createDefaultAnimation();}colorAnim.setCurrentPlayTime(seekTime);}private void createAnimation(int... colors) {if (colorAnim == null) {colorAnim = ObjectAnimator.ofInt(this,"backgroundColor", colors);colorAnim.setEvaluator(new ArgbEvaluator());colorAnim.setDuration(DURATION);colorAnim.addUpdateListener(this);}}private void createDefaultAnimation() {colorAnim = ObjectAnimator.ofInt(this,"backgroundColor", WHITE, RED, BLUE, GREEN, WHITE);colorAnim.setEvaluator(new ArgbEvaluator());colorAnim.setDuration(DURATION);colorAnim.addUpdateListener(this);}@Override public void onAnimationStart(Animator animation) {}@Override public void onAnimationEnd(Animator animation) {}@Override public void onAnimationCancel(Animator animation) {}@Override public void onAnimationRepeat(Animator animation) {}@Override public void onAnimationUpdate(ValueAnimator animation) {invalidate();//long playtime = colorAnim.getCurrentPlayTime();}private class PageChangeListenerimplements ViewPager.OnPageChangeListener {private int viewPagerChildCount;public void setViewPagerChildCount(int viewPagerChildCount) {this.viewPagerChildCount = viewPagerChildCount;}public int getViewPagerChildCount() {return viewPagerChildCount;}@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {int count = getViewPagerChildCount() - 1;if (count != 0) {float length = (position + positionOffset) / count;int progress = (int) (length * DURATION);ColorAnimationView.this.seek(progress);}// call the method by default if (onPageChangeListener!=null){ onPageChangeListener.onPageScrolled(position,positionOffset,positionOffsetPixels); }}@Override public void onPageSelected(int position) { if (onPageChangeListener!=null) { onPageChangeListener.onPageSelected(position); }}@Override public void onPageScrollStateChanged(int state) { if (onPageChangeListener!=null) { onPageChangeListener.onPageScrollStateChanged(state); }}}}
0 0
- ColorAnimationView 实现了滑动Viewpager 时背景色动态变化的过渡效果
- Android 特效库 - 实现滑动ViewPager渐变背景色
- Android_Button背景色的变化
- 润乾报表中实现背景色的隔行变化
- Listview的item选中时的背景色的变化
- ViewPager和Fragment结合,利用(HorizontalScrollView)实现指示器与ViewPager同时滑动的动态效果
- css样式实现 背景色的渐变效果及兼容性
- ViewPager引导页根据滑动渐变背景色
- 文本框里背景色的变化
- ViewPager左右滑动页面,下面的按钮跟着动态变化
- 自定义ViewGroup实现ViewPager的滑动效果
- ViewPager+Fragment 实现滑动页面的效果
- 实现viewpager的无限滑动效果
- 实现ViewPager的不同滑动效果
- 使用js实现鼠标移入移出时背景色的切换效果
- vue过渡效果的几种实现方法、各种滑动
- Dialog背景色透明效果轻松实现
- Dialog背景色透明效果轻松实现
- No.7_6 OpenCL 同步——异步拷贝
- 迷你轻量级全方向完美滑动处理侧滑控件SlideLayout
- ZooKeeper分布式 安装部署
- 在界面顶部滑动展开的自定义FrameLayout
- 数据库
- ColorAnimationView 实现了滑动Viewpager 时背景色动态变化的过渡效果
- PorterDuffColorFilter 在项目中的基本使用
- Android端与笔记本利用局域网进行FTP通信
- ios开发第一天-包装类
- ViewPager 与SwipeRefreshLayout,RecyclerView,ScrollView滑动冲突解决方法
- GPUImageTransformFilter实现3D旋转
- android引用资源@与属性?备忘单
- Android 新推出基于物理的动画库SpringAnimation,完全诠释什么叫做弹簧效果
- Github入门(一)注册Github账号