Slide up animation not working correctrly - android

I have one Linearlayout.Here is a my xml code
<LinearLayout
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-4dp">
<android.support.v4.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="#+id/paper_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="#ffffff"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:src="#mipmap/ic_app_icon" />
<TextView
android:id="#+id/company_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:singleLine="true"
android:text="Company Name"
android:textColor="#android:color/black"
android:textSize="30dp" />
<TextView
android:id="#+id/company_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:singleLine="true"
android:text="Company Address"
android:textColor="#android:color/black"
android:textSize="14dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="10dp"
android:focusableInTouchMode="false"
android:listSelector="#00000000"
android:overScrollMode="never"
android:scrollbars="none" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:src="#mipmap/test_img" />
<View
android:layout_width="match_parent"
android:layout_height="16dp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
I goal is to create bottom to top animation.I wrote some code ,but animation does not working correctly. Here is a my java code.
In Activity's onCreate method
slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, -5.0f);
slide.setDuration(4000);
slide.setFillAfter(true);
slide.setFillEnabled(true);
I try to start animation like this
if (slide != null) {
linearLayout.startAnimation(slide);
slide.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
linearLayout.clearAnimation();
startActivity(MainActivity.class);
}
});
}
My goal is to start another activity as soon as animation finish
public void startActivity(Class<?> cls) {
Intent intent = new Intent(this, cls);
startActivity(intent);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
But,after 4 second,when animation has finished ,new activity does not working immediately.Is a any way to speed up calling new activity?

Try using this:
val animation = ObjectAnimator.ofFloat(layout, "translationY", 440f)
animation.duration = 4500
animation.interpolator = LinearInterpolator()
animation.addListener(AnimatorListenerAdapter() {
//animation end is here
// TODO
})
The code is in kotlin, but you can easily convert it

Related

LinearLayout onClick doesn't work after TranslateAnimation

Here is my problem. I have 6 LinearLayouts wich are all in centerVertical and centerHorizontal. And now I want them move, when the Activity starts, to all corners and sides, each. ( In each corner should be one Layout and on the left center and on the right center ). I achieve this, for Example, with this TranslateAnimation :
xbox360animation = new TranslateAnimation(0.0f, distanceWidth,
0.0f, 0.0f);
xbox360animation.setDuration(1500);
xbox360animation.setFillAfter(true);
xbox360Layout.startAnimation(xbox360animation);
animationxboxone = new TranslateAnimation(0.0f, -distanceWidth,
0.0f, 0.0f);
animationxboxone.setDuration(1500);
animationxboxone.setFillAfter(true);
xboxOneLayout.startAnimation(animationxboxone);
animationps3 = new TranslateAnimation(0.0f, distanceWidth,
0.0f, -distanceHeight);
animationps3.setDuration(1500);
animationps3.setFillAfter(true);
ps3Layout.startAnimation(animationps3);
animationps4 = new TranslateAnimation(0.0f, -distanceWidth,
0.0f, -distanceHeight);
animationps4.setDuration(1500);
animationps4.setFillAfter(true);
ps4layout.startAnimation(animationps4);
animationwiiu = new TranslateAnimation(0.0f, distanceWidth,
0.0f, distanceHeight);
animationwiiu.setDuration(1500);
animationwiiu.setFillAfter(true);
wiiuLayout.startAnimation(animationwiiu);
animationpc = new TranslateAnimation(0.0f, -distanceWidth, 0.0f, distanceHeight);
animationpc.setDuration(1500);
animationpc.setFillAfter(true);
pcLayout.startAnimation(animationpc);
My XML File:
<RelativeLayout
android:layout_width="match_parent"
android:layout_below="#id/header"
android:layout_above="#id/nextBtn"
android:layout_marginLeft="36dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginRight="36dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/xboxOneLayout"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/xboxOne"
android:padding="10dp"
android:duplicateParentState="true"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/xbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Xbox One"
android:id="#+id/xboxOnetv"
android:visibility="invisible"
android:duplicateParentState="true"
android:textColor="#fff"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/xbox360Layout"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/xbox360"
android:padding="10dp"
android:duplicateParentState="true"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/xbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Xbox 360"
android:id="#+id/xbox360tv"
android:textColor="#fff"
android:duplicateParentState="true"
android:visibility="invisible"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/ps3Layout"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/ps3"
android:padding="10dp"
android:duplicateParentState="true"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/playstation"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ps3"
android:duplicateParentState="true"
android:id="#+id/ps3tv"
android:textColor="#fff"
android:visibility="invisible"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/ps4Layout"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/ps4"
android:duplicateParentState="true"
android:padding="10dp"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/playstation"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ps4"
android:duplicateParentState="true"
android:id="#+id/ps4tv"
android:textColor="#fff"
android:visibility="invisible"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/wiiuLayout"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/wiiu"
android:duplicateParentState="true"
android:padding="10dp"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/wii"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WiiU"
android:duplicateParentState="true"
android:id="#+id/wiiutv"
android:visibility="invisible"
android:textColor="#fff"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:id="#+id/pcLayout"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/pc"
android:duplicateParentState="true"
android:padding="10dp"
android:background="#drawable/icon_background_not_selected"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/desktop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PC"
android:duplicateParentState="true"
android:textColor="#fff"
android:id="#+id/pctv"
android:visibility="invisible"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
</RelativeLayout>
I can't click on them after the animation. When I click on the position where it should be before the Animation, I get the onclick on one of the Layouts.
How can I set the Layouts to the new position, so I recieve the onClick Event from the right Layout?
Here i am showing you example of Gesture Listener in Code i have using image View but you can change it into Linear Layout
public class MainActivity extends Activity {
private ImageView iv;
private Matrix matrix = new Matrix();
private float scale = 1f;
private ScaleGestureDetector SGD;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.imageView);
SGD = new ScaleGestureDetector(this,new ScaleListener());
}
public boolean onTouchEvent(MotionEvent ev) {
SGD.onTouchEvent(ev);
return true;
}
private class ScaleListener extends ScaleGestureDetector.
SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
scale *= detector.getScaleFactor();
scale = Math.max(0.1f, Math.min(scale, 5.0f));
matrix.setScale(scale, scale);
iv.setImageMatrix(matrix);
return true;
}
}
}
TranslateAnimation only moves the pixels on the screen, and does not really move your LinearLayouts, it only makes it look like it is moving, so an OnClickListener will not animate with it. You still should be able to click at the old position before the animation.
Try ObjectAnimator or ViewPropertyAnimator.
Example with ViewPropertyAnimator for your xbox 360 layout:
xbox360Layout.animate().translationX(distanceWidth).setDuration(1500);
Now your layout really moves on the screen and your OnClick should work on the new position.

