TextView右侧图标动画化

3
我注意到TextView有drawable top/bottom/left/right属性,你可以使用setCompoundDrawable方法在代码中设置它们。
当我使用ImageView时,我知道可以使用下面的内容创建可绘制动画(一帧接一帧): http://developer.android.com/guide/topics/graphics/drawable-animation.html 然而,我如何为TextView的drawable(例如右侧的drawable)实现这个效果呢?
谢谢。
1个回答

4

TextView 不允许通过 getter 访问复合绘制对象。所以,您可以按照您分享的链接中的方式添加动画: 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中作为左侧绘图运行动画的代码:

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);
}

如果您正在使用代码处理可绘制对象,则需要指定可绘制对象的边界。对于图像可绘制对象,您可以依靠 getIntrinsicWidthgetIntrinsicHeight,否则您需要指定一些尺寸。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接