点击动画并更改图标的颜色

时间:2015-10-21 19:21:21

标签: 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"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:background="@drawable/ic_globe"
        />

</LinearLayout>

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

  1. 图像&#34; pops&#34;稍微(从24dp缩放到32dp然后再回到24dp
  2. 将图标的颜色更改(淡入)为红色,因为它的缩放
  3. 我该怎么做?

3 个答案:

答案 0 :(得分:0)

听起来你想将LinearLayout的背景设置为发出&#34; red&#34;的StateListDrawable。在压制状态下可绘制。对于动画,您必须在XML中定义animation,然后使用以下命令运行该动画:

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

答案 1 :(得分:0)

试试这个:

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

答案 2 :(得分: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);