View animation only working second time

I'm trying to make an ImageButton slide up and out and then have another set of buttons slide in it's place, my problem is that the second buttons don't "slide" in the first time, only from the second and onward. I tried setting the button containers Visibility to invisible instead of gone, but it had no effect of the animation and just split the buttons to the top and bottom of the parent view.
xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:id="#+id/user_email_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#ffffff"
android:onClick="showEmail"
app:srcCompat="#drawable/logo_email" />+
<LinearLayout
android:id="#+id/user_email_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<ImageButton
android:id="#+id/user_email_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FFF"
android:onClick="addEmail"
android:visibility="gone"
app:srcCompat="#drawable/ic_add" />
<TextView
android:id="#+id/user_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<LinearLayout
android:id="#+id/user_email_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<ImageButton
android:id="#+id/user_email_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FFF"
android:onClick="editEmail"
app:srcCompat="#drawable/ic_edit" />
<ImageButton
android:id="#+id/user_email_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#FFF"
android:tint="#a7a7a7"
app:srcCompat="#drawable/ic_delete" />
</LinearLayout>
<ImageButton
android:id="#+id/user_email_hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFF"
android:onClick="hideEmail"
android:tint="#a7a7a7"
android:padding="15dp"
android:visibility="gone"
app:srcCompat="#drawable/ic_cancel" />
</LinearLayout>
</LinearLayout>
Show animation method
public void showEmail(View view) {
findViewById(R.id.user_email_hide).setVisibility(View.VISIBLE);
if (emailCheck == true) {
emailTextView.setVisibility(View.VISIBLE);
emailInput.setVisibility(View.VISIBLE);
} else {
addEmail.setVisibility(View.VISIBLE);
}
final Animation slide_up_in = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up_in);
Animation slide_up_out = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up_out);
showEmail.startAnimation(slide_up);
slide_up_out.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
showEmail.setVisibility(View.GONE);
emailContainer.setVisibility(View.VISIBLE);
emailContainer.setAnimation(slide_up_in);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
animation file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromYDelta="150%"
android:toYDelta="0" />
</set>

