简单实用ObjectAnimator属性动画

\\


activity_main.xml


<?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="com.example.myapplication.MainActivity">

    <Button
        android:onClick="weiyi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="位移"/>
    <Button
        android:onClick="xuanzhuan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="旋转"/>

    <Button
        android:onClick="jianbian"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="渐变"/>
    <Button
        android:onClick="suofang"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="缩放"/>
    <Button
        android:onClick="jihe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="集合"/>
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_margin="20dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@mipmap/n"/>
</LinearLayout>

MainActivity

package com.example.myapplication;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ImageView iv;
    private ObjectAnimator animator;

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

    private void initView() {
        iv = findViewById(R.id.iv);
    }

    /**
     * 位移
     * @param view
     */

    public void weiyi(View view) {
        animator = ObjectAnimator.ofFloat(iv,view.TRANSLATION_X,0,200f,0,0);
        animator.setDuration(3000).start();
    }

    /**
     * 旋转
     * @param view
     */
    public void xuanzhuan(View view) {
        animator = ObjectAnimator.ofFloat(iv,view.ROTATION_Y,0,360f);
        animator.setDuration(3000).start();
    }

    /**
     * 渐变
     * @param view
     */
    public void jianbian(View view) {
        animator = ObjectAnimator.ofFloat(iv,view.ALPHA,0,1f,0,1f);
        animator.setDuration(3000).start();
    }

    /**
     * 缩放
     * @param view
     */
    public void suofang(View view) {
        animator = ObjectAnimator.ofFloat(iv,view.SCALE_Y,0,2f,0,1f);
        animator.setDuration(3000).start();
    }

    /**
     * 集合动画
     * @param view
     */
    public void jihe(View view) {
        //组合动画
        PropertyValuesHolder trans=PropertyValuesHolder.ofFloat(view.TRANSLATION_X,0,200f);
        PropertyValuesHolder rotate=PropertyValuesHolder.ofFloat(view.ROTATION_Y,0,360f);
        PropertyValuesHolder alpha=PropertyValuesHolder.ofFloat(view.ALPHA,0,1f,0,1f);
        PropertyValuesHolder scale=PropertyValuesHolder.ofFloat(view.SCALE_Y,0,2f,0,1f);

        //给图片添加多动画
        ObjectAnimator animator= ObjectAnimator.ofPropertyValuesHolder(iv,trans,rotate,alpha,scale);
        animator.setDuration(3000).start();

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值