android动画基础--旋转移动平移缩放

package com.imooc.viewanim;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
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;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btnAlpha(View view) {
        //透明度动画 public AlphaAnimation(float fromAlpha, float toAlpha){}
        AlphaAnimation aa = new AlphaAnimation(0, 1);
        //持续时间
        aa.setDuration(1000);
        view.startAnimation(aa);
    }

    public void btnRotate(View view) {
//        旋转动画 public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY){}
//        分别对应起始角度(toDegrees-fromDegrees),旋转中心点。
        RotateAnimation ra = new RotateAnimation(90, 240, 50, 50);
        ra.setDuration(1000);
        view.startAnimation(ra);
    }

    public void btnRotateSelf(View view) {
        //旋转动画,设置旋转参考系
        RotateAnimation ra = new RotateAnimation(0, 360,
                RotateAnimation.RELATIVE_TO_SELF, 0.5F,
                RotateAnimation.RELATIVE_TO_SELF, 0.5F);
        ra.setDuration(1000);
        view.startAnimation(ra);
    }

    public void btnTranslate(View view) {
        //平移动画
        //自身中点做坐标原点(0,0) 向右移动200,向下移动300
        TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);
        ta.setDuration(1000);
        view.startAnimation(ta);
    }

    public void btnScale(View view) {
        //缩放动画 public ScaleAnimation(float fromX, float toX, float fromY, float toY){}
//        向右放大2,向下放大2 ,后恢复
        ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2);
        sa.setDuration(1000);
        view.startAnimation(sa);
    }

    public void btnScaleSelf(View view) {
        //缩放动画,设置缩放中心点。
        ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1,
                Animation.RELATIVE_TO_SELF, 0.5F,
                Animation.RELATIVE_TO_SELF, 0.5F);
        sa.setDuration(1000);
        view.startAnimation(sa);
    }
    //动画集合
    public void btnSet(View view) {
        AnimationSet as = new AnimationSet(true);
        as.setDuration(1000);

        AlphaAnimation aa = new AlphaAnimation(0, 1);
        aa.setDuration(1000);
        as.addAnimation(aa);

        TranslateAnimation ta = new TranslateAnimation(0, 100, 0, 200);
        ta.setDuration(1000);
        as.addAnimation(ta);

        view.startAnimation(as);
    }

}
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Alpha"
        android:layout_margin="10dp"
        android:onClick="btnAlpha" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rotate"
        android:layout_margin="10dp"
        android:onClick="btnRotate" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rotate_self"
        android:layout_margin="10dp"
        android:onClick="btnRotateSelf" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Translate"
        android:layout_margin="10dp"
        android:onClick="btnTranslate" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Scale"
        android:layout_margin="10dp"
        android:onClick="btnScale" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Scale_Self"
        android:layout_margin="10dp"
        android:onClick="btnScaleSelf" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Anim Set"
        android:layout_margin="10dp"
        android:onClick="btnSet" />

LinearLayout>

android动画基础--旋转移动平移缩放_第1张图片

你可能感兴趣的:(Android,例子)