Increase height of the recyclerview after translation animation(using ObjectAnimator)

What I have done is on button click I have hidden the barcode scanner i.e used the ObjectAnimator for "Y" translation. At the same time, recyclerview which is below barcode scanner will also be translated in "Y" direction using ObjectAnimator. Animation works great only problem that when recylcerview is "Y" translated, then below it has some white space and I am not able to make it fill whole space available. Check below code which I have used.
//scannerLayout is barcode scanner and recycler view is list
int height1 = scannerLayout.getHeight();
ObjectAnimator objectAnimator = ofFloat(scannerLayout, "translationY", 0, -1 * (height1));
objectAnimator.setDuration(1000);
objectAnimator.start();
ObjectAnimator objectAnimator1 = ofFloat(recyclerView, "y", barcodeInputBoxHeight); //barcodeInputBoxHeight - is height of edittext which is shown between toolbar and recyalerview
objectAnimator1.setDuration(1000);
objectAnimator1.start();
objectAnimator1.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
// here I am doing some other stuff
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
I tried setting new layout params for recyclerview onAnimationEnd but it didn't work.
Check the below video for exact problem
Video
Below is layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_pos"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".modules.pos.POSActivity">
<FrameLayout
android:id="#+id/screenFrame"
android:layout_width="match_parent"
android:layout_height="200dp"
android:elevation="6dp">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="#+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.journeyapps.barcodescanner.DecoratedBarcodeView>
<ImageButton
android:id="#+id/imageButtonFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_margin="15dp"
android:background="#drawable/button_border"
app:srcCompat="#drawable/ic_action_flash"/>
<ImageButton
android:id="#+id/imageButtonInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="5dp"
android:layout_marginTop="25dp"
android:background="#drawable/button_border"
android:visibility="gone"
app:srcCompat="#drawable/ic_action_input_barcode"/>
</FrameLayout>
<FrameLayout
android:id="#+id/rlInputBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimary"
android:elevation="8dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="10dp"
android:visibility="visible">
<EditText
android:id="#+id/editTextSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:background="#drawable/round_corner"
android:ems="10"
android:hint="Search"
android:imeOptions="actionSearch"
android:inputType="number"
android:paddingBottom="5dp"
android:paddingEnd="30dp"
android:paddingLeft="10dp"
android:paddingRight="20dp"
android:paddingStart="10dp"
android:paddingTop="5dp"/>
<ImageView
android:id="#+id/imageViewCloseInputBox"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="end|center_vertical"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
android:src="#drawable/ic_action_close_input_box"/>
<ImageView
android:id="#+id/imageViewSwitchKeyboard"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="end|center_vertical"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:src="#drawable/ic_action_keyboard"/>
</FrameLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/buttonCheckOut"
android:layout_below="#+id/screenFrame"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"/>
<Button
android:id="#+id/buttonCheckOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
android:elevation="6dp"
android:text="Checkout"
android:textColor="#ffffff"/>

Android Animation disabled EditText

