android - 动画android View 的高度

标签 android android-layout android-linearlayout android-animation

我的 Activity 布局如下。最初将“heightView”的权重设置为 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();

请帮我提前谢谢

最佳答案

试试下面的代码:

它可能无法准确地满足您的需求。但它绝对可以帮助您根据您的要求进行修改。

ma​​in.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>

主要 Activity :

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();
   }

关于android - 动画android View 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17017612/

相关文章:

android - 连字符将一行分成多行

android - 为什么 Kotlin 不允许主构造函数中有任何代码?

android - Google Glass GDK 是否支持 Intent.ACTION_CALL?

java - 我可以识别所有连接到 wifi 网络的 android 的主机名吗?

android - 由于自定义 View ,layout_below 不起作用

Android 按屏幕大小和密度过滤资源

android - 如何使用 Material 和 AndroidX 组件将折叠工具栏与 TabLayout 一起使用?

android - 在android中将TextView设置到垂直LinearLayout的右侧

android - LinearLayout 不渲染

具有四个 Listview 的 Android 线性布局不会在 Scrollview 内滚动