Android 以编程方式调整布局大小(作为动画)
我想调整活动中的一些布局 以下是主XML的代码:Android 以编程方式调整布局大小(作为动画),android,layout,animation,android-animation,Android,Layout,Animation,Android Animation,我想调整活动中的一些布局 以下是主XML的代码: <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <LinearLayout android:id="@+id/top"
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#3ee3e3" >
</LinearLayout>
<LinearLayout
android:id="@+id/middle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#fe51e6" >
</LinearLayout>
</LinearLayout>
如您所见,顶部和底部布局高度为0,中间为0
布局覆盖了所有的地方
我想通过编程减少中间布局的大小,同时增加顶部和底部的大小
和底部布局尺寸,直到所有布局具有相同的高度
我希望它看起来像一个动画
我该怎么做
谢谢我为类似的目的写了一个调整大小的动画。这很简单,但成本很高 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()
}
}
在Honeycomb(Android 3.0)上,有Animator
和ObjectAnimator
类用于更平滑的动画
读一下
有关如何使用反弹插值器设置视图组(LinearLayout)移动动画的示例
BounceInterpolator bounceInterpolator = new BounceInterpolator();
ObjectAnimator anim = ObjectAnimator.ofFloat(myViewGroup, "translationY", 0f, -200 );
anim.setInterpolator(bounceInterpolator);
anim.setDuration(1100).start();
这将触发一个带有反弹效果的平滑动画,并真正移动视图,而不是像蜂窝之前的动画那样。从文档中:
以前的动画更改了目标对象的视觉外观。。。但是他们实际上并没有改变对象本身
你也可以用谷歌新的Spring动画调整大小 SpringAnimation创建方法:
fun getSpringAnimation(view: View, springAnimationType: FloatPropertyCompat<View>, finalPosition: Float): SpringAnimation {
val animation = SpringAnimation(view, springAnimationType )
// create a spring with desired parameters
val spring = SpringForce()
spring.finalPosition = finalPosition
spring.stiffness = SpringForce.STIFFNESS_VERY_LOW // optional
spring.dampingRatio = SpringForce.DAMPING_RATIO_NO_BOUNCY // optional
// set your animation's spring
animation.spring = spring
return animation
}
你能在这些对象@ddcoy上发布一些代码示例或更多信息吗?我想你也需要覆盖
public void initialize
&public boolean willChangeBounds
。@faylon你为什么说它很昂贵?它会消耗更多的内存还是需要更多的时间来执行?@NiteshKhatri它需要大量的cpu,所以在低端手机上动画不会太流畅,尤其是当布局本身已经很复杂的时候。我认为我们已经有了更好的方法在新的android SDK中实现这些动画。@faylon I支持2.3(API 9)及以上版本。还有其他更有效的方法来实现这个动画吗?我会很感激的。@NiteshKhatri在过去的两年里我没有在Android上工作过,现在没有时间深入研究这个问题。很抱歉。
getSpringAnimation(view, SpringAnimation.SCALE_X, 0.8f).start()
getSpringAnimation(view, SpringAnimation.SCALE_Y, 0.8f).start()