0

i have a method i created that is called throughout my project, i made it because i have a Crouton (toast) that tells a user to activate there account, or it will also alert them when there is no valid internet connection... And i dont want it to interfere with the top of my View because theres user actions that are important there. Im using a RelativeLayout

First off, this piece of code doesnt work anyways, but as i was fixing it i realized my bar i have at the bottom to switch between different Activities is now gone because it was slid down, im thinking i can just resize the hieght.

can anyone point me int he right direction for two things, one resizing height instead of sliding the whole view down, and two, help me fix the crash im getting, which occurs on setLayoutParam

i call this like this teknoloGenie.slideViewDown("100", findViewById(R.id.RootView));

public void slideViewDown(final String distX, final View view) {

        final TranslateAnimation slideDown = new TranslateAnimation(0, 0, 0, Float.parseFloat(distX));

        slideDown.setDuration(500);
        slideDown.setFillEnabled(true);
        slideDown.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }
            @Override
            public void onAnimationRepeat(Animation animation) {

            }
            @Override public void onAnimationEnd(Animation animation) {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();
                params.addRule(RelativeLayout.CENTER_HORIZONTAL, -1);
                params.setMargins(0, view.getTop()+Integer.parseInt(distX), 0, 0);
                view.setLayoutParams(params);
                view.requestLayout();
            }
        });
        view.startAnimation(slideDown);
    }
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
NodeDad
  • 1,519
  • 2
  • 19
  • 48

1 Answers1

2

If you want to animate the height of a View, you need to write your own custom-animation and modify the LayoutParams of your animated view. In this example, the animation animates the height of the animated View.

This is how it could look like:

public class ResizeAnimation extends Animation {

    private int startHeight;
    private int deltaHeight; // distance between start and end height
    private View view;

    /**
     * constructor, do not forget to use the setParams(int, int) method before
     * starting the animation
     * @param v
     */
    public ResizeAnimation (View v) {
        this.view = v;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        view.getLayoutParams().height = (int) (startHeight + deltaHeight * interpolatedTime);
        view.requestLayout();
    }

    /**
     * set the starting and ending height for the resize animation
     * starting height is usually the views current height, the end height is the height
     * we want to reach after the animation is completed
     * @param start height in pixels
     * @param end height in pixels
     */
    public void setParams(int start, int end) {

        this.startHeight = start;
        deltaHeight = end - startHeight;
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}   

In code, create a new Animation and apply it to the RelativeLayout that you want to animate:

View v = findViewById(R.id.youranimatedview);

// getting the layoutparams might differ in your application, it depends on the parent layout
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) v.getLayoutParams();

ResizeAnimation a = new ResizeAnimation(v);
a.setDuration(500);

// set the starting height (the current height) and the new height that the view should have after the animation
a.setParams(lp.height, newHeight);

v.startAnimation(a);

To your LayoutParams problem:

My guess is that you are getting a ClassCastException because you are not using the correct LayoutParams class. If your animated view for example is contained by a RelativeLayout, you can only set RelativeLayout.LayoutParams to it. If your View is contained by a LinearLayout, you can only set LinearLayout.LayoutParams for your View.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • If i correct your approach will set it to a set height, not a height where it just 'chops' off 100dp or whatever the case may be for me. So to solve that portion would i just do a lp.height - 100 then asign that to newHeight? Im gonna need a little bit to implement this and test it out before accepting, but so far solid answer. – NodeDad Sep 20 '13 at 08:42
  • In the setParams(start, end) method, the start variable should always be the current height, the end variable can be whatever you want. It is the final height that the View will have after the animation. – Philipp Jahoda Sep 20 '13 at 08:46
  • getting this with your code java.lang.RuntimeException: Unable to start activity ComponentInfo{com.penguintech.atmeapp/com.penguintech.atmeapp.DashboardActivity}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams – NodeDad Sep 20 '13 at 08:51
  • I warned you about that, read the full answer and change RelativeLayout.LayoutParams to FrameLayout.LayoutParams. – Philipp Jahoda Sep 20 '13 at 08:52
  • I see what you mean now... Okay, well... why do i need FrameLayout when my XML is a RelativeLayout? Can you explain, just so i understand better. – NodeDad Sep 20 '13 at 08:54
  • 1
    It depends on the container layout. The LayoutParams class needs to be the class of the Layout the Animated view is inside of. – Philipp Jahoda Sep 20 '13 at 08:55
  • I see, so since im tackling my RelativeLayout, in essence it is inside a FrameLayout (the screen itself) am i correct? Also its doing what i want but the wrong way, its resizing from bottom up, i need it to go Top to down. What i mean is the bottom is blank black and not the top. – NodeDad Sep 20 '13 at 09:04
  • solved the bottom issue, i combined the slideDown method i made earlier, then resized, and now im getting the result i want. Thank you so much! – NodeDad Sep 20 '13 at 09:54