<set xmlns:android=“http://schemas.android.com/apk/res/android”
android:duration=“3000” android:fillAfter=“true”>
<translate xmlns:android=“http://schemas.android.com/apk/res/android”
android:fromXDelta=“0%p” android:toXDelta=“50%p” android:fromYDelta=“0%p” android:toYDelta=“50%p”>
2、代码调用
package com.wust.myanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_hello_world;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
tv_hello_world = findViewById(R.id.tv_hello_world);
//利用 AnimationUtils 这个工具类获取 Animation
Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
//因为这一步我们在 xml 中做了申明,所以这里不需要写,但是要记住,Duration 不设置你将会看不到动画
// translate.setDuration(3000);
//开始动画
tv_hello_world.startAnimation(translate);
}
}
代码方式
使用步骤:
- 编写java代码
package com.wust.myanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_hello_world;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
tv_hello_world = findViewById(R.id.tv_hello_world);
//利用 AnimationUtils 这个工具类获取 Animation Animation.RELATIVE_TO_PARENT:表示后面的值相对于父控件
Animation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.5f,
Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.5f);
//设置运动完了之后是否回来
translate.setFillAfter(true);
//这一步不能忘记了
translate.setDuration(3000);
//开始动画
tv_hello_world.startAnimation(translate);
}
}
缩放
==
效果展示
<?xml version="1.0" encoding="utf-8"?>xml 方式
<set xmlns:android=“http://schemas.android.com/apk/res/android”
android:fillAfter=“true” android:duration=“3000”>
<scale
android:fromXScale=“0%”
android:toXScale=“100%”
android:fromYScale=“0%”
android:toYScale=“100%”/>
package com.wust.myanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_hello_world;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
tv_hello_world = findViewById(R.id.tv_hello_world);
//利用 AnimationUtils 这个工具类获取 Animation Animation.RELATIVE_TO_PARENT:表示后面的值相对于父控件
Animation scale = AnimationUtils.loadAnimation(this,R.anim.scale);
//开始动画
tv_hello_world.startAnimation(scale);
}
}
java代码方式
package com.wust.myanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_hello_world;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
tv_hello_world = findViewById(R.id.tv_hello_world);
//利用 AnimationUtils 这个工具类获取 Animation Animation.RELATIVE_TO_SELF:表示后面的值相对于自己 这里的值大小都是 [0,1]
Animation scale = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//设置运动完了之后是否回来
scale.setFillAfter(true);
//这一步不能忘记了
scale.setDuration(3000);
//开始动画
tv_hello_world.startAnimation(scale);
}
}
旋转 和 透明度大家根据上面两种方式的规律自行尝试,我们抓紧篇幅赶紧来讲讲如何将这四种动画集合在一起展示。
综合动画
====
效果展示
<?xml version="1.0" encoding="utf-8"?>xml方式
<translate
android:fromXDelta=“0%p”
android:toXDelta=“50%p”
android:fromYDelta=“0%p”
android:toYDelta=“50%p”/>
<scale
android:pivotX=“0%”
android:pivotY=“0%”
android:fromXScale=“0%”
android:toXScale=“100%”
android:fromYScale=“0%”
android:toYScale=“100%”/>
<alpha
android:fromAlpha=“0”
android:toAlpha=“1”/>
<rotate
android:pivotY=“0%”
android:pivotX=“0%”
android:fromDegrees=“0”
android:toDegrees=“18”/>
package com.wust.myanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_hello_world;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
tv_hello_world = findViewById(R.id.tv_hello_world);
//利用 AnimationUtils 这个工具类获取 Animation Animation.RELATIVE_TO_SELF:表示后面的值相对于自己 这里的值大小都是 [0,1]
Animation scale = AnimationUtils.loadAnimation(this,R.anim.multianim);
//开始动画
tv_hello_world.startAnimation(scale);
}
}
java代码方式
package com.wust.myanimation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_hello_world;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
tv_hello_world = findViewById(R.id.tv_hello_world);
//利用 AnimationUtils 这个工具类获取 Animation Animation.RELATIVE_TO_SELF:表示后面的值相对于自己 这里的值大小都是 [0,1]
Animation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,
Animation.RELATIVE_TO_PARENT,0.5f,
Animation.RELATIVE_TO_PARENT,0,
Animation.RELATIVE_TO_PARENT,0.5f);
Animation scale = new ScaleAnimation(0,1,0,1,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
Animation alpha = new AlphaAnimation(0, 1);
Animation rotate = new RotateAnimation(0, 18,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//创建 AnimationSet 装 Animation
AnimationSet animationSet = new AnimationSet(false);
animationSet.addAnimation(translate);