i add the Bounce effect to Linear layout.This linear layout contain 2 EditTexts and one button.but when animation complete Edittexts and Button are Disabled.I cant use them.how can i enable Edittexts and Button.
XML
<LinearLayout android:background="#drawable/login_grd" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginTop="50dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:textSize="55sp"
android:gravity="center"
android:layout_gravity="center"
android:textColor="#fff"
android:id="#+id/_title"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="AFF"/>
<Button
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="200dp"
android:textColor="#000"
android:background="#fff"
android:id="#+id/animloginButton"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Login"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="480dp"
android:background="#color/white"
android:id="#+id/lnr_do_login"
android:layout_marginTop="280dp">
<android.support.design.widget.TextInputLayout
android:theme="#style/TextLabel2"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:id="#+id/input_layout_username"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:textSize="16sp"
android:textColor="#color/black"
android:id="#+id/input_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:theme="#style/TextLabel2"
android:id="#+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
<EditText
android:textSize="16sp"
android:textColor="#color/black"
android:inputType="textPassword"
android:id="#+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password" />
</android.support.design.widget.TextInputLayout>
<Button
android:textColor="#000"
android:background="#fff"
android:id="#+id/loginButton"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Sign In"/>
</LinearLayout>
<TextView
android:textColor="#color/white"
android:layout_gravity="bottom|right"
android:gravity="bottom|center"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/app_name"
android:layout_marginBottom="10dp" />
</LinearLayout>
</LinearLayout>
LoginActivity.class
private LinearLayout lnrLoginView;
#Override
public void onCreate(Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.before_login_screen);
animloginButton = (Button) findViewById(R.id.animloginButton);
lnrLoginView = (LinearLayout) findViewById(R.id.lnr_do_login);
TextView title = (TextView) findViewById(R.id._title);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/title_font.ttf");
title.setTypeface(type);
animloginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
lnrLoginView.clearAnimation();
TranslateAnimation translation;
translation = new TranslateAnimation(0f, 0F, 0f, -getDisplayHeight());
translation.setStartOffset(10);
translation.setDuration(1000);
translation.setFillAfter(true);
translation.setInterpolator(new BounceInterpolator());
lnrLoginView.startAnimation(translation);
animloginButton.setVisibility(View.INVISIBLE);
}
private int getDisplayHeight() {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics.widthPixels;
}
});
}
Try to use ObjectAnimator.onFloat instead.
animloginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(lnrLoginView.getTag() instanceof Animator){
((Animator)lnrLoginView.getTag()).cancel();
}
ObjectAnimator translateY = ObjectAnimator
.ofFloat(target, "translationY", -transY)
.setDuration(1000)
;
translateY.setStartDelay(10);
translateY.setTarget(lnrLoginView);
translateY.setInterpolator(new BounceInterpolator());
translateY.start();
animloginButton.setVisibility(View.INVISIBLE);
}
private int getDisplayHeight() {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics.widthPixels;
}
});
Use a animation listener to malipulate your views before and after the animation.
translation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
//Disable the views.
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
//Enable the views
}
});

How to make a right to left animation in a layout

