Android Adding Simple Animations While Setvisibility(View.Gone)

Android adding simple animations while setvisibility(view.Gone)

You can do two things to add animations, first you can let android animate layout changes for you. That way every time you change something in the layout like changing view visibility or view positions android will automatically create fade/transition animations. To use that set

android:animateLayoutChanges="true"

on the root node in your layout.

Your second option would be to manually add animations. For this I suggest you use the new animation API introduced in Android 3.0 (Honeycomb). I can give you a few examples:

This fades out a View:

view.animate().alpha(0.0f);

This fades it back in:

view.animate().alpha(1.0f);

This moves a View down by its height:

view.animate().translationY(view.getHeight());

This returns the View to its starting position after it has been moved somewhere else:

view.animate().translationY(0);

You can also use setDuration() to set the duration of the animation. For example this fades out a View over a period of 2 seconds:

view.animate().alpha(0.0f).setDuration(2000);

And you can combine as many animations as you like, for example this fades out a View and moves it down at the same time over a period of 0.3 seconds:

view.animate()
.translationY(view.getHeight())
.alpha(0.0f)
.setDuration(300);

And you can also assign a listener to the animation and react to all kinds of events. Like when the animation starts, when it ends or repeats etc. By using the abstract class AnimatorListenerAdapter you don't have to implement all callbacks of AnimatorListener at once but only those you need. This makes the code more readable. For example the following code fades out a View moves it down by its height over a period of 0.3 seconds (300 milliseconds) and when the animation is done its visibility is set to View.GONE.

view.animate()
.translationY(view.getHeight())
.alpha(0.0f)
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
}
});

Animate visibility of a view from gone to visible with animation

You can do this using XML animation.

Create a slide-up animation XML using set and alpha and put this XML into your resource anim folder.

slide_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="500"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>

USE:

Use AnimationUtils.loadAnimation() to load animation from XML and set and start animation using .startAnimation() method.

Here is an example:

ImageView imageView = (ImageView) findViewById(R.id.imageView);

// slide-up animation
Animation slideUp = AnimationUtils.loadAnimation(this, R.anim.slide_up);

if (imageView.getVisibility() == View.INVISIBLE) {
imageView.setVisibility(View.VISIBLE);
imageView.startAnimation(slideUp);
}

Hope this will help~

How do I Animate a view from View.GONE to View.VISIBLE in ANDROID

Add android:animateLayoutChanges="true" to your xml file and if you want fade out use view.animate().alpha(0.0f); in your java class and if you want fade in use view.animate().alpha(1.0f); .

Show and hide a View with a slide up/down animation

With the new animation API that was introduced in Android 3.0 (Honeycomb) it is very simple to create such animations.

Sliding a View down by a distance:

view.animate().translationY(distance);

You can later slide the View back to its original position like this:

view.animate().translationY(0);

You can also easily combine multiple animations. The following animation will slide a View down by its height and fade it in at the same time:

// Prepare the View for the animation
view.setVisibility(View.VISIBLE);
view.setAlpha(0.0f);

// Start the animation
view.animate()
.translationY(view.getHeight())
.alpha(1.0f)
.setListener(null);

You can then fade the View back out and slide it back to its original position. We also set an AnimatorListener so we can set the visibility of the View back to GONE once the animation is finished:

view.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
}
});

Set visibility of view GONE after animation ends

My mistake was adding android:animateLayoutChanges="true" in the root of layout. It animates my view again!

Add animation when hide LinearLayout (View.GONE) and show LinearLayout (View.VISIBLE)

I found a solution to the problem. Full lesson and source code can be downloaded here: click here

Or use the code below:

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:background="#FCF"
android:orientation="horizontal" >

<TextView
android:id="@+id/color"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/rectangle"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical"
android:text=""
android:textAlignment="center"
android:textStyle="bold" />

<TextView
android:id="@+id/clickme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical"
android:text="click_here"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:id="@+id/expandable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="#FFF"
android:orientation="vertical"
android:paddingLeft="4dp" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Слуга: text3" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text4" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text5" />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

