Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textview drawable right animation

Tags:

android

I noticed that textview have drawable top/bottom/left/right. And you can set it in the code using setCompundDrawable.

I know I can use whats below to create drawable animation (framees one after the other) when I have imageview. http://developer.android.com/guide/topics/graphics/drawable-animation.html

However, how can I do it for the drawable of the textview (Drawable Right for example)?

Thank you

like image 790
Snake Avatar asked Sep 18 '26 10:09

Snake


1 Answers

TextView doesn't allow access to compound drawables via getter. So you can add animation in next way, from the link you have shared: 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>

And here is code that can run animation in TextView as left 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);
}

if you are working with drawable from the code you need to specify drawable bounds. In case of image drawables you can relay on getIntrinsicWidth, getIntrinsicHeight otherwise you need to specify some dimensions.

like image 153
andrew Avatar answered Sep 20 '26 22:09

andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!