ViewPage滑动背景渐变效果的Splash主页面
2015-10-15 11:39
459 查看
package com.duguang.baseanimation.ui.splash.BackGroundColor; import com.duguang.baseanimation.R; import android.annotation.SuppressLint; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; /** * ViewPage滑动背景渐变效果的Splash主页面 * 继承BaseActivity获取ActionBar * @author duguang 博客地址:http://blog.csdn.net/duguang77 * */ public class BackGroundColorAnimationActivity extends FragmentActivity { private static final int[] resource = new int[] { R.drawable.backgroundcolor_welcome1, R.drawable.backgroundcolor_welcome4, R.drawable.backgroundcolor_welcome3, R.drawable.backgroundcolor_welcome4 }; private static final String TAG = BackGroundColorAnimationActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash_backgroundcolor_main); MyFragmentStatePager adpter = new MyFragmentStatePager( getSupportFragmentManager()); ColorAnimationView colorAnimationView = (ColorAnimationView) findViewById(R.id.ColorAnimationView); ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); viewPager.setAdapter(adpter); /** * 首先,你必须在 设置 Viewpager的 adapter 之后在调用这个方法 第二点,setmViewPager(ViewPager * mViewPager,Object obj, int count, int... colors) 第一个参数 是 你需要传人的 * viewpager 第二个参数 是 * 一个实现了ColorAnimationView.OnPageChangeListener接口的Object,用来实现回调 第三个参数 是 * viewpager 的 孩子数量 第四个参数 int... colors ,你需要设置的颜色变化值~~ 如何你传人 * 空,那么触发默认设置的颜色动画 * */ /** * Frist: You need call this method after you set the Viewpager adpter; * Second: setmViewPager(ViewPager mViewPager,Object obj, int count, * int... colors) so,you can set any length colors to make the animation * more cool! Third: If you call this method like below, make the colors * no data, it will create a change color by default. * */ 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"); } }); // Four : Also ,you can call this method like this: // colorAnimationView.setmViewPager(viewPager,this,resource.length,0xffFF8080,0xff8080FF,0xffffffff,0xff80ff80); } public class MyFragmentStatePager extends FragmentStatePagerAdapter { public MyFragmentStatePager(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return new MyFragment(position); } @Override public int getCount() { return resource.length; } } @SuppressLint("ValidFragment") public class MyFragment extends Fragment { private int position; public MyFragment(int position) { this.position = position; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ImageView imageView = new ImageView(getActivity()); imageView.setImageResource(resource[position]); return imageView; } } } 2:使用到的布局为 <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.duguang.baseanimation.ui.splash.BackGroundColor.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:layout_width="match_parent" android:layout_height="match_parent" android:padding="30dp" /> </FrameLayout>
3:自定义的背景view为
package com.duguang.baseanimation.ui.splash.BackGroundColor; 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; /** * Created by TaurusXi on 2014/5/20. */ public class ColorAnimationView extends View implements 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 obj * ,这个obj实现了 ColorAnimationView.OnPageChangeListener ,实现回调 * @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 PageChangeListener implements 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); } } } }
相关文章推荐
- AWR报告生成
- Objective-C排序算法实现
- nginx+uwsgi+django部署
- 解决WebView调用loadData()方法显示乱码的问题
- 解决WebView调用loadData()方法显示乱码的问题
- 控制台下 出现65001错误
- epoptes ubuntu 电子教学系统 电子教室 远程控制
- 百练 2755 神奇的口袋 dp背包
- 点击按钮不刷新页面
- Locality Sensitive Hashing(LSH)之随机投影法
- xhost display 详解
- 谷歌应用市场3
- Spring之——quartz集群的问题及解决方案(基于Spring4.0+quartz2.2.1的集群架构)
- mysql 实现多列唯一性约束
- string.format大全
- Android 双进程Service常驻后台,无惧“一键清理”
- 对象的序列化
- 1003. 我要通过!(20)
- Xamarin之WebView 使用CSS JS文件
- Git常用命令归档