介绍一下 android的基本动画:
1创建布局文件
<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: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"
android:orientation="vertical"
>
<Button
android:id="@+id/button_alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="透明度" />
<Button
android:id="@+id/button_tran"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="平移" />
<Button
android:id="@+id/button_rotate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="旋转" />
<Button
android:id="@+id/button_scal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="缩放" />
<Button
android:id="@+id/button_set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="综合" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
2 通过xml文件添加动画效果,要在res文件夹下创建anim文件,里边创建,alpha.xml,rotate.xml,scale.xml,translate.xml,set.xml
文件
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0.1"
android:toAlpha="1" >
</alpha>
这是透明度动画,duration是动画所持续的时长,fromAlpht是动画开始的透明度,toAlpha是动画结束的透明度。
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromDegrees="360"
android:toDegrees="0" >
</rotate>
这是旋转动画,设置的是时间和旋转的角度。
<scale
android:fromXScale="0"
android:fromYScale="0"
android:toXScale="1"
android:toYScale="1"
android:duration="2000"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="0"
android:pivotY="50%"
xmlns:android="http://schemas.android.com/apk/res/android">
</scale>
上述代码是缩放动画,fromXScale是缩放之前的x的比例,fromYScale这是y轴之前的缩放比例;toXScale是缩放后的x比例,toYScale是缩放后y的比例;interpolator是动画进行的效果(由慢到快再到慢),pivotX是缩放位置的x轴的坐标点,pivotY是缩放位置的y轴坐标点;
<?xml version="1.0" encoding="utf-8"?>
<translate
android:duration="2000"
android:fromXDelta="-100"
android:fromYDelta="-100"
android:toXDelta="200"
android:toYDelta="200"
xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
平移动画:设置的是时间和平移的位置这个就不多介绍了道理都一样。
3 添加java代码
代码很简单就不做过多的解释了;
package com.example.day1501;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private Button button1, button2, button3, button4,button5;
private ImageView imageView;
private Animation animation;
private Animation animation1;
private Animation animation2;
private Animation animation3;
private Animation animation4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setViews();
}
private void setViews() {
button1 = (Button) findViewById(R.id.button_alpha);
button2 = (Button) findViewById(R.id.button_tran);
button3 = (Button) findViewById(R.id.button_rotate);
button4 = (Button) findViewById(R.id.button_scal);
button5 = (Button) findViewById(R.id.button_set);
imageView = (ImageView) findViewById(R.id.imageView1);
button1.setOnClickListener(this);
button4.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button5.setOnClickListener(this);
imageView.setOnClickListener(this);
}
@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;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_alpha:
animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
imageView.setAnimation(animation);
break;
case R.id.button_tran:
animation1 = AnimationUtils.loadAnimation(this, R.anim.translate);
imageView.setAnimation(animation1);
break;
case R.id.button_rotate:
animation2 = AnimationUtils.loadAnimation(this, R.anim.rotate);
imageView.setAnimation(animation2);
break;
case R.id.button_scal:
animation3 = AnimationUtils.loadAnimation(this, R.anim.scale);
imageView.setAnimation(animation3);
break;
case R.id.button_set:
animation4 = AnimationUtils.loadAnimation(this, R.anim.set);
imageView.setAnimation(animation4);
break;
default:
break;
}
}
}
最后添加一个在anim中添加一个set文件,
就是综合了上述所有的动画一起显示,会有不一样的效果;
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000" >
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="0.1"
android:toAlpha="1" >
</alpha>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromDegrees="360"
android:toDegrees="0" >
</rotate>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="0"
android:fromYScale="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="0"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1" >
</scale>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXDelta="-100"
android:fromYDelta="-100"
android:toXDelta="200"
android:toYDelta="200" >
</translate>
</set>
以上就是android的简单的基本动画。