【问题标题】:Animate view on button click android按钮上的动画视图单击android
【发布时间】:2014-12-17 20:25:42
【问题描述】:

我有一个按钮,在那个按钮上我想在下面滑动一个视图。那个视图包含 2 个按钮来打开相机和画廊。为了实现这一点,我使用了翻译动画,但我没有得到想要的结果。我只想要那个视图的大部分内容在向下滑动时可见。

动画代码

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="-10%"
        android:toYDelta="0%"
        android:duration="1000" />
</set>

代码

public class Slide extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slide);
        Button bt = (Button) findViewById(R.id.button);
        final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.test);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation slide = AnimationUtils.loadAnimation(Slide.this, R.anim.top_slide);
//
                linearLayout.startAnimation(slide);
                linearLayout.setVisibility(View.VISIBLE);
            }
        });
    }
}

图片

我希望幻灯片应该从 + 图像下方发生,但它没有发生

XML 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/test"
        android:visibility="gone"
        android:layout_gravity="center_horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/butt3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/bton3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/butn3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/cb" />

    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android animation


    【解决方案1】:

    你不需要幻灯片动画,你真正想要的是对象动画

    代码

        Button bt = (Button) findViewById (R.id.button);
        final LinearLayout linearLayout = (LinearLayout) findViewById (R.id.test);
        bt.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View v) {
    
                ValueAnimator anim = ValueAnimator.ofInt(linearLayout.getMeasuredHeight (), 500);
                anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        int val = (Integer) valueAnimator.getAnimatedValue();
                        ViewGroup.LayoutParams layoutParams = linearLayout.getLayoutParams();
                        layoutParams.height = val;
                        linearLayout.setLayoutParams(layoutParams);
                    }
                });
                anim.setDuration(1000);
                anim.start(); 
            }
        });
    

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_gravity="center_horizontal" />
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/test"
    
            android:background="#fff333"
            android:layout_gravity="center_horizontal">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button2" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button3" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/butt3" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/bton3" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/butn3" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/cb" />
    
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多