【问题标题】:how to add animation to textView drawable如何将动画添加到textView drawable
【发布时间】:2014-02-14 19:30:44
【问题描述】:

我也使用了这一行,在我的 textView 中添加图像:android:drawableLeft="@drawable/ic_launcher" 在我的 xml 文件中。

   <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawablePadding="5dp"
    android:gravity="center_vertical"
    android:text="@string/hello_world"
    android:textSize="14sp"
    android:textStyle="bold"
    android:drawableLeft="@drawable/ic_launcher"
    >

</TextView>

现在我想给这个drawable添加动画。 我不知道如何访问此图像。

有什么帮助吗? 提前谢谢

【问题讨论】:

  • 使用 setCompoundDrawables,复合drawables似乎没有getter
  • 你能再解释一下吗?我应该如何使用它?
  • 使用 setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) 为上、右和下 Drawable 传递 null,这样你只设置左 Drawable

标签: android animation drawable


【解决方案1】:

如果您在 XML 中设置可绘制对象,您将无法像使用 ImageViewgetDrawable() 那样访问它。相反,从您的 XML 中省略它并在您的 Activity/Fragment 中执行它:

TextView tv = (TextView) view.findViewById(R.id.textView1);
AnimationDrawable d = (AnimationDrawable) getResources().getDrawable(R.drawable.ic_launcher);
tv.setCompoundDrawables(d, null, null, null);
d.start();

如果你的drawable ic_launcher 可以像AnimationDrawable 一样动画,这应该会启动动画。致电d.stop() 停止动画。

【讨论】:

  • 你能展示一下你用过的drawable吗?以及如何反转动画?
【解决方案2】:

要制作像旋转这样的简单动画,您可以执行以下操作:

假设 @drawable/ic_launcher 是一个您想要制作动画的可绘制对象。
使用适当的值定义some_drawable.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
            android:drawable="@drawable/ic_launcher"
            android:pivotX="50%"
            android:pivotY="50%"
            android:fromDegrees="0"
            android:toDegrees="180" />
    </item>
</layer-list>

将此可绘制对象作为复合对象分配给您的 TextView:

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawablePadding="5dp"
    android:gravity="center_vertical"
    android:text="@string/hello_world"
    android:textSize="14sp"
    android:textStyle="bold"
    android:drawableLeft="@drawable/some_drawable"
    >

开始动画:

int MAX_LEVEL = 10000;

Drawable[] myTextViewCompoundDrawables = myTextView.getCompoundDrawables();
for(Drawable drawable: myTextViewCompoundDrawables) {

    if(drawable == null)
        continue;

    ObjectAnimator anim = ObjectAnimator.ofInt(drawable, "level", 0, MAX_LEVEL);
    anim.start();
}

【讨论】:

  • 10000从哪里来?
猜你喜欢
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多