安卓仿ios饿了么商品详情淡入淡出效果

 我来答
匿名用户
2017-06-29
展开全部
public class MainActivity extends Activity implements View.OnClickListener{
private TextView[] tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.objectanimatortest);
tv = new TextView[]{
(TextView) findViewById(R.id.tv01),
(TextView) findViewById(R.id.tv02),
(TextView) findViewById(R.id.tv03),
(TextView) findViewById(R.id.tv04),

(TextView) findViewById(R.id.tv11),
(TextView) findViewById(R.id.tv12),
(TextView) findViewById(R.id.tv13),
(TextView) findViewById(R.id.tv14)
};
initTextViews();
}
private void initTextViews(){
for(TextView textView :tv){//颜色随机变化代码 使用ValueAnimator类给每个视图加上北京颜色随时间变化的动画
int color1 = Color.rgb((new Random()).nextInt(255), (new Random()).nextInt(255), (new Random()).nextInt(255)) ;
int color2 = Color.rgb((new Random()).nextInt(255), (new Random()).nextInt(255), (new Random()).nextInt(255)) ;
ValueAnimator animator = ObjectAnimator.ofInt(textView, "backgroundColor", color1,color2);
animator.setDuration(3000);
animator.setEvaluator(new ArgbEvaluator());//设置数值计算器,保证动画变化过程中的数值正确
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();

textView.setOnClickListener(this);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(final View view) {
final ValueAnimator animator1 = ObjectAnimator.ofFloat(view, "alpha",1,0);//淡出效果
animator1.setDuration(1000);
animator1.setInterpolator(new AccelerateInterpolator());
ValueAnimator animator2 = ObjectAnimator.ofFloat(view,"x",view.getX(),(view.getX()-view.getWidth()));//向左移动效果
animator2.setDuration(1000);
animator2.setInterpolator(new DecelerateInterpolator());

AnimatorSet animatorSet = new AnimatorSet();//合起来就是左淡出效果
animatorSet.play(animator2).before(animator1);
// animatorSet.start();

final ValueAnimator animator3 = ObjectAnimator.ofFloat(view, "alpha",0,1);//淡入效果
animator3.setDuration(1000);
animator3.setInterpolator(new AccelerateInterpolator());
ValueAnimator animator4 = ObjectAnimator.ofFloat(view,"x",view.getX()+2*view.getWidth(),view.getX());//从右边向左移动
animator4.setDuration(1000);
animator4.setInterpolator(new DecelerateInterpolator());

animator4.addListener(new AnimatorListenerAdapter() {//当动画播放完,我们做什么

@Override
public void onAnimationEnd(Animator animation) {//向下移动淡出,然后向上移动淡入
super.onAnimationEnd(animation);
final ValueAnimator animatorY = ObjectAnimator.ofFloat(view, "y", view.getY(),view.getY()+view.getHeight());
animatorY.setDuration(1000);
final ValueAnimator alphaY = animator1.clone();

ValueAnimator rotate = ObjectAnimator.ofFloat(view,"rotationY",0,90);
rotate.setDuration(2000);
// rotate.start();

animatorY.addListener(new AnimatorListenerAdapter() {

@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
animatorY.reverse();
animator3.clone().start();
}

});
AnimatorSet set = new AnimatorSet();
set.play(animatorY).with(alphaY);
set.start();
}

});
// animatorSet.play(animator3).after(animator1);//合起来就是左淡出,右淡入效果
// animatorSet.play(animator3).with(animator4);
// animatorSet.start();

AnimatorSet animatorSet1 = new AnimatorSet();
animatorSet1.play(animator3).with(animator4);

AnimatorSet animatorSet2 = new AnimatorSet();
animatorSet2.play(animator2).with(animator1);

AnimatorSet set = new AnimatorSet();
set.playSequentially(animatorSet2,animatorSet1);//使用playSequentially方法测试效果
set.start();
}
}这是我敲的一个小例子
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式