Add Fade-in effect to imageview image [Android] - java

Guys I need help to understand, how to add some animation to static imageview. My project contains this:
layout/drawer_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nav_header_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/launcher_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:visibility="invisible"
android:src="#mipmap/ic_launcher" />
<ImageView
android:id="#+id/drawer_icon"
android:flipInterval="3000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="245dp"
android:scaleType="centerCrop"
android:src="#drawable/vert_loading" />
</RelativeLayout>
anim/fadein.xml
<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"/>
anim/fadeout.xml
<?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="2000"/>
</set>
The question is: How to fadein and fadeout image automaticaly:
android:src="#drawable/vert_loading"
Add something to MainActiviti.java?

In you java code you need to startAnimation & to repeat one after another you can listen using setAnimationListener. When onAnimationEnd is called for one you can call another startAnimation.
Following is the example for your use case:
//find imageview
ImageView image = findViewById(R.id.drawer_icon);
// get Animation objcets
Animation fadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
Animation fadeOut = AnimationUtils.loadAnimation(this, R.anim.fadeout);
//set the initial animation
image.startAnimation(fadeIn);
//set fadeIn listener
fadeIn.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//when fadeIn ends start the fadeout
image.startAnimation(fadeOut);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
//set fadeout listener
fadeOut.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//when fadeOut ends start the fadeIn
image.startAnimation(fadeIn);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Related

How to make ImageView partially visible and fade in completely after?

I am currently making an application where I want part of the logo of the application to appear at the center of the screen, translates to the top and then shows the entire logo. I currently have the application showing the entire logo and translating it as desired, but I am not sure how to hide most of the logo and make it show after. My logo consists of an icon on the left and text on the right. Once the logo reaches the top, I would like the name to appear smoothly while pushing the icon to the left as it goes. Is there a way to do this using the current code I have.
logo_animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="800"
android:fromAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
<translate
android:duration="800"
android:fillAfter="true"
android:fromYDelta="0%p"
android:startOffset="2000"
android:toYDelta="-30%p" />
</set>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingStart="15dp"
android:paddingRight="15dp"
android:paddingEnd="15dp"
android:background="#color/colorPrimary"
tools:context=".Main">
<ImageView
android:id="#+id/mainLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="#string/logo"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>
Main.java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
public class Main extends AppCompatActivity implements AnimationListener {
ImageView mainLogo;
Animation mainLogoAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
searchLogo = (ImageView) findViewById(R.id.searchLogo);
mainLogoAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.logo_animation);
mainLogoAnimation.setAnimationListener(this);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
}
Just put the text in a separate TextView, and animate it on its own.

Move imageview after animation (update position)

