Android-Animation动画基础复习
动画是我最不了解的一部分,所以必须得学习了,之前都没有了解过,还是好好看书学习,
写几个例子记录一下,不会安卓动画,那么界面效果会大打折扣。
首先是布局文件:
activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:id="@+id/id_image"
android:scaleType="fitXY"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:src="@mipmap/anim3"/>
<LinearLayout
android:layout_marginTop="40dp"
android:id="@+id/id_layout1"
android:layout_below="@id/id_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/id_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明"/>
<Button
android:id="@+id/id_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
主活动:
MainActivity.java
package com.xieth.as.animdemoone;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Animation loadAnimation = null;
private ImageView image = null;
//透明按钮
private Button btnAlpha = null;
// 缩放按钮
private Button btnScale = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
image = (ImageView) findViewById(R.id.id_image);
btnAlpha = (Button) findViewById(R.id.id_alpha);
btnAlpha.setOnClickListener(this);
btnScale = (Button) findViewById(R.id.id_scale);
btnScale.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.id_alpha:
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
image.startAnimation(loadAnimation);
break;
case R.id.id_scale:
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.scale);
image.startAnimation(loadAnimation);
break;
default:
break;
}
}
}
其中启动动画是:
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
image.startAnimation(loadAnimation);
透明度动画
alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="3000"
android:fromAlpha="0.1"
android:toAlpha="1.0"
>
</alpha>
</set>
运行:
缩放
scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="2000"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0"
/>
</set>
运行:
位移动画
translate.xml
启动动画的代码都一样
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="10"
android:fromYDelta="10"
android:toXDelta="100"
android:toYDelta="100"
/>
</set>
运行:
旋转动画
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotY="50%"
android:pivotX="50%"
android:toDegrees="+360"
/>
</set>
运行:
组合动画
先位移再旋转
case R.id.id_alpha2:
// 先位移再旋转
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
image.startAnimation(loadAnimation);
final Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.rotate);
loadAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
// 当这个动画结束之后
@Override
public void onAnimationEnd(Animation animation) {
image.startAnimation(animation2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
break;
运行:
推荐一篇动画的博客,点击进入