使用动画

时间:2017-10-28 14:38:28

标签: android animation android-animation textview

我正在开发一个Android应用程序我想在单个圆形区域中显示字母表,我还需要圆形区域为白色,字母表为primaryColor我的应用程序,

这是我的textView

   <TextView
            android:id="@+id/first"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="A"
            android:background="@drawable/circle" />

这是circle.xml文件夹中drawable的代码:

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" >
    <stroke android:width="1dp" android:color="@color/colorPrimaryDark" />
    <gradient android:startColor="@color/white" android :endColor="@color/white"
    android:angle="270"
      />
 </shape>

这是filled_circle

的代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="oval" >
  <stroke android:width="1dp" android:color="@color/colorPrimaryLight" />
   <gradient android:startColor="@color/colorPrimaryLight" 
      android:endColor="@color/colorPrimaryLight"
    android:angle="270"
    />
</shape>

java文件中的代码:

 @Override
public void onClick(View v) {


}

问题 当单击每个字母表的textarea时,白色应该淡出,primaryColor应该淡入,并带有动画。 有人可以帮忙,在android 4及以上版本的animation库的帮助下完成这项工作吗?

我目前正在尝试用其他资源替换textview的背景并将动画应用到它,但我完全接受任何新建议和执行此操作的新方法, 我还提供了上面的当前代码,但如果我必须进行基本的更改或修改以使其与animation库一起使用,请告诉我?

1 个答案:

答案 0 :(得分:1)

我建议您执行以下操作:

不要试图在单个背景中绘制所有内容的动画。你需要:

<FrameLayout>
  <View/> //Just a view with your white circular background
  <View/> //Another view with circular background, but this one is gonna display the color
  <TextView/> //Centered, your alphabet letter
</FrameLayout>

所以你想把所有这些放在自定义视图中。白色圆形背景视图将留在它。您可以使用第二个圆形视图来设置字母表的字母颜色。有很多简单的方法可以从自定义可绘制列表层中的图层更改颜色(例如Programmatically change color of shape in layer list)。

首次展开自定义视图时,您希望将彩色圆形视图的可见性设置为不可见。白色圆形视图将保持可见。 TextView始终位于顶部。

重写onClick方法,使您的自定义视图实现View.OnClickListener,您应该放置代码来运行动画。您需要做的唯一事情是对彩色圆圈视图进行alpha动画,并且在onAnimationEnd()挂钩中,您应该将视图的可见性设置为可见。您将需要在自定义视图中使用布尔变量来跟踪是否显示彩色圆圈。这样,如果正在显示彩色圆圈视图并且用户点击它,您可以执行反向动画以仅显示白色圆圈视图。

请记住在自定义视图中执行所有操作以保持其良好和干净。还要记得添加一些自定义属性,这样就可以从xml中设置彩色圆圈视图的颜色。

我希望这能让你了解如何实现它,IDK真的有时间作为例子,但是如果你被某个地方卡住或者你没有得到我说的话,请告诉我。< / p>

相关问题