点击时动画和更改图标颜色

3

我的图标布局如下:

<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. 在缩放时将图标的颜色更改为红色(淡入)

我该怎么做?

3个回答

0

看起来你想将LinearLayout的背景设置为一个StateListDrawable,在其按下状态下发出"红色"可绘制对象。至于动画,你需要在XML中定义一个animation,然后使用以下代码运行该动画:

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

动画文件应该是什么样子才能实现我需要的“弹出”效果?此外,StateListDrawable 可以立即更改图标的颜色,但是否有一种方法可以将颜色淡入红色? - user5473046
对于颜色渐变,您可以使用ObjectAnimator,如此处所述。如果您阅读了Android注释文档,应该能够根据给出的示例构建您的动画。 - Mike Holler

0

试试这个:

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

0

尝试使用这个进行淡入淡出

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接