I am involved with a slide right to left problem. I created it by "hide" and "visible" layout on button clicked. But now I want an animation so that when I clicked on button, a "right to left" animation will happen and when I click again on button, same layout will be "left to right" animation and work as slide layout.
code
final RelativeLayout rightlayout = (RelativeLayout) findViewById(R.id.relativelayout2);
rightlayout.setVisibility(View.GONE);
Button rightbtn = (Button) findViewById(R.id.righttoleft);
rightbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("clickecd");
if(keyright == 0) {
//rightlayout.setVisibility(View.VISIBLE);
keyright++;
slideToLeft(rightlayout);
} else
{
//rightlayout.setVisibility(View.GONE);
TranslateAnimation animate = new TranslateAnimation(0,rightlayout.getWidth(),0,0);
animate.setDuration(1000);
animate.setFillAfter(true);
rightlayout.startAnimation(animate);
keyright--;
}
}
});
// To animate view slide out from left to right
public void slideToRight(View view){
TranslateAnimation animate = new TranslateAnimation(0,view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
}
// To animate view slide out from right to left
public void slideToLeft(View view){
TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
}
xml:
<RelativeLayout
android:id="#+id/rightsidemenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/frameLayout1"
android:gravity="right"
android:orientation="vertical" >
<Button
android:id="#+id/righttoleft"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="#drawable/right_switcher_expanded" />
<RelativeLayout
android:id="#+id/relativelayout2"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/righttoleft"
android:background="#EDEEF3"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:padding="5dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:gravity="right"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/titleframe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F4F4F4"
android:orientation="horizontal" >
<TextView
android:id="#+id/firsttitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Job and Qualification"
android:textColor="#56A91E" />
</FrameLayout>
<FrameLayout
android:id="#+id/firstframe1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/titleframe"
android:layout_margin="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView81"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Highest qualification" />
<TextView
android:id="#+id/higesttxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="highest qualification" />
</FrameLayout>
<FrameLayout
android:id="#+id/thirdff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/firstframe1"
android:layout_margin="10dp" >
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Occupation" />
<TextView
android:id="#+id/occupationtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="occupation" />
</FrameLayout>
<FrameLayout
android:id="#+id/thirdfff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/thirdff"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" >
<TextView
android:id="#+id/textView100"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CTC (Income)" />
<TextView
android:id="#+id/incometxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="income" />
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:gravity="right"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/titleframe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F4F4F4"
android:orientation="horizontal" >
<TextView
android:id="#+id/firsttitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Family Info"
android:textColor="#56A91E" />
</FrameLayout>
<FrameLayout
android:id="#+id/firstframe11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/titleframe"
android:layout_margin="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView811"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Have children" />
<TextView
android:id="#+id/havechildrntxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="have children" />
</FrameLayout>
<FrameLayout
android:id="#+id/secondfff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/firstframe11"
android:layout_margin="10dp" >
<TextView
android:id="#+id/textView91"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Father Alive" />
<TextView
android:id="#+id/fathrtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Father Alive" />
</FrameLayout>
<FrameLayout
android:id="#+id/thirdfff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/secondfff"
android:layout_margin="10dp" >
<TextView
android:id="#+id/textView101"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mother Alive" />
<TextView
android:id="#+id/mothertxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Mother alive" />
</FrameLayout>
<FrameLayout
android:id="#+id/thirdffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/thirdfff"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" >
<TextView
android:id="#+id/textView103"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Brother" />
<TextView
android:id="#+id/brothercounttxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Brother" />
</FrameLayout>
<FrameLayout
android:id="#+id/thirdffff3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/thirdffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
<TextView
android:id="#+id/textView104"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sister" />
<TextView
android:id="#+id/sistercounttxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Sister" />
</FrameLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
This code may help you.
Animation Left slide:
Create a file under res/anim/anim_left
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="100%"
android:toXDelta="0%" >
</translate>
Animation Right slide:
Create a file under res/anim/anim_right
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="-100%"
android:toXDelta="0%" >
</translate>
Use it in Activity as:
Animation rightSwipe = AnimationUtils.loadAnimation(this, R.anim.anim_right);
<view>.startAnimation(rightSwipe);
public void slidefromRightToLeft(View view) {
TranslateAnimation animate;
if (view.getHeight() == 0) {
main_layout.getHeight(); // parent layout
animate = new TranslateAnimation(main_layout.getWidth()/2,
0, 0, 0);
} else {
animate = new TranslateAnimation(view.getWidth(),0, 0, 0); // View for animation
}
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.VISIBLE); // Change visibility VISIBLE or GONE
}
Solution for those who are looking for right to left animation:
TranslateAnimation animation1 = new TranslateAnimation(0.0f, 0.0f, 1500.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation1.setDuration(1500); // animation duration
animation1.setRepeatCount(4); // animation repeat count if u want to repeat
animation1.setFillAfter(true);
your_view .startAnimation(animation);//your_view for mine is imageView
Solution for those who are looking for repeated animation (for eg. left to right and right to left)
TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)
animation.setDuration(1500); // animation duration
animation.setRepeatCount(8); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left)
animation.setFillAfter(true);
your_view.startAnimation(animation);//your_view for mine is imageView
Main layout and back layouts are two linear layouts, when click on button call the startanimation method.
boolean menuOut=false;
public void startAnimation()
{
Animation anim;
int w = mainLayout.getMeasuredWidth();
int h = mainLayout.getMeasuredHeight();
int left = (int) (mainLayout.getMeasuredWidth() * 0.80);
if(!menuOut)
{
anim = new TranslateAnimation(0, -left, 0, 0);
backLayout.setVisibility(View.VISIBLE);
animParams.init(-left, 0, -left + w, h);
}
else
{
anim = new TranslateAnimation(0, left, 0, 0);
animParams.init(0, 0, w, h);
}
anim.setDuration(400);
anim.setAnimationListener(Sample.this);
anim.setFillAfter(true);
mainLayout.startAnimation(anim);
}
AnimParams.java
public class AnimParams {
int left, right, top, bottom;
void init(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
}
You can also use animate().translationXBy([n_pixels]f).
where n_pixels] > 0 moves the object LTR, n_pixels] < 0 moves the object RTL.

Categories

Resources