热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android动画案例,淡入淡出效果

转载请注明出处:http:blog.csdn.netdroyonarticledetails8689249源代码下载1、android动画测试程序,

转载请注明出处:http://blog.csdn.net/droyon/article/details/8689249

源代码下载

1、android动画测试程序,界面如图:

颜色随机变化,点击视图,左淡出,右淡入,下淡出,上淡入效果。


主要源代码解析:

package com.example.objectanimatortest;import java.util.Random;import android.os.Build;
import android.os.Bundle;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.TextureView;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity implements View.OnClickListener{private TextView[] tv;@Overrideprotected 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);}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {//getMenuInflater().inflate(R.menu.activity_main, menu);return true;}@Overridepublic 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() {//当动画播放完,我们做什么@Overridepublic 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() {@Overridepublic 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();}}
在源代码中还有测试类,修改一下mainifest.xml文件,让另外的那个类运行便可以看到效果。







推荐阅读
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • importjava.util.ArrayList;publicclassPageIndex{privateintpageSize;每页要显示的行privateintpageNum ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • Python教学练习二Python1-12练习二一、判断季节用户输入月份,判断这个月是哪个季节?3,4,5月----春 ... [详细]
  • 在本教程中,我们将看到如何使用FLASK制作第一个用于机器学习模型的RESTAPI。我们将从创建机器学习模型开始。然后,我们将看到使用Flask创建AP ... [详细]
  • fileuploadJS@sectionscripts{<scriptsrc~Contentjsfileuploadvendorjquery.ui.widget.js ... [详细]
  • java布尔字段用is前缀_POJO类中布尔类型的变量都不要加is前缀详解
    前言对应阿里巴巴开发手册第一章的命名风格的第八条。【强制】POJO类中布尔类型的变量都不要加is前缀,否则部分框架解析会引起序列化错误。反例:定义为基本 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 使用Flutternewintegration_test进行示例集成测试?回答首先在dev下的p ... [详细]
author-avatar
finaokas_261
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有