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

时间:2015-07-11 10:20:34

标签: java android animation android-animation android-view

我有android视图,当用户点击按钮时我想要fadeIn和Fadeout。正在调用动画本身并且正在调用回调,但是当动画触发时,无法看到视图。我认为问题出在xml布局上,如果有人可以提供帮助,我已经坚持了几个小时。非常感谢。

我的主要布局: - 只是查看问题

<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>

淡入

<?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>

淡出

<?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>

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

1 个答案:

答案 0 :(得分:2)

删除此行可能会有所帮助

<强>机器人:能见度=&#34;去&#34;

可见性消失这就是为什么您的视图不可见

    android:visibility="gone"