Android属性动画,和ButterKnife的使用方法

目录

1.属性动画

        1.什么是属性动画       

        2.ValueAnimator的使用方法

                 1.首先先创建四个按钮和一个图片 ,还要给布局设置ID 

                 2.接下来在MainActivity里进行动画操作,这个是上下移动的动画

                3.这个是缩放效果的动画

                4.这个是旋转的同时进行透明度变化的动画

                5.这个是圆形旋转的动画

2.ButterKnife(黄油刀)

        1.引入外部资源

        2.使用@BindView()

        3.使用@OnClick()

        4.在onCreate()中 ButterKnife.bind(this)

        5.整体代码


1.属性动画

        1.什么是属性动画       

  • Andoid 3.0引入,可以说是补间动画的增强版,不止可以实现四种动画效果,可以定义任何属性的变化;

  • 执行动画的对象不只是U控件。可以对任何对象执行动画(不管是否显示在屏幕上)

  • 属性动画通过对目标对象进行赋值来修改其属性,上面那个按钮问题就不存在了

        2.ValueAnimator的使用方法

                直接上案例   

                        1.首先先创建四个按钮和一个图片 ,还要给布局设置ID 

<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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:padding="15dp"
    android:id="@+id/ly_root">

    <Button
        android:id="@+id/but_01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动画1" />

    <Button
        android:id="@+id/but_02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动画2" />

    <Button
        android:id="@+id/but_03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动画3" />

    <Button
        android:id="@+id/but_04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动画4" />

    <ImageView
        android:id="@+id/img_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/img_babi" />
</LinearLayout>

                        2.接下来在MainActivity里进行动画操作,这个是上下移动的动画

                                声明容器和获取监听就不展示了哈

 public void onClick(View view) {
//这个是获取页面的宽和高
        width=ly_root.getWidth();
        height=ly_root.getHeight();
//这个是设置图片移动的区间
        ValueAnimator valueAnimator=ValueAnimator.ofInt(height,0,height/4,height/2,height/4*3,height);
//设置时间
        valueAnimator.setDuration(3000l);
//设置动画,开始让图片动起来
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int x= (int) valueAnimator.getAnimatedValue();
                int y=width/2;
//引用方法,把x轴和y轴和图片传过去
                ac(x,y,img_show);
            }
        });

        valueAnimator.setInterpolator(new LinearInterpolator());
//开启动画
        valueAnimator.start();
    }
});

//定义一个修改ImageView位置的方法
public void ac(int x,int y,View view){
        int left=y-view.getWidth()/2;
        int top=x-view.getHeight();
        int width1=left+view.getWidth();
        int height1=top+view.getHeight();
        view.layout(left,top,width1,height1);
    }

                3.这个是缩放效果的动画

  private void scaleAnimator(){
    
        //这里故意用两个是想让大家体会下组合动画怎么用而已~
        final float scale = 0.5f;
        AnimatorSet scaleSet = new AnimatorSet();
        ValueAnimator valueAnimatorSmall = ValueAnimator.ofFloat(1.0f, scale);
        valueAnimatorSmall.setDuration(500);

        ValueAnimator valueAnimatorLarge = ValueAnimator.ofFloat(scale, 1.0f);
        valueAnimatorLarge.setDuration(500);

        valueAnimatorSmall.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float scale = (Float) animation.getAnimatedValue();
                img_babi.setScaleX(scale);
                img_babi.setScaleY(scale);
            }
        });
        valueAnimatorLarge.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float scale = (Float) animation.getAnimatedValue();
                img_babi.setScaleX(scale);
                img_babi.setScaleY(scale);
            }
        });

        scaleSet.play(valueAnimatorLarge).after(valueAnimatorSmall);
        scaleSet.start();

        //其实可以一个就搞定的
//        ValueAnimator vValue = ValueAnimator.ofFloat(1.0f, 0.6f, 1.2f, 1.0f, 0.6f, 1.2f, 1.0f);
//        vValue.setDuration(1000L);
//        vValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                float scale = (Float) animation.getAnimatedValue();
//                img_babi.setScaleX(scale);
//                img_babi.setScaleY(scale);
//            }
//        });
//        vValue.setInterpolator(new LinearInterpolator());
//        vValue.start();
    }

                4.这个是旋转的同时进行透明度变化的动画

 private void raAnimator(){
        ValueAnimator rValue = ValueAnimator.ofInt(0, 360);
        rValue.setDuration(1000L);
        rValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int rotateValue = (Integer) animation.getAnimatedValue();
                img_babi.setRotation(rotateValue);
                float fractionValue = animation.getAnimatedFraction();
                img_babi.setAlpha(fractionValue);
            }
        });
        rValue.setInterpolator(new DecelerateInterpolator());
        rValue.start();
    }

                5.这个是圆形旋转的动画

protected void circleAnimator() {
        width = ly_root.getWidth();
        height = ly_root.getHeight();
        final int R = width / 4;
        ValueAnimator tValue = ValueAnimator.ofFloat(0,
                (float) (2.0f * Math.PI));
        tValue.setDuration(1000);
        tValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 圆的参数方程 x = R * sin(t) y = R * cos(t)
                float t = (Float) animation.getAnimatedValue();
                int x = (int) (R * Math.sin(t) + width / 2);
                int y = (int) (R * Math.cos(t) + height / 2);
                moveView(img_babi, x, y);
            }
        });
        tValue.setInterpolator(new DecelerateInterpolator());
        tValue.start();
    }
}

2.ButterKnife(黄油刀)

        ButterKnife就是可以简便点击事件的,其实还有其他作用的哈,不过我这学了这一个,就先展示这个

        1.引入外部资源

                资源文件 

    implementation 'com.jakewharton:butterknife:10.2.3'// 添加此依赖
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'// 添加此规则

        2.使用@BindView()

                括号里面方按钮地址

@BindView(R.id.button1)
    Button button1;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.button3)
    Button button3;

        3.使用@OnClick()

                括号里面方按钮地址

    @OnClick(R.id.button1)
    private void an1(){
        Toast.makeText(MainActivity2.this,"按钮1",Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.button2)
    private void an2(){
        Toast.makeText(MainActivity2.this,"按钮2",Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.button3)
    private void an3(){
        Toast.makeText(MainActivity2.this,"按钮3",Toast.LENGTH_SHORT).show();
    }

        4.在onCreate()中 ButterKnife.bind(this)

ButterKnife.bind(MainActivity2.this);

        5.整体代码

package com.hopu.day11;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity2 extends AppCompatActivity {
    @BindView(R.id.button1)
    Button button1;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.button3)
    Button button3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        ButterKnife.bind(MainActivity2.this);

    }

    @OnClick(R.id.button1)
    public void an1(){
        Toast.makeText(MainActivity2.this,"按钮1",Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.button2)
    public void an2(){
        Toast.makeText(MainActivity2.this,"按钮2",Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.button3)
    public void an3(){
        Toast.makeText(MainActivity2.this,"按钮3",Toast.LENGTH_SHORT).show();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值