首先在主布局xml文件下添加一个按钮
然后再主文件里用findViewById(R.id.XX) 找到按钮并未其添加点击事件
package com.example.l01alphaanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.AnimationUtils;
public class MainActivity extends Activity {
@Override
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) {
// TODO Auto-generated method stub
/* //设置它的透明度运动
AlphaAnimation aa = new AlphaAnimation(0,1);
//设置动画的时间长度
aa.setDuration(1000);
//给按钮指明动画效果
v.startAnimation(aa);*/
v.clearAnimation();
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.aa));
}
});
}
}
这是主文件xml代码
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnAnimMe"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Animate_me" />
</RelativeLayout>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Animation 的属性可以是手动设置也可以用xml文件设置,上面代码已经贴出手动设置,下面我们来看一下xml文件的设置</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<alpha
android:fromAlpha="0"
xmlns:android="http://schemas.android.com/apk/res/android"
android:toAlpha="1"
android:duration="1000">
</alpha>
新建的xml文件属性是 Resouce File 选 Tween Animation Root Element 选alpha
旋转动画效果
:
package com.example.l02rotateanimation;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
public class MainActivity extends Activity {
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, 100, 50);
//换成相对于自身的中心点旋转
// ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//用xml文件设置
// ra = new RotateAnimation(this, attrs);
//
// ra.setDuration(1000);
findViewById(R.id.btnRotateme).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ra));
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%" >
</rotate>
依然通过配置xml文件来获取属性。
移动动画效果
TranslateAnimation 用的是此类,设置方法大同小异,都是继承自Animation .话不多说 上代码
package com.example.l03translate;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
public class MainActivity extends Activity {
private TranslateAnimation ta ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* //结果所示,此增量是从所在的位置开始,向右下角开始移动
ta = new TranslateAnimation(20, 100, 20, 100);
ta.setDuration(3000);*/
findViewById(R.id.btnTranslateMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// v.startAnimation(ta);
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.la));
}
});
}
@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;
}
}
xml文件
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnTranslateMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginTop="156dp"
android:text="@string/translate_me" />
</RelativeLayout>
xml资源文件
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="200"
android:toYDelta="200" >
</translate>
动画缩放效果
ScaleAnimation 用到此类,
package com.example.l04scaleanimation;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
public class MainActivity extends Activity {
private ScaleAnimation sa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// sa = new ScaleAnimation(0, 0, 200, 200);
/* sa = new ScaleAnimation(0, 100, 0, 100, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF);
sa.setDuration(1000);*/
findViewById(R.id.btnScaleMe).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// v.startAnimation(sa);
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.sa));
}
});
}
}
布局xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnScaleMe"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/scale_me" />
</RelativeLayout>
资源文件
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1" >
</scale>
,缩放to 指的是放大的倍数,混合动画效果 mix
用到的是 AnimationSet ;
它的构造方法
public AnimationSet(boolean shareInterpolator)
package com.example.l0sanimation;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
public class MainActivity extends Activity {
private AnimationSet as;
private TranslateAnimation ta;
private AlphaAnimation aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//as = new AnimationSet(true);
//as.addAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.set));
findViewById(R.id.btnMix).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//v.startAnimation(as);
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.set));
}
});
}
}
布局xml文件
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/mix"
android:id="@+id/btnMix"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
资源xml文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1" >
</alpha>
<translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="200"
android:toYDelta="200" >
</translate>
</set>
可以通过a.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(MainActivity.this, "动画结束了", Toast.LENGTH_SHORT).show();
}
});
添加动画监听效果
自定义动画效果
自己创建一个类,继承自 Animation
重写applyTransformation ()方法
以下是摇摆动画的代码
package com.example.l07customanimation;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class CustomAnimation extends Animation{
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
System.out.println("init");
super.initialize(width, height, parentWidth, parentHeight);
}
/**
* interpolatedTime 补间,时间
* 在动画执行完毕之后interpolatedTime是1
* 在动画执行过程中,applyTransformation方法会不断执行,
* 然后interpolatedTime 从0到1
*/
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// System.out.println(interpolatedTime);
// t.setAlpha(interpolatedTime);
t.getMatrix().setTranslate((float) (Math.sin(interpolatedTime*20)*100), 0);
super.applyTransformation(interpolatedTime, t);
}
}
package com.example.l07customanimation;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private CustomAnimation ca;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ca = new CustomAnimation();
ca.setDuration(5000);
findViewById(R.id.btn_Rock).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(ca);
}
});
}
}
xml
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/rock"
android:id="@+id/btn_Rock"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>