【问题标题】:How to set rounded corner on ImageView while using Animation on it如何在 ImageView 上使用动画时设置圆角
【发布时间】:2017-03-28 07:50:16
【问题描述】:

我有一个像这样的 ImageView 布局:

正如在 imageView 角落中提到的那样,在其上使用动画时不会保持圆形。

在使用动画时如何使其变圆?

布局代码:

    <TableLayout
        android:layout_width="wrap_content"
        android:id="@+id/table1"
        android:gravity="center"
        android:layout_height="wrap_content">

    <TableRow>

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/img1_1"
        android:background="@drawable/button"
        android:layout_margin="3dp"
        />

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/img1_2"
        android:background="@drawable/button"
        android:layout_margin="3dp"
        />
    </TableRow>
    <TableRow>

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/img1_3"
        android:background="@drawable/button"
        android:layout_margin="3dp"
   />

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/img1_4"
        android:background="@drawable/button"
        android:layout_margin="3dp"
       />
    </TableRow>
</TableLayout>

Java 代码:

  ObjectAnimator animator = ObjectAnimator.ofInt(findViewById(id), "backgroundColor", Color.RED, Color.parseColor("#4caf41")).setDuration(5000);
        Toast.makeText(Game.this, "index" + findViewById(id), Toast.LENGTH_LONG).show();
        animator.setEvaluator(new ArgbEvaluator());
        animator.start();   

【问题讨论】:

标签: android android-layout android-animation android-imageview background-color


【解决方案1】:

当动画开始时,您正在按下 Imageview。所以你可以使用

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/numpad_button_bg_normal"></item>

</selector> 

按下按钮后使用相同的图像.. 可能会对您有所帮助..

【讨论】:

  • 动画在创建时开始。没有任何点击/按下/触摸事件
【解决方案2】:

您可以使用滑行。它还允许您添加动画。

https://futurestud.io/tutorials/glide-custom-animations-with-animate

【讨论】:

    【解决方案3】:

    我已解决此问题以在此行中进行更改:

      ObjectAnimator animator = ObjectAnimator.ofInt(findViewById(id), "backgroundColor", Color.RED, Color.parseColor("#4caf41")).setDuration(5000);
    

    像这样:

      ObjectAnimator animator = ObjectAnimator.ofInt(findViewById(id), "backgroundResource", R.drawable.original_img, R.drawable.org_state).setDuration(2000);
    

    我设置了 backgroundResource 而不是 backgroundColor。并设置了一个图像而不是十六进制颜色代码。

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2021-03-21
      • 2011-07-23
      相关资源
      最近更新 更多