属性动画

属性动画

JAVA代码实现

代码

package com.example.day02;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_java_)
public class Java_Activity extends AppCompatActivity {

    @ViewInject(R.id.img_java)
    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_java_);
        x.view().inject(this);
        img.setImageResource(R.drawable.backs);
    }

//按钮点击事件
    @Event(value = {R.id.one,R.id.two,R.id.three,R.id.fore},type = View.OnClickListener.class)
    private void click(View view){
    //判断按钮实现动画效果
        switch (view.getId()){
            case R.id.one:
                translate();
                break;
            case R.id.two:
                alpha();
                break;
            case R.id.three:
                rotate();
                break;
            case R.id.fore:
                scale();
                break;
        }
    }

    private void scale() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "scaleX", 1.0f, 05.f);
        scaleX.setDuration(2000);
        scaleX.setRepeatCount(2);
        scaleX.setRepeatMode(ObjectAnimator.REVERSE);
        scaleX.start();
    }

    private void rotate() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "rotation",0.f,360.f,0.25f,0.25f);
        scaleX.setDuration(2000);
        scaleX.setRepeatCount(2);
        scaleX.setRepeatMode(ObjectAnimator.REVERSE);
        scaleX.start();
    }

    private void alpha() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "alpha",1.0f,0.5f);
        scaleX.setDuration(2000);
        scaleX.setRepeatCount(2);
        scaleX.setRepeatMode(ObjectAnimator.REVERSE);
        scaleX.start();
    }

    private void translate() {
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "translationX",0.0f,300.0f);
        //设置持续时间
        scaleX.setDuration(2000);
        //设置执行次数
        scaleX.setRepeatCount(2);
        //设置执行模式
        scaleX.setRepeatMode(ObjectAnimator.REVERSE);
        scaleX.start();
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Java_Activity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/one"
        android:text="平移"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/two"
        android:text="透明"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/three"
        android:text="旋转"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fore"
        android:text="缩放"
        />
    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/img_java"
        />

</LinearLayout>

Xml实现

注:”Xml实现需要在res文件下创建animator文件夹

代码

package com.example.day02;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_xml_)
public class Xml_Activity extends AppCompatActivity {

    @ViewInject(R.id.img_xml)
    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xml_);
        x.view().inject(this);
        img.setImageResource(R.drawable.backs);
    }

    @Event(value = {R.id.one,R.id.two,R.id.three,R.id.fore},type = View.OnClickListener.class)
    private void click(View view){
        switch (view.getId()){
            case R.id.one:
                translate();
                break;
            case R.id.two:
                alpha();
                break;
            case R.id.three:
                rotate();
                break;
            case R.id.fore:
                scale();
                break;
        }
    }

    private void scale() {
        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.scale);
        animator.setTarget(img);
        animator.start();
    }

    private void rotate() {
        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.rotate);
        animator.setTarget(img);
        animator.start();
    }

    private void alpha() {
        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.alpha);
        animator.setTarget(img);
        animator.start();
    }

    private void translate() {
        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.translate);
        animator.setDuration(2000);
        animator.setTarget(img);
        animator.start();
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".Xml_Activity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/one"
        android:text="平移"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/two"
        android:text="透明"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/three"
        android:text="旋转"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fore"
        android:text="缩放"
        />
    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/img_xml"
        />

</LinearLayout>

透明动画效果布局

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="Alpha"
    android:valueFrom="0.0"
    android:valueTo="1.0"
    android:repeatCount="2"
    android:repeatMode="reverse"
    android:duration="5000"
    >

</objectAnimator>

旋转动画效果布局

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="rotation"
    android:valueFrom="0"
    android:valueTo="360"
    android:duration="2000"
    android:repeatMode="reverse"
    android:repeatCount="2"
    >

</objectAnimator>

平移动画效果布局

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="translationX"
    android:valueFrom="0"
    android:valueTo="300"
    android:repeatCount="2"
    >

</objectAnimator>

缩放动画效果布局

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="scaleX"
    android:valueFrom="1.0"
    android:valueTo="0.5"
    android:repeatCount="2"
    android:repeatMode="reverse"
    android:duration="2000"
    >

</objectAnimator>
上一篇:问题记录:Animator播放动画不显示模型


下一篇:Unity——动画Animator的方法调用