I am trying to to do a translate animation on an image view from the bottom to the middle of the screen. Upon finish of the animation, I want the image view to stay there. I dont want the setFillAfter(true) because I want the actual position of the imageview to be updated.
I do it currently by having 2 image view (one at the start of animation and one at the end) and I play with the setVisibility to achieve this. Is this the correct way to do things? Here is the code I used:
<ImageView
android:id="#+id/ivStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/typer_step_1"
android:gravity="center"
/>
<ImageView
android:id="#+id/ivMiddle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/typer_step_1"
android:gravity="center"
android:visibility="invisible"
/>
TranslateAnimation translate = new TranslateAnimation(0, mDestLoc1[0]-mSrcLoc1[0], 0, mDestLoc1[1]-mSrcLoc1[1]);
translate.setDuration(2000);
translate.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
ivMiddle.setVisibility(View.VISIBLE)
ivStart.setVisibility(View.INVISIBLE)
}
#Override
public void onAnimationRepeat(Animation animation) {}
});
ivStart.startAnimation(translate);
Then you must set new LayoutParams for your View which is animating. When animation finishes, in your onAnimationEnd part, set new position of your View.
TranslateAnimation translate = new TranslateAnimation(0, mDestLoc1[0]-mSrcLoc1[0], 0, mDestLoc1[1]-mSrcLoc1[1]);
translate.setDuration(2000);
translate.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation) {
RelativeLayout.LayoutParams par = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
par.topMargin = mDestLoc1[1]-mSrcLoc1[1];
par.leftMargin = mDestLoc1[0]-mSrcLoc1[0];
view.setLayoutParams(par);
}
#Override
public void onAnimationRepeat(Animation animation) {}
});
view.startAnimation(translate);
Snake use my code this ll help you,
//Call this in your onCreate
private void StartAnimationsDtoU()
{
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alphadtou);
anim.reset();
RelativeLayout l=(RelativeLayout) findViewById(R.id.lin_lay);
l.clearAnimation();
l.startAnimation(anim);
anim = AnimationUtils.loadAnimation(this, R.anim.translate);
anim.reset();
ImageView iv = (ImageView) findViewById(R.id.logo);
iv.setImageResource(R.drawable.earth);
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
iv.clearAnimation();
iv.startAnimation(anim);
}
This is your linear layout with an image which ll move from down to middle of the screen.
<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:id="#+id/lin_lay">
<ImageView
android:id="#+id/logo"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/earth" />
and this ll be your xml file for translation i.e translate.xml ...
<set xmlns:android="http://schemas.android.com/apk/res/android"><translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="200%"
android:toYDelta="0%"
android:duration="4000"
android:zAdjustment="top" /></set>
and this ll be your down to up alphadtou.xml...
<?xml version="1.0" encoding="utf-8"?><alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000" />
and also override onAttachedToWindow method like this...
#Override
public void onAttachedToWindow()
{
// TODO Auto-generated method stub
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
Hope this ll help you alot.
If you want the actual starting position of the image view you can get it at the time of animationStart And you can use setFillAfter(true). The setFillAfter(true) will update the position after the animation end.
If you need the new position you can use the setFilter(true). If you are not ready to use setFillAfter(true) (if you need the older position), then you have done the right thing by having two Image Views. But its better to get the position in animationStart and use setFillAfter(true).
Add this line before starting the animation
translate.setFillAfter(true);

How to add a View above the display Android

I want to create a translate animation which will pop up a View from the top of the screen. Now I need to put my view above the screen , so that it will be invisible . The root of all Views is a vertical LinearLayout . This is how I inflate the RelativeLayout (which is going to be animated) into my LinearLayout.
final LinearLayout root = (LinearLayout) findViewById (R.id.mainLayout);
final RelativeLayout container = (RelativeLayout)
LayoutInflater.from(getBaseContext()).
inflate(R.layout.animated_layout ,
root , false );
root.addView(container);
and this is the Relativelayout xml .
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#drawable/list_item_back1" >
<TextView
android:textSize="25sp"
android:layout_width = "match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Ass you said, become your view invisible. Then, create an XML translate animation:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<translate
android:fromYDelta="100%"
android:toYDelta="0%"
android:duration="1000" />
</set>
In your java code do something like this:
Animation translateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate_animation);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
yourAnimatedView.setvisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
yourAnimatedView.startAnimation(translateAnimation);
});

Difficulties fading out and hiding Relative Layout

I'm trying to do a simple fade-out animation for my "custom" media controller.
The problem I'm having is that the layout does not fade-out, it just disappears after a second (as if I would set the Visibility to GONE with a 1 second delay).
This is my function:
private void fadeOut(final View view) {
final AlphaAnimation fadeOutAnimation = new AlphaAnimation(1.0F, 0.0F);
fadeOutAnimation.setDuration(1000);
fadeOutAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
view.setVisibility(View.GONE);
}
});
view.startAnimation(fadeOutAnimation);
}
And the layout that I'm trying to hide:
<RelativeLayout
android:id="#+id/videoControllerContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0dp"
android:alpha="50"
android:background="#color/DarkGrey" >
<ImageButton
android:id="#+id/playVideoButton"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:background="#null"
android:contentDescription="#string/playButton"
android:scaleType="fitCenter"
android:src="#drawable/play" />
<ImageButton
android:id="#+id/fullScreenVideoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:background="#null"
android:contentDescription="#string/fullscreen"
android:src="#drawable/fullscreen" />
</RelativeLayout>
It's worth mentioning that all this is being used inside a fragment.
I'm hoping that someone with a bit more experience will take the time and let me know what I'm doing wrong.
I think you should have this in an anim xml file where you would want this:
<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="1000"/>
</set>
I think your problem is that the interpolator isn't defined. So try to put this line's equivalent in your code:
android:interpolator="#android:anim/accelerate_interpolator"
Hope it helps
EDIT: did a little research, it looks like you want to say
fadeOutAnimation.setInterpolator(new AccelerateInterpolator());

