• 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();
        }
    }
    

      

  • 相关阅读:
    Web大文件上传断点续传解决方案
    STL 源代码剖析 算法 stl_algo.h -- rotate
    BZOJ 1260 CQOI2007 涂色paint 动态规划
    Shiro学习(总结)
    数组与指针
    Doing Homework again(杭电1789)
    leetCode 75.Sort Colors (颜色排序) 解题思路和方法
    hdu 4786 Fibonacci Tree(最小生成树)
    Havel--Hakimi定理推断可图化 python
    最近小感——一个残疾人写的操作系统
  • 原文地址:https://www.cnblogs.com/changyiqiang/p/15035555.html
Copyright © 2020-2023  润新知