1.activity.main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/iv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn01"
android:text="透明度动画"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn02"
android:text="渐变动画"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn03"
android:text="位移动画"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn04"
android:text="旋转动画"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn05"
android:text="动画集"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
2.MainActivity.java
package com.example.android_animation;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener{
private ImageView ivShow;
private Button btn01,btn02,btn03,btn04,btn05;
private Animation animation;//动画抽象类
private Animation aPha;//透明度
private Animation scale;//缩放动画
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivShow=(ImageView) findViewById(R.id.iv_show);
ivShow.setImageResource(R.drawable.ic_login2);
btn01=(Button) findViewById(R.id.btn01);
btn02=(Button) findViewById(R.id.btn02);
btn03=(Button) findViewById(R.id.btn03);
btn04=(Button) findViewById(R.id.btn04);
btn05=(Button) findViewById(R.id.btn05);
btn01.setOnClickListener(this);
btn02.setOnClickListener(this);
btn03.setOnClickListener(this);
btn04.setOnClickListener(this);
btn05.setOnClickListener(this);
btn01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
animation=new AlphaAnimation(0.1f, 1.0f);
animation.setDuration(3000);
ivShow.startAnimation(animation);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn01://透明度变化
animation=new AlphaAnimation(0.1f, 1.0f);
animation.setDuration(3000);
ivShow.startAnimation(animation);
break;
case R.id.btn02://缩放动画
animation=new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);
animation.setDuration(2000);
ivShow.startAnimation(animation);
break;
case R.id.btn03:
animation=new TranslateAnimation(0.1f, 100.0f,1.0f, 100.0f);
animation.setDuration(3000);
ivShow.startAnimation(animation);
break;
case R.id.btn04:
animation=new RotateAnimation(0, 360);
animation.setDuration(2000);
ivShow.startAnimation(animation);
break;
case R.id.btn05:
aPha=new AlphaAnimation(0.1f, 1.0f);
animation=new TranslateAnimation(0.1f, 100.0f,1.0f, 100.0f);
scale=new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);
AnimationSet set=new AnimationSet(true);
set.addAnimation(aPha);
set.addAnimation(scale);
set.addAnimation(animation);
set.setDuration(3000);
ivShow.startAnimation(set);
break;
default:
break;
}
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章