1、透明动画效果
方法一:代码实现
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
rootView.findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(1000);
v.startAnimation(aa);
}
});
return rootView;
}
方法二,用配置文件的形式
第一步,新建一个aa.xml
<?xml version="1.0" encoding="utf-8"?> <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="1000" xmlns:android="http://schemas.android.com/apk/res/android"> </alpha>
第二步,代码调用
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
rootView.findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
v.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.aa));
}
});
return rootView;
2、旋转动画效果
第一种实现方式:代码
private RotateAnimation ra; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ra = new RotateAnimation(0, 360, 100, 50); ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(1000); findViewById(R.id.btnRotateme).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(ra); } });
第二种实现方式:配置文件
<?xml version="1.0" encoding="utf-8"?> <rotate android:fromDegrees="0" android:toDegrees="360" android:duration="1000" android:pivotX="50%" android:pivotY="50%" xmlns:android="http://schemas.android.com/apk/res/android"> <!-- pivotY写数值当像素解析,写百分比,就是相对自身的百分比 --> </rotate>
代码部分:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnRotateme).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ra)); } }); }
3、移动动画效果
第一种:代码实现方式
private TranslateAnimation ta; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ta = new TranslateAnimation(0, 200, 0, 200);//是增量的概念, 相对自身当前来说,向右移动200像素,向下移动200像素 ta.setDuration(1000); findViewById(R.id.btnTranslateMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(ta); } }); }
第二种配置文件方式
<?xml version="1.0" encoding="utf-8"?> <translate android:fromXDelta="0" android:toXDelta="200" android:fromYDelta="0" android:toYDelta="200" android:duration="1000" xmlns:android="http://schemas.android.com/apk/res/android"> </translate>
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnTranslateMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ta)); } }); }
4、缩放动画效果
第一种代码实现:
private ScaleAnimation sa; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // sa = new ScaleAnimation(0, 1, 0, 1); // // sa = new ScaleAnimation(0, 1, 0, 1, 100, 50); sa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//相对自己的中心点来绽放 sa.setDuration(1000); findViewById(R.id.btnScaleMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(sa); } }); }
第二种配置文件:
<?xml version="1.0" encoding="utf-8"?> <scale android:fromXScale="0" android:toXScale="1" android:fromYScale="0" android:toYScale="1" android:duration="1000" android:pivotX="50%" android:pivotY="50%" xmlns:android="http://schemas.android.com/apk/res/android"> </scale>
代码部分:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnScaleMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.sa));; } });
5、混合动画效果
第一种代码实现
private AnimationSet as; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); as = new AnimationSet(true);//是否共用动画补间,动画执行的过程,匀速,加速,减速执行,这个为动画补间 as.setDuration(1000); AlphaAnimation aa = new AlphaAnimation(0, 1); aa.setDuration(1000); as.addAnimation(aa); TranslateAnimation ta = new TranslateAnimation(200, 0, 200, 0); ta.setDuration(1000); as.addAnimation(ta); findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(as); } }); }
第二种配置文件方式:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:shareInterpolator="true" > <alpha android:fromAlpha="0" android:toAlpha="1" /> <translate android:fromXDelta="200" android:fromYDelta="200" android:toXDelta="0" android:toYDelta="0" /> </set>
代码部分:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim));; } }); }
6、动画侦听
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { Toast.makeText(MainActivity.this, "Animation start", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationRepeat(Animation animation) { Toast.makeText(MainActivity.this, "Animation repeat", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationEnd(Animation animation) { Toast.makeText(MainActivity.this, "Animation End", Toast.LENGTH_SHORT).show(); } }); v.startAnimation(animation);; } }); }
7、自定义动画效果,比如单击按钮后,让按钮摇摆
第一步自定义一个类,继承自Animation
package com.michael.lcustomanimation; import android.view.animation.Animation; import android.view.animation.Transformation; public class CustomAnim extends Animation { @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { System.out.println("init"); super.initialize(width, height, parentWidth, parentHeight); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { //interpolatedTime 从0-1 System.out.println(interpolatedTime); // t.setAlpha(interpolatedTime); // t.getMatrix().setTranslate(200, 200);//这个会直接跳过去,不是我们想要的移动的效果,分别剩上一个补间就可以了,我们可以随意自定义动画 // t.getMatrix().setTranslate(200*interpolatedTime, 200*interpolatedTime); //比如在登录窗口,密码错了,我们给他一个这样的效果 t.getMatrix().setTranslate((float) (Math.sin(interpolatedTime*10)*50), 0);//需要周期性的运动,数学里面有很多这样的函数,比如sin super.applyTransformation(interpolatedTime, t); } }
代码调用
private CustomAnim ca; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ca = new CustomAnim(); ca.setDuration(1000); findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(ca); } }); }