简体   繁体   English

Android动画淡入/淡出未在回调函数中设置视图的可见性

[英]Android animation fade in / fade out is not setting the visibility of the view in the callback function

I have android view that I want to fadeIn and Fadeout when users click on a button. 当用户单击按钮时,我具有要淡入和淡出的android视图。 The animation itself is being called and the callback is being called but when the animation triggers the view cannot be seen. 动画本身正在被调用,回调函数正在被调用,但是当动画触发时,将无法看到该视图。 I think the problem is with the xml layout, I have been stuck on this for hours if someone can help it would be greatly appreciated. 我认为问题出在xml布局上,如果有人可以帮助我,我会坚持几个小时,将不胜感激。

My Main Layout: - Just view in question 我的主要布局:-只是有问题的视图

<LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="left|top"
        android:background="@color/whiteOverlay"
        android:gravity="center_vertical|center_horizontal"
        android:id="@+id/uploadOptions"
        android:alpha="0"
        android:visibility="gone">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="40dp"
            android:paddingRight="40dp">

            <Button
                style = "@style/basic_button"
                android:text="@string/selectFromGallery"
                android:id="@+id/gallery_select"
                android:layout_marginTop="10dp"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

            <Button
                style = "@style/basic_button"
                android:text="@string/selectFromCamera"
                android:id="@+id/camera_select"
                android:layout_marginTop="10dp"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </LinearLayout>
    </LinearLayout>

FadeIn 淡入

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="500" />
</set>

FadeOut 淡出

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="500" />
</set>

Activity Java - 活动Java-

public class regPP extends Activity {
    private String lang = "";
    private Boolean optionsActive = false;
    private LinearLayout options = null;


    private void fade (final View view, final Boolean Toggle){
        Animation anim;
        view.setVisibility(View.VISIBLE);
        if(Toggle){
            //fadeOut
            anim = AnimationUtils.loadAnimation(regPP.this, R.anim.fadeout);
            Log.d("FADE","FadeOut");
        } else {
            //fadeIn
            anim = AnimationUtils.loadAnimation(regPP.this, R.anim.fadein);
            Log.d("FADE","FadeIn");
        }
        view.startAnimation(anim);
        anim.setAnimationListener(new Animation.AnimationListener(){
            @Override
            public void onAnimationStart(Animation animation) {

            }
            @Override
            public void onAnimationEnd(Animation arg0) {
                //Functionality here
                if(Toggle){
                    view.setVisibility(View.GONE);
                    Log.d("FADE", "FadeOut Callback");
                } else {
                    view.setVisibility(View.VISIBLE);
                    Log.d("FADE", "FadeIn Callback");
                }
            }
            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.lang = getResources().getString(R.string.lang);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        final Context context = regPP.this;

        setContentView(R.layout.reg_pp);

        ImageButton uploadTog = (ImageButton) findViewById(R.id.uploadTog);
        options = (LinearLayout) findViewById(R.id.uploadOptions);

        uploadTog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fade(options,false);
                optionsActive = true;
            }
        });

    }
    @Override
    public void onBackPressed() {
        if(optionsActive){
            fade(options,true);
            optionsActive = false;
        } else {
            super.onBackPressed();
        }
    }
}

remove this line may help u 删除此行可能对您有帮助

android:visibility="gone" 机器人:能见度=“水涨船高”

visibility is gone that's why your view is not visible 可见性消失了 ,这就是为什么您的视图不可见的原因

    android:visibility="gone"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM