Android基础-简单的动画实现 *补间动画 *透明度渐变 *旋转动画 *位移动画 *如果需要几个效果整合到一起,需要将所有效果全部放到set里

 *补间动画 

 *透明度渐变 

 *旋转动画

  *位移动画

  *如果需要几个效果整合到一起,需要将所有效果全部放到set里


MainActivity

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image=(ImageView)findViewById(R.id.image);
ImageView image2=(ImageView)findViewById(R.id.image2);
ImageView image3=(ImageView)findViewById(R.id.image3);
ImageView image4=(ImageView)findViewById(R.id.image4);
ImageView image5=(ImageView)findViewById(R.id.image5);
//加载动画
//透明度渐变
Animation animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
image.startAnimation(animation);
//旋转动画
Animation animation2=AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
image2.startAnimation(animation2);
Animation animation3=AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
image3.startAnimation(animation3);
Animation animation4=AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
image4.startAnimation(animation4);
Animation animation5=AnimationUtils.loadAnimation(MainActivity.this, R.anim.set);
image5.startAnimation(animation5);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<!--  动画——旋转
android:repeatCount="infinite"重复旋转
android:fromDegrees="0"开始角度
    android:toDegrees="360" 结束角度
    android:pivotX="0"
    android:pivotY="0"旋转中心
    android:repeatMode="reverse"重复模式改为相反
 -->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360" 
    android:duration="2000"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    >
</rotate>


scale.xml

<?xml version="1.0" encoding="utf-8"?>
<!--动画——缩放 
android:fromXScale="1"
    android:fromYScale="1"开始大小
     android:toXScale="2"
    android:toYScale="2"结束大小
    android:repeatCount="infinite"重复执行
     android:pivotX="50%"
    android:pivotY="50%"缩放中心
 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000" 
    android:fromXScale="1"
    android:fromYScale="1"
    android:toXScale="2"
    android:toYScale="2"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    >
</scale>



set.xml

<?xml version="1.0" encoding="utf-8"?>
<!--若要实现多个效果共同体现在同一个组件上,将各种效果文件全部写到set里 
 -->
<set>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360" 
    android:duration="2000"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    />
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000" 
    android:fromXScale="1"
    android:fromYScale="1"
    android:toXScale="2"
    android:toYScale="2"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    />
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="-200"
    android:fromYDelta="0" 
    android:toXDelta="200"
    android:toYDelta="0"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:duration="2000"
    />
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromAlpha="1"
    android:toAlpha="0" 
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    />
</set>



translate.xml

<?xml version="1.0" encoding="utf-8"?>
<!--动画——位移
-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="-200"
    android:fromYDelta="0" 
    android:toXDelta="200"
    android:toYDelta="0"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:duration="2000"
    >
</translate>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,作为一个语言模型 AI,我无法提交演示程序的运行效果图。但我可以告诉您如何实现这四种补间动画。 在Android程序中,可以通过使用Animation类和它的子类,实现四种补间动画: 1. 透明度渐变动画 使用AlphaAnimation类实现透明度渐变动画。可以通过设置动画的起始透明度和结束透明度,以及动画的持续时间来控制动画效果。 ``` AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); alphaAnimation.setDuration(1000); view.startAnimation(alphaAnimation); ``` 2. 旋转动画 使用RotateAnimation类实现旋转动画。可以通过设置动画的起始角度和结束角度,以及动画的持续时间来控制动画效果。 ``` RotateAnimation rotateAnimation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(1000); view.startAnimation(rotateAnimation); ``` 3. 缩放动画 使用ScaleAnimation类实现缩放动画。可以通过设置动画的起始大小和结束大小,以及动画的持续时间来控制动画效果。 ``` ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(1000); view.startAnimation(scaleAnimation); ``` 4. 平移动画 使用TranslateAnimation类实现平移动画。可以通过设置动画的起始位置和结束位置,以及动画的持续时间来控制动画效果。 ``` TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f); translateAnimation.setDuration(1000); view.startAnimation(translateAnimation); ``` 以上是四种补间动画的基本实现方式,可以根据需要进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值