LinearLayout mLinearLayout;
LinearLayout mLinearLayoutHeader;
ValueAnimator mAnimator;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setTitle("title");

mLinearLayout = (LinearLayout) findViewById(R.id.expandable);
// mLinearLayout.setVisibility(View.GONE);
mLinearLayoutHeader = (LinearLayout) findViewById(R.id.header);

// Add onPreDrawListener
mLinearLayout.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {

@Override
public boolean onPreDraw() {
mLinearLayout.getViewTreeObserver()
.removeOnPreDrawListener(this);
mLinearLayout.setVisibility(View.GONE);

final int widthSpec = View.MeasureSpec.makeMeasureSpec(
0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec
.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
mLinearLayout.measure(widthSpec, heightSpec);

mAnimator = slideAnimator(0,
mLinearLayout.getMeasuredHeight());
return true;
}
});

mLinearLayoutHeader.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (mLinearLayout.getVisibility() == View.GONE) {
expand();
} else {
collapse();
}
}
});
}

private void expand() {
// set Visible
mLinearLayout.setVisibility(View.VISIBLE);

mAnimator.start();
}

private void collapse() {
int finalHeight = mLinearLayout.getHeight();

ValueAnimator mAnimator = slideAnimator(finalHeight, 0);

mAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
// Height=0, but it set visibility to GONE
mLinearLayout.setVisibility(View.GONE);
}

@Override
public void onAnimationStart(Animator animator) {
}

@Override
public void onAnimationCancel(Animator animator) {
}

@Override
public void onAnimationRepeat(Animator animator) {
}
});
mAnimator.start();
}

private ValueAnimator slideAnimator(int start, int end) {

ValueAnimator animator = ValueAnimator.ofInt(start, end);

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
// Update Height
int value = (Integer) valueAnimator.getAnimatedValue();

ViewGroup.LayoutParams layoutParams = mLinearLayout
.getLayoutParams();
layoutParams.height = value;
mLinearLayout.setLayoutParams(layoutParams);
}
});
return animator;
}
}

Animate visibility modes, GONE and VISIBLE

Like tomash said before: There's no easy way.

You might want to take a look at my answer here.

It explains how to realize a sliding (dimension changing) view.

In this case it was a left and right view: Left expanding, right disappearing.

It's might not do exactly what you need but with inventive spirit you can make it work ;)

View.GONE doesn't work after Translate Animation

I have done again a research on this link: Why doesn't setVisibility work after a view is animated?

And found the answer of @Chris Knight:

Another way to work around this is to wrap your animated view in another view and set the visibility of that wrapper view.

So I used as he did two FrameLayout and then set the setVisibility(View.GONE) for one at a time, because the user would hit one button at a time so it would open a Slide Menu at a time.

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#BE2625" >
<Button
android:id="@+id/btn_home1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="342"
/>

<Button
android:id="@+id/btn_home11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:text="34243"
/>
<FrameLayout
android:id="@+id/lsd1"
android:layout_height="match_parent"
android:layout_width="240dp">

<LinearLayout
android:id="@+id/lala"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#80000000"
android:visibility="gone"
android:orientation="vertical">

<Button
android:id="@+id/btn_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>

<Button
android:id="@+id/btn_book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>

<Button
android:id="@+id/btn_find_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>

<Button
android:id="@+id/btn_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>

</LinearLayout>
</FrameLayout>

<FrameLayout
android:id="@+id/lsd2"
android:layout_height="match_parent"
android:layout_width="240dp">
<LinearLayout
android:id="@+id/lala1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#80000000"
android:visibility="gone"
android:orientation="vertical">

<Button
android:id="@+id/btn_home2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>

<Button
android:id="@+id/btn_book2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>

<Button
android:id="@+id/btn_find_us2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>

<Button
android:id="@+id/btn_menu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.25"
/>

</LinearLayout>
</FrameLayout>

</RelativeLayout>


Related Topics



Leave a reply



Submit