shanjohri shanjohri - 1 month ago 13
Android Question

Animations in custom view - android

I am having a little confusion in handling animations when it comes to my custom view.
What I'm having right now is a class like this:

public class ConcreteView extends RelativeLayout {
//blah blah code
public ConcreteView(Context context, AttributeSet attrs) {
//blah blah code
}
//blah blah code
}


and an xml like this:

<com.package.ConcreteView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:id="@+id/suggest"
app:headerText="This is a custom view. Animations yet to be implemented"
app:headertextColor="#212121"
app:footerText="Frostbite engine"
app:footertextColor="#424242"
app:footertextSize="9"
app:headerTextFontSize="13"/>


Now what I'm looking for is a way to implement all of the basic animations (like fadeIn, fadeOut, slide In/Out etc.) inside this class (programmatically) so that I just have to make an instance of ConcreteView and access the setAnimation method. Any Ideas?

Thanks,
Shantanu

Answer

Inside your implementation of the custom view, you can always access the view and the view functions using the this operator. Using that, you should be able to change the properties of the view as per the animation.

So, it would be something like

public class ConcreteView extends RelativeLayout {
      //blah blah code
      public ConcreteView(Context context, AttributeSet attrs) {
          //blah blah code
      }
       //blah blah code

      public void blahAnimationFadeOut(){
           this.setAlpha(0.5); // dummy example to access all view functions, assuming you can write your own animations programatically
      }
}