欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android 控件动画效果实现

程序员文章站 2022-07-14 17:44:49
...

           android实现控件抖动,晃动,闪烁,高亮等动画效果实现。

         1. 抖动:

         

  /**
     * view抖动
     * @param iv
     */
    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static void viewShaking(View iv){
        ObjectAnimator animator = SeekAttentionView.tada(iv);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.start();
    }


      2.晃动:

  /**
     * view晃动
     * @param iv
     */
    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public  static void viewSloshing(View iv){
        ObjectAnimator nopeAnimator = SeekAttentionView.nope(iv);
        nopeAnimator.setRepeatCount(ValueAnimator.INFINITE);
        nopeAnimator.start();
    }
 只要传入对应的view子类即可。

其中 SeekAttentionView 是自定义类:

/**
 * 对控件的抖动
 * 对view的x轴和y轴进行0.9倍到1.1倍的缩放,同时对view进行一定角度的上下旋转。
 * Created by Administrator on 2017/11/13 0013.
 */

public class SeekAttentionView {
    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static ObjectAnimator tada(View view) {
        return tada(view, 1f);
    }

    /**
     * 控件抖动的方法
     * @param view
     * @param shakeFactor
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static ObjectAnimator tada(View view, float shakeFactor) {

        PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                Keyframe.ofFloat(0f, 1f),
                Keyframe.ofFloat(.1f, .9f),
                Keyframe.ofFloat(.2f, .9f),
                Keyframe.ofFloat(.3f, 1.1f),
                Keyframe.ofFloat(.4f, 1.1f),
                Keyframe.ofFloat(.5f, 1.1f),
                Keyframe.ofFloat(.6f, 1.1f),
                Keyframe.ofFloat(.7f, 1.1f),
                Keyframe.ofFloat(.8f, 1.1f),
                Keyframe.ofFloat(.9f, 1.1f),
                Keyframe.ofFloat(1f, 1f)
        );

        PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                Keyframe.ofFloat(0f, 1f),
                Keyframe.ofFloat(.1f, .9f),
                Keyframe.ofFloat(.2f, .9f),
                Keyframe.ofFloat(.3f, 1.1f),
                Keyframe.ofFloat(.4f, 1.1f),
                Keyframe.ofFloat(.5f, 1.1f),
                Keyframe.ofFloat(.6f, 1.1f),
                Keyframe.ofFloat(.7f, 1.1f),
                Keyframe.ofFloat(.8f, 1.1f),
                Keyframe.ofFloat(.9f, 1.1f),
                Keyframe.ofFloat(1f, 1f)
        );

        PropertyValuesHolder pvhRotate = PropertyValuesHolder.ofKeyframe(View.ROTATION,
                Keyframe.ofFloat(0f, 0f),
                Keyframe.ofFloat(.1f, -3f * shakeFactor),
                Keyframe.ofFloat(.2f, -3f * shakeFactor),
                Keyframe.ofFloat(.3f, 3f * shakeFactor),
                Keyframe.ofFloat(.4f, -3f * shakeFactor),
                Keyframe.ofFloat(.5f, 3f * shakeFactor),
                Keyframe.ofFloat(.6f, -3f * shakeFactor),
                Keyframe.ofFloat(.7f, 3f * shakeFactor),
                Keyframe.ofFloat(.8f, -3f * shakeFactor),
                Keyframe.ofFloat(.9f, 3f * shakeFactor),
                Keyframe.ofFloat(1f, 0)
        );

        return ObjectAnimator.ofPropertyValuesHolder(view, pvhScaleX, pvhScaleY, pvhRotate).
                setDuration(1000);
    }

    /**
     * 左右摇晃的效果
     * @param view
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static ObjectAnimator nope(View view) {
        int delta = view.getResources().getDimensionPixelOffset(R.dimen.spacing_medium);

        PropertyValuesHolder pvhTranslateX = PropertyValuesHolder.ofKeyframe(View.TRANSLATION_X,
                Keyframe.ofFloat(0f, 0),
                Keyframe.ofFloat(.10f, -delta),
                Keyframe.ofFloat(.26f, delta),
                Keyframe.ofFloat(.42f, -delta),
                Keyframe.ofFloat(.58f, delta),
                Keyframe.ofFloat(.74f, -delta),
                Keyframe.ofFloat(.90f, delta),
                Keyframe.ofFloat(1f, 0f)
        );

        return ObjectAnimator.ofPropertyValuesHolder(view, pvhTranslateX).
                setDuration(500);
    }
}
    

        3.闪烁

  private static ObjectAnimator animator;
   /**
     * 闪耀的textview
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    public static void flickerText(View context){
        animator = flicker(context);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.start();
    }
关掉闪烁:

    /**
     * 关掉闪烁
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    public static void notFlickerText(View context){

        if(animator==null){
            animator = flicker(context);
        }
        animator.cancel();
    }
其中flicker()方法是自定义的:

    /**
     * 闪烁方法
     */
    @RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private  static ObjectAnimator flicker(View context){
        PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                Keyframe.ofFloat(0f, 1f),
                Keyframe.ofFloat(.3f, .9f),
                Keyframe.ofFloat(.4f, .9f),
                Keyframe.ofFloat(.5f, 1.3f),
                Keyframe.ofFloat(.6f, 1.3f),
                Keyframe.ofFloat(.7f, 1.3f),
                Keyframe.ofFloat(.8f, 1.3f),
                Keyframe.ofFloat(.9f, 1.3f),
                Keyframe.ofFloat(1f, 1.3f),
                Keyframe.ofFloat(1.2f, 1.3f),
                Keyframe.ofFloat(1f, 1.3f)
        );

        PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                Keyframe.ofFloat(0f, 1f),
                Keyframe.ofFloat(.3f, .9f),
                Keyframe.ofFloat(.4f, .9f),
                Keyframe.ofFloat(.5f, 1.3f),
                Keyframe.ofFloat(.6f, 1.3f),
                Keyframe.ofFloat(.7f, 1.3f),
                Keyframe.ofFloat(.8f, 1.3f),
                Keyframe.ofFloat(.9f, 1.3f),
                Keyframe.ofFloat(1f, 1.3f),
                Keyframe.ofFloat(1.2f, 1.3f),
                Keyframe.ofFloat(1f, 1.3f)
        );
        return ObjectAnimator.ofPropertyValuesHolder(context, pvhScaleX, pvhScaleY).
                setDuration(600);
    }
     4.高亮

   

   /**
     * textview 高亮处理
     *
     * @param context:上下文
     * @param iv_backgroundl:textView后面放一个ImageView,当背景
     * @param text:需要高亮处理的文字控件
     * @param mHideAnimation:传个AlphaAnimation的对象,不用初始化,在setHideAnimation方法中判断是否为空进行初始化
     *或者关闭高亮效果
     */
    @RequiresApi(api = Build.VERSION_CODES.FROYO)
    public static void TextViewHeightLight(Context context, View iv_backgroundl, TextView text, AlphaAnimation mHideAnimation) {
        text.setTextColor(context.getResources().getColor(R.color.white));
        iv_backgroundl.setBackgroundResource(R.drawable.roundedsquare_red);
        setHideAnimation(mHideAnimation, iv_backgroundl, 1000);
    }
其中setHideAnimation()方法自定义:

    /**
     * View渐隐动画效果
     */
    @RequiresApi(api = Build.VERSION_CODES.FROYO)
    public static void setHideAnimation(AlphaAnimation mHideAnimation, View view, int duration) {
        if (null == view || duration < 0) {
            return;
        }
        if (null != mHideAnimation) {
            mHideAnimation.cancel();
        }
        mHideAnimation = new AlphaAnimation(1.0f, 0.0f);
        mHideAnimation.setDuration(duration);
        mHideAnimation.setFillAfter(true);
        view.startAnimation(mHideAnimation);
    }






相关标签: android 动画