• Android中使用抖动动画吸引来用户注意-属性动画


    使用属性动画实现view抖动效果

    如图:

    代码:

    1、布局

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity2">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:src="@drawable/shake"
            android:id="@+id/iv_shake"></ImageView>
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:id="@+id/btn_shake"
            android:text="抖动"
            app:layout_constraintTop_toBottomOf="@+id/iv_shake"
            app:layout_constraintLeft_toLeftOf="parent"
            android:layout_marginTop="30dp"></Button>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

     2、MainActivity

     

    public class MainActivity2 extends AppCompatActivity {
    
        private ImageView iv_shake;
        private Button btn_shake;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            iv_shake = findViewById(R.id.iv_shake);
    
            btn_shake = findViewById(R.id.btn_shake);
            btn_shake.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                   startShakeByPropertyAnim(iv_shake, 0.9f, 1.1f, 10f, 1000);
                }
            });
    
    
    
    
    
        }
    
       
    
        private void startShakeByPropertyAnim(View view, float scaleSmall, float scaleLarge, float shakeDegrees, long duration) {
            if (view == null) {
                return;
            }
            //TODO 验证参数的有效性
    
            //先变小后变大
            PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                    Keyframe.ofFloat(0f, 1.0f),
                    Keyframe.ofFloat(0.25f, scaleSmall),
                    Keyframe.ofFloat(0.5f, scaleLarge),
                    Keyframe.ofFloat(0.75f, scaleLarge),
                    Keyframe.ofFloat(1.0f, 1.0f)
            );
            PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                    Keyframe.ofFloat(0f, 1.0f),
                    Keyframe.ofFloat(0.25f, scaleSmall),
                    Keyframe.ofFloat(0.5f, scaleLarge),
                    Keyframe.ofFloat(0.75f, scaleLarge),
                    Keyframe.ofFloat(1.0f, 1.0f)
            );
    
            //先往左再往右
            PropertyValuesHolder rotateValuesHolder = PropertyValuesHolder.ofKeyframe(View.ROTATION,
                    Keyframe.ofFloat(0f, 0f),
                    Keyframe.ofFloat(0.1f, -shakeDegrees),
                    Keyframe.ofFloat(0.2f, shakeDegrees),
                    Keyframe.ofFloat(0.3f, -shakeDegrees),
                    Keyframe.ofFloat(0.4f, shakeDegrees),
                    Keyframe.ofFloat(0.5f, -shakeDegrees),
                    Keyframe.ofFloat(0.6f, shakeDegrees),
                    Keyframe.ofFloat(0.7f, -shakeDegrees),
                    Keyframe.ofFloat(0.8f, shakeDegrees),
                    Keyframe.ofFloat(0.9f, -shakeDegrees),
                    Keyframe.ofFloat(1.0f, 0f)
            );
    
            ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, scaleXValuesHolder, scaleYValuesHolder, rotateValuesHolder);
            objectAnimator.setDuration(duration);
            objectAnimator.start();
        }
    }
    

      

  • 相关阅读:
    js广告随窗口滚动小案例
    CSS中Float概念相关文章
    图片展示javascript各类型的关系
    ajax调用异常
    设置修改 Apache 文件根目录 (Document Root)
    SQL查询今天与昨天的记录,及本月记录、本周记录
    服务器角色
    javaScript进阶昂贵的集合
    .net实体新解
    数组操作时避免空值出现
  • 原文地址:https://www.cnblogs.com/changyiqiang/p/15035555.html
Copyright © 2020-2023  润新知