如何向textView drawable添加动画

13 animation android drawable

我在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>
Run Code Online (Sandbox Code Playgroud)

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

有帮助吗?提前致谢

San*_*ers 11

如果设置在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();
Run Code Online (Sandbox Code Playgroud)

如果您的drawable ic_launcher可以像AnimationDrawable一样动画,这应该启动动画.呼吁d.stop()停止动画.


Vit*_*nko 10

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

假设@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>
Run Code Online (Sandbox Code Playgroud)

将此drawable作为复合一个分配给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"
    >
Run Code Online (Sandbox Code Playgroud)

要开始动画:

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();
}
Run Code Online (Sandbox Code Playgroud)

  • 超过9000! (2认同)