Android开发-自定义View-AndroidStudio(四)简介动画

转载请注明出处: http://blog.csdn.net/iwanghang/article/details/53738561
绝对博文有用,请点赞,请留言,谢谢!~

直接看GIF效果和代码:




MainActivity.java:
package com.iwanghang.propertyanimation;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ImageView iv_animation;
    private TextView tv_animation_msg;

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

        iv_animation = (ImageView) findViewById(R.id.iv_animation);
        iv_animation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 补间(视图)动画
     */
    public void testTweenAnimation(View v) {
        // 由左上向右下移动
        TranslateAnimation animation = new TranslateAnimation(0, iv_animation.getWidth(), 0, iv_animation.getHeight());
        // 由右下向左上移动
        //TranslateAnimation animation = new TranslateAnimation(iv_animation.getWidth(), 0, iv_animation.getHeight(), 0);
        animation.setDuration(3000); // 设置持续时间
        animation.setFillAfter(true); // 界面会停留在动画播放完时的界面
        iv_animation.startAnimation(animation); // 运行动画
    }

    /**
     * 属性动画
     */
    public void testPropertyAnimation(View v) {

        ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv_animation,"translationX",0,iv_animation.getWidth());
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(iv_animation,"translationY",0,iv_animation.getHeight());

        /**
         * TranslateAnimation 补间动画 一个动画
         * AnimatorSet + playTogether 属性动画 两个动画一起播放
         */
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator3,animator4);
        set.setDuration(2000);
        set.start();

//        ObjectAnimator animator = ObjectAnimator.ofFloat(iv_animation, "translationX", 0,iv_animation.getWidth());
//        ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv_animation, "translationY", 0,iv_animation.getHeight());
//        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.setDuration(2000);
//        animatorSet.setInterpolator(new BounceInterpolator());
//        //两个动画一起播放
//        animatorSet.playTogether(animator, animator2);
//        //开始播放
//        animatorSet.start();

//      //另外一种写法
//        iv_animation.animate()
//                 .translationXBy(iv_animation.getWidth())
//                 .translationYBy(iv_animation.getWidth())
//                 .setDuration(2000)
//                 .setInterpolator(new BounceInterpolator())
//                 .start();

    }

    public void reset(View v) {
        iv_animation.clearAnimation();
    }

}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context="com.iwanghang.propertyanimation.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="testTweenAnimation"
        android:text="测试补间(视图)动画" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="testPropertyAnimation"
        android:text="测试属性动画" />
    <!--我是分割-->
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#000000" />

    <TextView
        android:textColor="#000000"
        android:text="补间动画可以被clearAnimation,属性动画不可以
        因为属性动画没有setFillAfter属性"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="reset"
        android:text="重置补间动画" />

    <ImageView
        android:id="@+id/iv_animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/logo" />

    <!--我是分割-->
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#000000" />

</LinearLayout>



转载请注明出处: http://blog.csdn.net/iwanghang/article/details/53738561



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式

微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



绝对博文有用,请点赞,请留言,谢谢!~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值