Android(安卓)4种补间动画基础使用。
16lz
2021-12-04
package com.example.thinkpad.animation;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class Main2Activity extends AppCompatActivity implements View.OnClickListener { ImageView imageView; TranslateAnimation translateAnimation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Toast.makeText(this,"hello",Toast.LENGTH_SHORT).show(); imageView=(ImageView) findViewById(R.id.image); Button button0 =(Button) findViewById(R.id.bt_translation); Button button1=(Button) findViewById(R.id.bt_rotate); Button button2 =(Button) findViewById(R.id.bt_scale); Button button3 =(Button) findViewById(R.id.bt_alpha); button1.setOnClickListener(this); button0.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); } public void onClick(View v){ switch (v.getId()){ case R.id.bt_translation://平移 translateAnimation =new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT, 1.0f); translateAnimation.setDuration(3000); translateAnimation.setRepeatCount(Animation.INFINITE); translateAnimation.setFillAfter(true); translateAnimation.setRepeatMode(Animation.REVERSE); imageView.startAnimation(translateAnimation); break; case R.id.bt_rotate://旋转 RotateAnimation rotateAnimation=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setRepeatCount(Animation.INFINITE); rotateAnimation.setDuration(3000); imageView.startAnimation(rotateAnimation); break; case R.id.bt_scale://缩放 ScaleAnimation scaleAnimation=new ScaleAnimation(Animation.RELATIVE_TO_SELF,0,1.5f,Animation.RELATIVE_TO_SELF,0,1.5f); scaleAnimation.setDuration(3000); scaleAnimation.setRepeatCount(3); imageView.startAnimation(scaleAnimation); break; case R.id.bt_alpha://透明度 AlphaAnimation alphaAnimation=new AlphaAnimation(0,1); alphaAnimation.setDuration(3000); imageView.startAnimation(alphaAnimation); } }}
注意:需使用view类的startAnimation()开启动画而不是Animation类的start()或者startNow(),否则有可能不会及时播放动画。
更多相关文章
- Android(安卓)图片缩放与旋转
- Android(安卓)ViewPager动画第三方库(MagicViewPager)
- Android中位图缩放
- Android(安卓)动画框架代码分析
- Android(安卓)Animation动画(Frame-By-Frame Animations 、Tween
- Android(安卓)图片缩放
- 【Android】【Lottie】在Android中使用Lottie动画
- Android图形报表之AchartEngine(附开发包+API文档)
- android图片闪烁或帧动画