Fade effect between layouts

As by object, I would reproduce fade effect between two layout.
Now I've this situation:
LinearLayout l;
LinearLayout l2;
To switch between them I've used
l.setVisibility(View.GONE);
l2.setVisibility(View.VISIBLE);
I want add fade effect between this transiction, how I can do it?
Here is a working solution to cross fade between 2 layouts:
public class CrossFadeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crossfade);
final View l1 = findViewById(R.id.l1);
final View l2 = findViewById(R.id.l2);
final Animation fadeOut = AnimationUtils.loadAnimation(CrossFadeActivity.this, R.anim.fade_out);
final Animation fadeIn = AnimationUtils.loadAnimation(CrossFadeActivity.this, R.anim.fade_in);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
fadeOut.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
l1.setVisibility(View.GONE);
}
});
l1.startAnimation(fadeOut);
l2.setVisibility(View.VISIBLE);
l2.startAnimation(fadeIn);
}
});
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
fadeOut.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
l2.setVisibility(View.GONE);
}
});
l2.startAnimation(fadeOut);
l1.setVisibility(View.VISIBLE);
l1.startAnimation(fadeIn);
}
});
}
}
crossfade.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/l1"
android:layout_width="fill_parent"
android:layout_height="300dip"
android:orientation="vertical"
android:background="#drawable/someimage"
>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<RelativeLayout
android:id="#+id/l2"
android:layout_width="fill_parent"
android:layout_height="300dip"
android:orientation="vertical"
android:background="#drawable/someimage2"
android:visibility="gone"
>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</RelativeLayout>
Where l1 and l2 are 2 random example layouts. The trick is to put them in XML such that they overlap each other (e.g. in a RelativeLayout) with visible / gone, add listeners to the animations to toggle the visibility on finish, and set the view which has to fade in to visible before the animation starts, otherwise the animation will not be visible.
I put the buttons with the listeners to toggle the animation in the layouts itself, because I need to implement it that way but the click listener can be of course somewhere else (if it's only one it should be used in combination with some flag or check to know how to toggle).
These are the animation files. They have to be stored in folder res/anim:
fade_in.xml
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
fade_out.xml
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromAlpha="1.0"
android:toAlpha="0" />
UPDATE:
Instead of using R.anim.fade_in, you can use the default fade_in from Android API (android.R.fade_in):
final Animation fadeIn = AnimationUtils.loadAnimation(CrossFadeActivity.this, android.R.anim.fade_in);
Using android.R.anim.fade_in, you will not need to create the file res/anim/fade_in.xml.
Android has a package with some useful animations on android.R.anim: http://developer.android.com/reference/android/R.anim.html
Using R.anim.fade_out & .R.anim.fade_in you can create an animation which does this. I don't know much about this myself but heres a tutorial regarding animations in android: Animation Tutorial
P.S. This tutorial is not mine thus credit does not go out to me.
Edit:
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(100);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
l.setLayoutAnimation(controller);
Fade out Animation
public static Animation runFadeOutAnimationOn(Activity ctx, View target) {
Animation animation = AnimationUtils.loadAnimation(ctx,android.R.anim.fade_out);
target.startAnimation(animation);
return animation;
}
I'm guessing you can try something like this, I copy pasted the animation from the tutorial I don't know what it does exactly as I have no experience with Android development. Another example could be Example 2
Since android 3.0 you can use
android:animateLayoutChanges="true"
in a layout in xml and any changes to this specific layouts content at run time will be animated. For example, in your case, you will need to set this attribute for the parent for the parent layout of l and l2 e.g
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="me.kalem.android.RegistrationActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:padding="20dp">
<LinearLayout
android:id="#+id/l1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible">
........
</LinearLayout>
<LinearLayout
android:id="#+id/l2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone">
........
</LinearLayout>
</LinearLayout>
Now hiding l1 and showing l2 at runtime will be animated.

Categories