[英]Animate android view's height
我的活动布局如下。 最初将“ heightView's”的权重设置为100。然后在启动后4秒钟,我希望将heightView的高度动画化为51,以便可以在内部的“ LinearLayout”中产生悬吊效果。
我对android非常陌生。
我的布局XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/main_bg"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="bottom"
android:weightSum="100">
<View
android:id="@+id/heightView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="100" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="41"
android:orientation="horizontal"
android:background="@drawable/menu_repeat"
android:tileMode="repeat" >
<ImageButton
android:id="@+id/contactBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/contact" />
<ImageButton
android:id="@+id/events"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/events" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
我的Java代码(无法正常工作)
setContentView(R.layout.activity_main);
View myView=(View) findViewById(R.id.heightView);
ObjectAnimator scaleYOut = ObjectAnimator.ofFloat(myView, "weight", 50, 0f);
AnimatorSet set = new AnimatorSet();
set.play(scaleYOut);
set.setDuration(1000);
set.start();
请事先帮助我
试试下面的代码:
它可能无法完全满足您的需求。 但是它绝对可以帮助您根据需要进行修改。
main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="bottom"
android:weightSum="100">
<View
android:id="@+id/heightView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:background="#80009977"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="60"
android:orientation="horizontal"
android:background="@drawable/ic_launcher"
android:tileMode="repeat" >
<ImageButton
android:id="@+id/contactBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/events"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
主要活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View myView=(View) findViewById(R.id.heightView);
ObjectAnimator scaleXIn = ObjectAnimator.ofFloat(myView, "scaleX", 0f, 1f);
ObjectAnimator scaleYIn = ObjectAnimator.ofFloat(myView, "scaleY", 0f, 1f);
AnimatorSet set = new AnimatorSet();
set.play(scaleXIn).with(scaleYIn);
set.setDuration(2000);
set.start();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.