one more problem.
I am trying to make RecycleView items fade away, so I wrote an XML animation for each item, then a layer animation XML and added code in Java main activity. Everything works ok, views disappear, but the problem is, after they all disappear, they all of a sudden appear back again on their own! What can be the reason? How to keep alpha 0?
Codes:
Main activity method:
private void layoutDisappear(final RecyclerView recyclerView) {
if(recyclerView.isAnimating()){
return;
}
final Context context = recyclerView.getContext();
final LayoutAnimationController controller =
AnimationUtils.loadLayoutAnimation(context, R.anim.layout_recycleview_disappear);
recyclerView.setLayoutAnimation(controller);
recyclerView.setLayoutAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
}
});
recyclerView.scheduleLayoutAnimation();
Item animation XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1400">
<alpha
android:fromAlpha="1"
android:interpolator="#android:anim/decelerate_interpolator"
android:toAlpha="0" />
<scale
android:fromXScale="100%"
android:fromYScale="100%"
android:interpolator="#android:anim/decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="102%"
android:toYScale="102%" />
</set>
Layer XML:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="#anim/item_recycleview_disappear"
android:animationOrder="reverse"
android:delay="7%" />
You need add android:fillAfter="true" property to Item animation XML to keep animation changes.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:duration="1400">
...
</set>
Related
I'm showing issues with animation. Animation works fine when i clicked on button to show linear layout when i clicked buttton to close linear layout animation perform as i want but when i click second time to open linear layout animation doesn't work for both to show layout or close layout. I also want to inform you that button click perform properly. layout visibility GONE & VISIBLE works proper but animation didn't work second time.
public class AdminViewComplaintActivity extends AppCompatActivity implements View.OnClickListener {
ImageView btn_search, btn_close_search ;
LinearLayout ll_search;
Animation animationIn, animationOut;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_complaint);
btn_search = findViewById(R.id.btn_search);
btn_close_search = findViewById(R.id.btn_close_search);
ll_search = findViewById(R.id.ll_search);
animationIn= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.view_in);
animationOut= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.view_out);
}
#Override
public void onClick(View view) {
if (view==btn_search){
if(ll_search.getVisibility()==View.GONE){
// show linear layout with animation
ll_search.setAnimation(animationIn);
ll_search.setVisibility(View.VISIBLE);
}
}
else if(view==btn_close_search){
// close linear layout with animation
ll_search.setAnimation(animationOut);
ll_search.setVisibility(View.GONE);
}
}
}
xml for animation view_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<translate
android:duration="1000"
android:fromXDelta="100%"
android:toXDelta="0%" />
</set>
xml for animation view_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<translate
android:duration="1000"
android:fromXDelta="0%"
android:toXDelta="100%" />
</set>
add this code to view
anim.setFillEnabled(true);
anim.setFillAfter(true);
in programmatically use this :
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.nameYourAnim);
viewToAnimate.startAnimation(animation);
for example anim :
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="#android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="#android:integer/config_mediumAnimTime" />
I have a relative layout, I need to show that view when network connectivity is not there and gone that view when connection came back, I need to do it like youtube. YT will show the popup in bottom with green color and after sometimes the view slide down. Thanks in advance I am new to this. I can't find any equivalent answer in StackOverflow
<RelativeLayout
android:id="#+id/dashboard_xmpp_connection_status_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:visibility="gone">
<TextView
android:id="#+id/dashboard_xmpp_connection_text"
android:layout_width="match_parent"
android:layout_height="33dp"
android:animateLayoutChanges="true"
style="#style/Connection_status"
android:text="#string/msg_no_internet" />
</RelativeLayout>
binding.filter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (binding.filterExams.getVisibility() == View.VISIBLE) {
Animation slide_down = AnimationUtils.loadAnimation(binding.filterExams.getRootView().getContext(),
R.anim.slide_up_filter);
slide_down.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
binding.filterExams.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
binding.filterExams.startAnimation(slide_down);
} else {
binding.filterExams.setVisibility(View.VISIBLE);
Animation slide_down = AnimationUtils.loadAnimation(binding.filterExams.getRootView().getContext(),
R.anim.slide_down);
binding.filterExams.startAnimation(slide_down);
}
}
});
you can add animation file in xml file for e.g
here i used slide down and up animation
slide_up_filter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<scale
android:duration="250"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<scale
android:duration="250"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
like this there are sevaral animation
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="0.1"
android:toAlpha="1.0">
</alpha>
</set>
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
You can use Transition API which is available in Support(androidx) package.
To use this you have to call TransitionManager.geginDelayedTransition method and change the visibility of the view to visible and gone as per the requirement.
private void slideView(boolean show) {
Transition transition = new Slide(Gravity.BOTTOM);
transition.setDuration(600);
transition.addTarget(R.id.id_of_relative_layout);
TransitionManager.beginDelayedTransition(id_of_relative_layout, transition);
if(show){
relativeView.setVisibility(View.VISIBLE);
}
else{
relativeView.setVisibility(View.GONE);
}
}
Call slideView() function on the action from where you want to perform it.
For more deatil follow this -
Show and hide a View with a slide up/down animation
so you can use LottieAnimationView
your app gradle file
implementation 'com.airbnb.android:lottie:3.0.7'//2.7.0
go this website sign and download free lottie json file
please click
step 1:put this download lottie json file to android studio assets folder
your layout file
<FrameLayout
android:id="#+id/dashboard_xmpp_connection_status_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:visibility="gone">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/showAnimations"
android:layout_gravity="center"
android:layout_height="66dp"
android:layout_width="56dp"
android:visibility="gone"
app:lottie_autoPlay="true"
app:lottie_loop="false"
app:lottie_fileName="animsix.json" <!--please put your download json file -->
/>
</FrameLayout>
MainActivity
LottieAnimationView showLottie=findViewById(R.id.showAnimations);
if("network is available"){
showLottie.setVisibility(View.GONE);
}else{
showLottie.setVisibility(View.VISIBLE);
}
You could simply use fade in animation for text view like this.
void fadeIn() {
textView.setVisibility(View.VISIBLE);
textView.setAlpha(0);
int duration = getResources().getInteger(android.R.integer.config_mediumAnimTime);
textView.animate().alpha(1).setDuration(duration);
}
textView.animate() returns ViewPropertyAnimator which contains various methods to customize animations.
Visit this for more details
I have an app with certain background and i want to change it to different background very nicely and gradually on a button click.
I tried doing it with objectanimator by setting background attribute of root layout to two png files that are in my drawable folder but it did not work as type of value of background is in drawable.
My root layout is relative layout and i want to change its background.
RelativeLayout.setbackground(drawable image);
,and objectanimator does not take property with values that are not int,float etc which in my case i have drawable type.
objectanimator.offloat(view,property,values....);
What are the best ways to accomplish this without any library?
Add these two animations to your anim folder
fade_in.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:fillAfter="true"
android:duration="2000"
/>
</set>
fade_out.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:fillAfter="true"
android:duration="2000"
/>
</set>
and then in your Activity/Fragment
Animation fadeIn = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in);
imageView.startAnimation(fadeIn);
fadeIn.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_out);
imageView.startAnimation(fadeOut);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
How can I move an object out of the screen?
This is how I move the TextView from under the screen into the screen.
slide_up_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%" android:toYDelta="0%" android:duration="500" android:fillAfter="true"/>
</set>
This is how I move it out of the screen:
slide_down_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="100%" android:duration="500" android:fillAfter="true"/>
</set>
But the TextView appears again. Why?
That is because it renders it again after the animation and will actually place the TextView to its default position from the screen/layout.
solution:
You need to add a listener and set the visibility of your TextView to gone or invisible at the end of the listener.
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation) {
You_text_view.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
I have 2 imageview (a imageview for a pen image, and a imageview for a line, both are drawable), that each one has it own view animation that works perfectly.
my problem is that when I start the animation for the pen I want it to interact and animate the drawing of the line in the other view animation (I want that it will show as the pen is drawing the line), how do I do that?
my xml for animation for the pen imageview is:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:toXDelta="100%p"
android:fromYDelta="-50%p"
android:toYDelta="-50%p"
android:duration="2000"/>
</set>
my xml for animation for the line imageview is:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="0%"
android:pivotY="50%"
android:fromXScale="0%"
android:toXScale="100%"
android:fromYScale="1.0"
android:toYScale="1.0"
android:fillAfter="true"
android:duration="2000"
android:interpolator="#android:anim/linear_interpolator"
/>
please help!
I believe this can be done with the help of animationListener. Add an animationListener for the pen's animation. In onAnimationStart method do lineImage.startAnimation(lineAnimation);
Sample:
penAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
lineImage.startAnimation(lineAnimation);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});