demo下载地址: http://yun.baidu.com/s/1i3wqEMh
由于前几年csdn泄露password的事件,导致我的账号被拿去发广告文章了,联系了管理员帮我把几篇广告文删掉,可是那几篇高质量的文章就这样没了。如今补回当中一篇。是关于怎样实现像赶集生活client第一次启动时的介绍动画的,demo在上面,能够下载来试试效果。有须要的能够在评论里提供邮箱。博主会把project发过去 : )
//FeatureAnimationListener.java package com.example.animatetest; import android.view.View; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; public class FeatureAnimationListener implements AnimationListener { private View mAnimView; private boolean mAnimIn; public FeatureAnimationListener(View animView, boolean animIn) { mAnimView = animView; mAnimIn = animIn; } @Override public void onAnimationEnd(Animation animation) { if(!mAnimIn) { mAnimView.setVisibility(View.INVISIBLE); } } @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationStart(Animation animation) { if(mAnimIn) { mAnimView.setVisibility(View.VISIBLE); } } }
//OnScrollChangedListener.java package com.example.animatetest; public abstract interface OnScrollChangedListener { public abstract void onScrollChanged(int top, int oldTop); }
//ObservableScrollView.java package com.example.animatetest; import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; public class ObservableScrollView extends ScrollView { private OnScrollChangedListener onScrollChangedListener; public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ObservableScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ObservableScrollView(Context context) { super(context); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if(this.onScrollChangedListener != null) { onScrollChangedListener.onScrollChanged(t, oldt); } } public void setOnScrollChangedListener(OnScrollChangedListener onScrollChangedListener) { this.onScrollChangedListener = onScrollChangedListener; } }
//MainActivity.java package com.example.animatetest; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; public class MainActivity extends Activity implements OnGlobalLayoutListener, OnScrollChangedListener { private ObservableScrollView mScrollView; private View mAnimView; private int mScrollViewHeight; private int mStartAnimateTop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mScrollView = (ObservableScrollView)this.findViewById(R.id.scrollView1); mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(this); mScrollView.setOnScrollChangedListener(this); mAnimView = this.findViewById(R.id.anim1); mAnimView.setVisibility(View.INVISIBLE); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onGlobalLayout() { mScrollViewHeight = mScrollView.getHeight(); mStartAnimateTop = mScrollViewHeight / 3 * 2; } boolean hasStart = false; @Override public void onScrollChanged(int top, int oldTop) { int animTop = mAnimView.getTop() - top; if(top > oldTop) { if(animTop < mStartAnimateTop && !hasStart) { Animation anim1 = AnimationUtils.loadAnimation(this, R.anim.feature_anim2scale_in); anim1.setAnimationListener(new FeatureAnimationListener(mAnimView, true)); mAnimView.startAnimation(anim1); hasStart = true; } } else { if(animTop > mStartAnimateTop && hasStart) { Animation anim1 = AnimationUtils.loadAnimation(this, R.anim.feature_alpha_out); anim1.setAnimationListener(new FeatureAnimationListener(mAnimView, false)); mAnimView.startAnimation(anim1); hasStart = false; } } } }
这里主要是继承ScrollView,重载里面的onScrollChanged方法。监听ScrollView的滑动状态,从而控制动画的显示和消失。
时间: 2024-11-07 18:41:24