How to use fade in and fade out effect with an ima

2020-02-14 07:03发布

问题:

I have seen in Android mobile. When i click photo gallery, one image is opened (Fade in) and after I clicked disappeared (Fade out).

Same, I have done in my app. I have pasted one image in Drawable. And applied Fade in and Fade out conditions. But I have not seen any images than sky blue color background. That is view.

How could I do this programmatically in Android?

In what way can I fix this problem? What mistake have I done here?

My code is:

btn=(Button)findViewById(R.id.Click);
   viewToAnimate=(View)findViewById(R.id.view1);
   btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {


        if(viewToAnimate.getVisibility()==View.VISIBLE)
        {
            boolean toRight = false;
            Context c = null;
            //Animation out=AnimationUtils.makeOutAnimation(this,true);
            Animation out=AnimationUtils.makeOutAnimation(c, toRight);
            viewToAnimate.startAnimation(out);
             viewToAnimate.setVisibility(View.INVISIBLE);

        } else {
          int id = 0;
        Context c = null;
        //  Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
           Animation in=AnimationUtils.loadAnimation(c, id);

            viewToAnimate.startAnimation(in);
            viewToAnimate.setVisibility(View.VISIBLE);
        }
    }
} );



} }

Then in Main.xml :

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/fade" />


<View
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#AAA" 
    android:src="@drawable/fade"/>

回答1:

for fadeIn.xml

 <?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="2000"/>
     </set>

for fadeOut.xml

 <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="2000"/>
     </set>

try using this animation in your image view..



回答2:

This works for me:

Fade in-

<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="1000" />
</set>

Fade out-

<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="1000" />
</set>