Android 在单击时设置图标的动画并更改其颜色

Android 在单击时设置图标的动画并更改其颜色,android,android-layout,android-activity,android-animation,android-xml,Android,Android Layout,Android Activity,Android Animation,Android Xml,我的图标有如下布局: <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"

我的图标有如下布局:

<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>

我希望这样,如果单击布局:

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

  • 如何执行此操作?

    听起来像是要将线性布局的背景设置为在按下状态下发出“红色”可绘制图像的。对于动画,您必须在XML中定义一个,然后使用以下内容运行该动画:

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

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

    试试这个,淡入淡出

    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);
    

    动画文件看起来像什么来执行我需要的“弹出”操作?此外,StateListDrawable会立即更改图标的颜色,但是有没有办法将颜色淡入红色?对于颜色淡入,可以使用ObjectAnimator,如前所述。如果您阅读了android注释文档,您应该能够根据它们给出的示例构建动画。