【问题标题】:Animate and change color of icon on click单击动画并更改图标的颜色
【发布时间】:2016-01-20 22:26:15
【问题描述】:

我的图标有这样的布局:

<LinearLayout
    android:id="@+id/icon_layout"
    android:layout_width="36dp"
    android:layout_height="36dp"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:background="@drawable/ic_globe"
        />

</LinearLayout>

我想这样做,如果点击布局:

  1. 图像略微“弹出”(从24dp 缩放到32dp,然后再回到24dp
  2. 在缩放时将图标的颜色更改(淡入)为红色

我该怎么做?

【问题讨论】:

    标签: android android-layout android-activity android-animation android-xml


    【解决方案1】:

    听起来您想将 LinearLayout 的背景设置为 StateListDrawable,它会在按下状态下发出“红色”可绘制对象。对于动画,您必须在 XML 中定义一个 animation,然后运行该动画:

    ImageView iconLayout = (ImageView) findViewById(R.id.icon_layout);
    Animation expandAnimation = AnimationUtils.loadAnimation(this, R.anim.icon_expand);
    iconLayout.startAnimation(expandAnimation);
    

    【讨论】:

    • 动画文件看起来像什么来做我需要的“pop”?此外,StateListDrawable 会立即 更改图标的颜色,但有没有办法淡入 将颜色变为红色?
    • 对于颜色渐变,您可以使用 ObjectAnimator,如 here 所述。如果您阅读了 android 注释文档,您应该能够根据他们提供的示例构建您的动画。
    【解决方案2】:

    试试这个:

    ImageView imageView = (ImageView) findViewById(R.id.icon);
    imageView.animate().scaleX(newScaleX).scaleY(newScaleY).alpha(1).setDuration(300).start();
    

    【讨论】:

      【解决方案3】:

      试试这个淡入淡出

      private Animation animation;
      
      animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
                  animation.setDuration(500); // duration - half a second
                  animation.setInterpolator(new LinearInterpolator());
                  animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
                  animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
      
                  //Use this animation where ever you want to use
                  icon.startAnimation(animation);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-04
        • 1970-01-01
        • 1970-01-01
        • 2015-10-17
        • 2017-06-23
        • 2019-07-14
        • 2018-10-25
        • 1970-01-01
        相关资源
        最近更新 更多