0

So my problem is actually related to animations. I want to animate the red square in this linear (horizontal) layout: http://image.noelshack.com/fichiers/2014/22/1401291148-stck.jpeg

I want to use an animation that upscales the red square, so I want to get this: http://image.noelshack.com/fichiers/2014/22/1401291147-stck2.jpeg (The red square goes over the left and right grey squares. I don't care it is cropped by the height of the linear layout, I know how to deal with this)

Now if I launch the animation, it goes over the left grey square but under the one on the right... I want the red one to be on top of the two others.

Is there anyway I can achieve that? I tried myImageView.BringToFront() but it breaks the linear layout, bringing the red square to the far right.

NOTE: It HAS to be a linear layout, I know how to do it with a relative layout but I have other constraints that makes me need a linear layout...

PS: If this is not possible at all, is there anyway I could achieve in a relative layout what "weights" do in linear layouts? (I want my 4 squares to upscale equally so that they fill the width of the screen of the user. I can make that happen in a linear layout but it brings me back to my 1st question...)

mmohab
  • 2,303
  • 4
  • 27
  • 43
François Guthmann
  • 461
  • 1
  • 4
  • 15
  • You should change the z-order. Check also this developer.android.com/reference/android/view/View.html#bringToFront This is of' course if you don't use Canvas. – Mike Argyriou May 28 '14 at 16:03
  • The z-order is the order in which they are drawn. In a linear layout, the first element is drawn, then the second, etc.; so that is why the third element is being drawn under the 4th. A linear layout does this because it calculates the size of the first item, then the second, and so on; so bringToFront is changing this order around and that is why it is hosing up your layout. What type of animation are you using and animation class? Are you sure you can't use RelativeLayout or maybe a FrameLayout? Is it the layout weights you need, because there are ways around that esp for 4 child views. – Chris Feist May 28 '14 at 17:00
  • @Chris: Thanks for the explanations about z-order. About the animations, I don't know yet. This one for example: `RotateAnimation anim = new RotateAnimation(0f, 350f,125f, 125f); anim.setInterpolator(new LinearInterpolator()); anim.setDuration(1400);` What I need is both the weights and the animation to play over/in front of / above everything. If you can tell me how to use weights in a relative layout that would solve my problem. (It adds some work but it should be ok) – François Guthmann May 28 '14 at 17:22
  • Can't I just redraw the specific imageView btw? If I don't redraw the whole layout it should do it right? Oh and I have 16 child views in my project if it is of any help to find the best solution to achieve weights in relative layout. – François Guthmann May 28 '14 at 17:56

1 Answers1

1

I think the easiest thing to do would be to create your own subview of whatever your child views are using, I'm guessing you use ImageView. Then add getter and setter methods for layout weight. Something like this:

public float getLayoutWeight()
{
    ViewGroup.LayoutParams layoutParams = getLayoutParams();
    if (layoutParams instanceof LinearLayout.LayoutParams)
    {
        return ((LinearLayout.LayoutParams)layoutParams).weight;
    }

    return -1;
}

public void setLayoutWeight(float weight)
{
    ViewGroup.LayoutParams layoutParams = getLayoutParams();
    if (layoutParams instanceof LinearLayout.LayoutParams)
    {
        ((LinearLayout.LayoutParams)layoutParams).weight = weight;
        requestLayout();
        invalidate();
    }
}

Then use an ObjectAnimator to animate the weight. See the example here. You would do something like this:

ObjectAnimator anim = ObjectAnimator.ofFloat(redSquareView, "layoutWeight", 1f, 2f);
anim.setDuration(1000);
anim.start();

NOTE: I don't know for certain if this will work and I haven't test it; but based on what you said, its the least amount of code. Also, this class is only available for API 11 and newer.

Chris Feist
  • 1,678
  • 15
  • 17
  • This sounds complicated. I'm fairly new to android and didn't understand half of this answer. I was really looking for a simple line I was missing. I guess I can't have the advantages of both linear and relative layouts... Thank you for your time anyway, I might try this if I manage to understand it all. I'll wait a few days before marking your answer as accepted, to see if anyone has another solution. I doubt it. – François Guthmann May 29 '14 at 20:33
  • Sorry for any confusion. I meant create a subclass of the view you are using for your squares. So if you were using an `ImageView` make `MrGoodKillsImageView` with all 3 constructors (calling the corresponding `super(args...)` in each. Then implement the 2 methods I gave you. The `ObjectAnimator` class uses whatever string variable thats passed in as the 2nd param, and calls the getter and setter methods (converted to camel case) to "animate" that property. – Chris Feist May 29 '14 at 22:13
  • I didn't mean your explanations weren't clear, I meant I'm really new at android and java, what makes it kinda hard for me. But from what I understand you thought I needed to animate the weights. I don't actually. I'm trying to create a 2048 like. So, what I want is my grid of grey squares to extend to the edges of the screen. (I want it to get as wide as possible, the app being only in portrait mode)I can do that with a linear layout. But then I wanted to animate my grey squares and I have the problem describe above... – François Guthmann Jun 01 '14 at 10:34
  • Now, I think i managed to create a subview from this question: http://stackoverflow.com/questions/1470867/creating-custom-imageview. I guess I must modify the onDraw function but I really don't know what to put in it. (As I mentionned earlier, I thought there would be a simple line to add, I don't expect you to explain to me how java works ^^. Anyway, still appreciate the help :)) – François Guthmann Jun 01 '14 at 10:39