textview drawable右动画

时间:2014-05-01 21:28:55

标签: android

我注意到textview有可绘制的上/下/左/右。您可以使用setCompundDrawable在代码中设置它。

我知道当我有imageview时,我可以使用下面的内容来创建可绘制的动画(一个接一个地构图)。 http://developer.android.com/guide/topics/graphics/drawable-animation.html

但是,我如何才能为textview的drawable(例如Drawable Right)做到这一点?

谢谢

1 个答案:

答案 0 :(得分:3)

TextView不允许通过getter访问复合drawable。因此,您可以从已共享的链接以下一种方式添加动画: rocket_thrust.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

这里的代码可以在TextView中以左侧drawable运行动画:

AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  rocketAnimation = (AnimationDrawable) getResources().getDrawable(R.drawable.rocket_thrust);
  rocketAnimation.setBounds(0, 0, rocketAnimation.getIntrinsicWidth(), rocketAnimation.getIntrinsicHeight());

  TextView rocketText = (TextView) findViewById(R.id.text_view);
  rocketText.setCompoundDrawables(rocketAnimation, null, null, null);
}

public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
    rocketAnimation.start();
    return true;
  }
  return super.onTouchEvent(event);
}

如果您正在从代码中使用drawable,则需要指定drawable bounds。如果是图片绘图,您可以在getIntrinsicWidthgetIntrinsicHeight转发,否则您需要指定一些尺寸。

相关问题