实现背景动态化android,ColorAnimationView 实现了滑动Viewpager 时背景色动态变化的过渡效果详解...

用法在注释中:

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");

}

});

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="cn.taurusxi.guidebackgroundcoloranimation.sample.SampleActivity">

android:id="@+id/ColorAnimationView"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

android:id="@+id/viewPager"

android:padding="30dp"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

* @author Administrator

*

*/

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 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);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值