Resizing layouts programmatically (as animation)
I wrote a ResizeAnimation for a similar purpose. It's simple but costly.
Java
/**
* an animation for resizing the view.
*/
public class ResizeAnimation extends Animation {
private View mView;
private float mToHeight;
private float mFromHeight;
private float mToWidth;
private float mFromWidth;
public ResizeAnimation(View v, float fromWidth, float fromHeight, float toWidth, float toHeight) {
mToHeight = toHeight;
mToWidth = toWidth;
mFromHeight = fromHeight;
mFromWidth = fromWidth;
mView = v;
setDuration(300);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float height =
(mToHeight - mFromHeight) * interpolatedTime + mFromHeight;
float width = (mToWidth - mFromWidth) * interpolatedTime + mFromWidth;
LayoutParams p = mView.getLayoutParams();
p.height = (int) height;
p.width = (int) width;
mView.requestLayout();
}
}
Kotlin
class ResizeAnimation(
private val view: View,
private val toHeight: Float,
private val fromHeight: Float,
private val toWidth: Float,
private val fromWidth: Float,
duration: Long
) : Animation() {
init {
this.duration = duration
}
override fun applyTransformation(
interpolatedTime: Float,
t: Transformation?
) {
val height = (toHeight - fromHeight) * interpolatedTime + fromHeight
val width = (toWidth - fromWidth) * interpolatedTime + fromWidth
val layoutParams = view.layoutParams
layoutParams.height = height.toInt()
layoutParams.width = width.toInt()
view.requestLayout()
}
}
Resizing layouts programatically (as animation) up to wrap_content
Not sure if this is exactly what you're looking for, but you could define the layout_ width and height as WRAP_CONTENT in the xml, measure it in your constructor or onCreate, and then resize the layout to your desired size. Holding on to those values, you could return it to the WRAP_CONTENT size in your animation.
Android - LayoutChange Animation for resize a View
You can use ValueAnimator and then add update listener where you change your layout parameters programaticly.
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int value = (Integer) valueAnimator.getAnimatedValue();
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = value;
animatedView.setLayoutParams(lp);
}
});
mAnimator.start();
How to animate the width and height of a Layout?
Sure that is possible.
Simply 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. Of course, animating the width is also possible.
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;
}
/**
* set the duration for the hideshowanimation
*/
@Override
public void setDuration(long durationMillis) {
super.setDuration(durationMillis);
}
@Override
public boolean willChangeBounds() {
return true;
}
}
In code, create a new Animation and apply it to the RelativeLayout that you want to animate:
RelativeLayout relativeLayout = (RelativeLayout) ((LinearLayout) view.findViewById(viewId)).getParent();
// getting the layoutparams might differ in your application, it depends on the parent layout
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
ResizeAnimation a = new ResizeAnimation(relativeLayout);
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);
relativeLayout.startAnimation(a);
How to expand a layout height with animation?
Please check below new edited answer as below. But here you need to know the exact new height.
public class LayoutAnimationActivity extends Activity {
RelativeLayout ril1;
Button btn;
int initialHeight;
int actualHeight;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
ril1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
btn = new Button(this);
btn.setWidth(100);
btn.setHeight(200);
btn.setText("Button");
actualHeight = 210;
Ani a = new Ani();
a.setDuration(2000);
ril1.startAnimation(a);
}
class Ani extends Animation {
public Ani() {}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight;
newHeight = (int) (initialHeight * interpolatedTime);
ril1.removeAllViews();
btn.setWidth(100);
btn.setHeight(300);
btn.setText("as");
ril1.addView(btn);
ril1.getLayoutParams().height = newHeight;
ril1.requestLayout();
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
initialHeight = actualHeight;
}
@Override
public boolean willChangeBounds() {
return true;
}
};
}
Animate resizing of ImageView to size of another ImageView
create a float value for the ratios between the two images:
float w_ratio = (targetImage.getMeasuredWidth() - sourceImage.getMeasuredWidth()) / sourceImage.getMeasuredWidth();
float h_ratio = (targetImage.getMeasuredHeight() - sourceImage.getMeasuredHeight()) / sourceImage.getMeasuredHeight();
// then animate:
v.animate().scaleXBy(w_ratio).scaleYBy(h_ratio).setDuration(1000);
// Don't forget to reset the size later if needed, using:
v.setScaleX(1.0f);
v.setScaleY(1.0f);
Related Topics
How to Use a Compound Drawable Instead of a Linearlayout That Contains an Imageview and a Textview
Android Compass That Can Compensate for Tilt and Pitch
How to Force the Action Bar to Be at the Bottom in Ics
How to Add to My Android Application a Button Than Do Like to a Facebook Page
How to Use Adb in Android Studio to View an SQLite Db
Onrequestpermissionsresult Not Being Called in Fragment If Defined in Both Fragment and Activity
Differencebetween Android Margin Start/End and Right/Left
Uploading a Large File in Multipart Using Okhttp
Sniffing/Logging Your Own Android Bluetooth Traffic
How to Change a Tab Background Color When Using Tablayout
Android: Wrap_Content Is Not Working with Listview
Android Broadcast Receiver Bluetooth Events Catching
Activity Declaration in Androidmanifest.Xml