Animating a relative layout to move down/up like Snack Bar - android

So I have been trying to animate a smaller relative layout down/up from another relative layout.
I used this in hopes of trying to solve my problem. I was thinking of doing something like the snack bar, but I still would want to add an image to it, which may be a problem completely on its own. Ideas?
Here is my xml:
<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/activity_log_in"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.esole.lithouse.SignUp"
android:background="#E6E6E6">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
android:id="#+id/logInBar"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
app:title="Log In" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="83dp"
android:id="#+id/editTextUsernameOrEmail"
android:imeOptions="actionNext"
android:layout_below="#+id/logInBar"
android:layout_centerHorizontal="true"
android:hint="username or email " />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:imeOptions="actionDone"
android:layout_centerVertical="true"
android:id="#+id/passwordLogIn"
android:hint="password "
android:layout_alignEnd="#+id/editTextUsernameOrEmail" />
<Button
android:text="Log In"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/LogIn"
android:layout_above="#+id/statusIndicator"
android:layout_centerHorizontal="true"
android:layout_marginBottom="67dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#006D6D"
android:id="#+id/statusIndicator"
android:animateLayoutChanges="true"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/indicatorStatusLabel"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#+id/imageView4"
android:layout_alignTop="#+id/imageView4"
android:layout_alignBottom="#+id/imageView4"
android:textAlignment="center"
android:textSize="24sp"
android:text="test "
android:typeface="normal"
android:lineSpacingExtra="24sp"
android:layout_marginRight="67dp" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
app:srcCompat="#drawable/refresh"
android:id="#+id/imageView4"
android:layout_marginStart="16dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Here is some of the code I have tried to use:
slideInAndOut = (RelativeLayout) findViewById(R.id.statusIndicator);
TranslateAnimation slide = new TranslateAnimation(0, 0, 0, -slideInAndOut.getHeight());
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
slide = new TranslateAnimation(0, 0, slideInAndOut.getHeight(), 0);
slide.setDuration(1000);
slide.setFillAfter(true);

First you should use android:src instead of app:srcCompat in your imageView
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) are the paras he is using!
Second this will work
slideInAndOut = (RelativeLayout) findViewById(R.id.statusIndicator);
TranslateAnimation slide = new TranslateAnimation(0, 0, 0, -slideInAndOut.getHeight());
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
slide = new TranslateAnimation(0, 0, 100, 0); // seems you have a problem with 3rd param
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
----- Wait you will see a code repetition there. Double check!
For only up this is enough!
TranslateAnimation slide = new TranslateAnimation(0, 0, 100, 0);
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
To add more value I modified your code a bit.
(now this view will come up and again will go down if you only need one TranslateAnimation you dont need both!)
// goes up
TranslateAnimation slide = new TranslateAnimation(0, 0, 100,0 );
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// goes down
TranslateAnimation slide = new TranslateAnimation(0, 0,0,100);
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
}
}, 3000);

Related

How to animate scaling of a relative layout(Which contains many layouts and ImageViews inside)

I tried with scaling but while scaling layouts inside are shrinking,
Can anyone explain how to animate a layout without shrinking
need to scale letterLayout from a little bit above the parent bottom to top
Animation anim = new ScaleAnimation(
1f, 1f, // Start and end values for the X axis scaling
startScale, endScale, // Start and end values for the Y axis scaling
Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
anim.setFillAfter(false); // Needed to keep the result of the animation
anim.setDuration(1000);
v.startAnimation(anim);
Layout code
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sample.LaunchScreenActivity">
<ImageView
android:layout_marginTop="195dp"
android:layout_marginLeft="26dp"
android:src="#drawable/ic_envelope_back"
android:layout_width="308dp"
android:layout_height="327dp" />
<RelativeLayout
android:id="#+id/letterLayout"
android:layout_width="284dp"
android:layout_height="451dp"
android:layout_marginLeft="38dp"
android:layout_marginTop="19dp">
<RelativeLayout
android:id="#+id/logo_layout"
android:layout_width="284dp"
android:layout_height="236dp"
android:background="#ff33">
<ImageView
android:layout_centerInParent="true"
android:src="#drawable/ic_logo_launch_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:background="#ff3344"
android:layout_below="#id/logo_layout"
android:layout_width="284dp"
android:layout_height="215dp">
<Button
android:layout_width="160dp"
android:layout_height="41dp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:background="#FC5D2D"
android:text="brinblox_login"
android:textColor="#ffff"/>
</RelativeLayout>
</RelativeLayout>
<FrameLayout
android:layout_marginTop="316dp"
android:layout_width="308dp"
android:layout_height="206dp"
android:layout_marginLeft="26dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/ic_envelope_front"/>
</FrameLayout>
</RelativeLayout>
I would suggest you to use a Value animator like this and change the margins of the RelativeLayout:
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
ValueAnimator va = ValueAnimator.ofInt(initialMargin, finalMargin);
int mDuration = 3000; //in millis
va.setDuration(mDuration);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
params.setMargins((int)animation.getAnimatedValue(),(int)animation.getAnimatedValue(),(int)animation.getAnimatedValue(),(int)animation.getAnimatedValue());
relativeLayout.setLayoutParams(params);
}
}
);
va.start();
Hope this helps.

ColorFilter doesn't work for TextView background

I want to change color of several TextView elements background using ColorFilters. I've tried several ways to do it. Two of them are bellow:
TextView tvLeftTop, tvLeftBottom;
tvLeftTop = (TextView) findViewById(R.id.textview_LeftTop);
tvLeftBottom = (TextView) findViewById(R.id.textview_LeftBottom);
float[] cmData = new float[]{
1, 0, 0, 1, 0,
0, 1, 0, 0, 0,
1, 1, 1, 1, 0,
0, 0, 0, 1, 0};
ColorMatrix cm = new ColorMatrix(cmData);
ColorFilter filter1 = new ColorMatrixColorFilter(cm);
ColorFilter filter2 = new PorterDuffColorFilter(0x20003300, PorterDuff.Mode.LIGHTEN);
tvLeftBottom.getBackground().setColorFilter(filter1);
tvLeftTop.getBackground().setColorFilter(filter2);
Both TextView are contained in GridLayout. Here is the appropriate part of .xml activity file:
<GridLayout
android:id="#+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:columnCount="4"
android:rowCount="4"
android:layout_above="#+id/seekBar">
<TextView
android:id="#+id/textview_LeftTop"
android:layout_columnWeight="1"
android:layout_rowWeight="2"
android:layout_rowSpan="2"
android:layout_row="0"
android:layout_column="0"
android:layout_marginBottom="3dp"
android:layout_marginRight="3dp"
android:layout_gravity="fill"
android:background="#6A77B7"
android:text="Title1" />
<TextView
android:id="#+id/textview_LeftBottom"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_columnWeight="1"
android:layout_rowWeight="2"
android:layout_rowSpan="2"
android:layout_row="2"
android:layout_column="0"
android:layout_marginTop="3dp"
android:layout_marginRight="3dp"
android:layout_gravity="fill"
android:background="#D64F97"
android:text="Title2" />
</GridLayout>
But there is no effect for both target text views (their colors are not changed).
What I am doing wrong?
P.S. Colors' values are test, so there are entire strings (not values from .xml file). I think it doesn't matter now.
tvLeftBottom.setBackgroundColor(Color.argb(0, 255, 255, 255));
Your textview don't have background so you can't set the color.
Set your color as the background.

Animation not smooth

I have made a app in which on button click i have slided a linear layout.The code to slide the linear layout is working fine but the problem is that it lags when sliding.Also the height which i have set to slide the layout doesnt look proper on all screen sizes(Specially on Nexus 5).Please do help me out
XMl
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_upload_add"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/add" />
<LinearLayout
android:id="#+id/ll_upload_options"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#drawable/choosefile_bg"
android:gravity="center"
android:orientation="vertical">
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
android:id="#+id/tv_upload_gallery"
style="#style/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="5dp"
android:text="GALLERY"
android:textSize="20sp"
android:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#drawable/line" />
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
android:id="#+id/tv_upload_camera"
style="#style/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="5dp"
android:text="CAMERA"
android:textSize="20sp"
android:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#drawable/line" />
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
android:id="#+id/tv_upload_file"
style="#style/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="5dp"
android:text="FILE"
android:textSize="20sp"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/all_left_mar"
android:layout_marginRight="#dimen/all_right_mar"
android:layout_weight="2"
android:gravity="center"
android:orientation="vertical">
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.EditTextFont
style="#style/edit_text"
android:drawableBottom="#drawable/line_black"
android:hint="File Title"
android:textColor="#000"
android:textColorHint="#000" />
<AutoCompleteTextView
style="#style/edit_text"
android:layout_marginTop="10dp"
android:completionThreshold="1"
android:drawableBottom="#drawable/line_black"
android:hint="Type"
android:textColor="#000"
android:textColorHint="#000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Switch
android:id="#+id/switch1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:textOff="No"
android:textOn="Yes" />
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
style="#style/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="#string/Protect"
android:textColor="#000" />
</LinearLayout>
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.EditTextFont
style="#style/edit_text"
android:layout_marginTop="10dp"
android:drawableBottom="#drawable/line_black"
android:hint="Password"
android:inputType="numberPassword"
android:textColor="#000"
android:textColorHint="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="#dimen/all_left_mar"
android:layout_marginRight="#dimen/all_right_mar"
android:layout_weight="1">
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.ButtonFont
style="#style/button"
android:text="UPLOAD" />
</LinearLayout>
</LinearLayout>
Code
if (llUploadOptions.getMeasuredHeight() != 0) {
// tvGallery.setVisibility(View.GONE);
// tvCamera.setVisibility(View.GONE);
// tvFile.setVisibility(View.GONE);
ValueAnimator anim = ValueAnimator.ofInt(llUploadOptions.getMeasuredHeight(),200);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = llUploadOptions.getLayoutParams();
layoutParams.height = val;
llUploadOptions.setLayoutParams(layoutParams);
}
});
anim.setDuration(700);
anim.start();
} else {
ValueAnimator anim = ValueAnimator.ofInt(llUploadOptions.getMeasuredHeight(),250);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = llUploadOptions.getLayoutParams();
layoutParams.height = val;
llUploadOptions.setLayoutParams(layoutParams);
}
});
anim.setDuration(700);
anim.start();
}
The problem is, that you are trying to animate the height of your Layout. That means, that with every animation step you call setLayoutParams() which forces your whole layout (incl. children) to be relayouted by going through the complete view hierarchy and this leads to laggy animation. Layouting things is an expensive operation!
There are some "workarounds" (I'm not sure what you want to do):
Since Android 4.0 you simply can use animate layout changes in your xml if you simply want to show / hide a element with animations in your layout
For more complex things you may consider to use Transitions framework introduced in Kitkat (Api 19) https://www.youtube.com/watch?v=S3H7nJ4QaD8. Backports: https://github.com/guerwan/TransitionsBackport or https://github.com/andkulikov/transitions-everywhere
"Fake" the animation by animating views by hand with translation, alpha etc. and set the LayoutParameters only once after the animation has been finished
I can't remember how the api is called, but you can subclass from ViewGroup and use it as your root layout. You have to intercept layout changes while animating the height somehow to get a smooth animation (this is how animateLayoutChanges = true works). However thats an advanced topic an I would recommend to go one of the other ways
you are using Animators (not Animations), which might be laggy, especially with moving widgets. try TranslateAnimation
TranslateAnimation animation = new TranslateAnimation(0.0f, 200.0f,
0.0f, 0.0f);
// these are delta's, check doc for this constructor
// TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
animation.setDuration(700);
llUploadOptions.startAnimation(animation);
if you want to leave your animated view with final coordinates use
animation.setFillAfter(true);
but it might not be clickable or other issues might occur. for these you might use
animation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation anim)
{
};
public void onAnimationRepeat(Animation anim)
{
};
public void onAnimationEnd(Animation anim)
{
//set fixed position in here, using LayoutParams or setTop/setRight etc. methods of View (API 11)
};
});
edit:
so instead of ValueAnimator you might do smth like this
int desiredHeightInPx = getResources().getDimensionPixelSize(R.dimen.expandedHeight);
//note those are pixels, not dp. you might set this as =200 or =250 like you have
ScaleAnimation animation = new ScaleAnimation(0, 0, 0, desiredHeightInPx/llUploadOptions.getMeasuredHeight(), Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
animation.setDuration(1000); //ms
llUploadOptions.startAnimation(animation);
next steps as above, fillAfter or set AnimationListener and inside onAnimationEnd set final LayoutParams.
be aware that ScaleAnimation is scaling, it might be more appropiate to use RelativeLayout instead of Linear (which rescales its childers)

After Slide Up/Down Animation Textview onClickListener not working

I'm using Slide Up/Down Animation for my four TextView. Following is my XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shadow"
android:orientation="vertical"
android:weightSum="2.0" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" >
<TextView
android:id="#+id/textViewblue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginLeft="25dp"
android:background="#drawable/blue"
android:gravity="center"
android:text="blue" />
<TextView
android:id="#+id/textVieworange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginRight="25dp"
android:background="#drawable/orange"
android:gravity="center"
android:text="orange" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<TextView
android:id="#+id/textViewpink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:background="#drawable/pink"
android:gravity="center"
android:text="pink" />
<TextView
android:id="#+id/textViewgreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:background="#drawable/green"
android:gravity="center"
android:text="green" />
</RelativeLayout>
</LinearLayout>
and Following is my Class
public class HomeScreenActivity extends Activity implements OnClickListener {
TextView textViewblue, textVieworange, textViewpink,
textViewgreen;
Animation mAnimation_up, mAnimation_down;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
mAnimation_up = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 1.6f,
TranslateAnimation.RELATIVE_TO_SELF, 0f);
mAnimation_up.setDuration(1000);
mAnimation_up.setRepeatCount(-1);
mAnimation_up.setRepeatMode(Animation.REVERSE);
mAnimation_up.setInterpolator(new LinearInterpolator());
mAnimation_down = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 1.6f);
mAnimation_down.setDuration(1000);
mAnimation_down.setRepeatCount(-1);
mAnimation_down.setRepeatMode(Animation.REVERSE);
mAnimation_down.setInterpolator(new LinearInterpolator());
initialize();
}
private void initialize() {
textViewblue = (TextView) findViewById(R.id.textViewblue);
textVieworange = (TextView) findViewById(R.id.textVieworange);
textViewpink = (TextView) findViewById(R.id.textViewpink);
textViewgreen = (TextView) findViewById(R.id.textViewgreen);
textViewblue .setOnClickListener(this);
textVieworange .setOnClickListener(this);
textViewpink .setOnClickListener(this);
textViewgreen .setOnClickListener(this);
textViewblue .setAnimation(mAnimation_down);
textViewpink .setAnimation(mAnimation_down);
textVieworange .setAnimation(mAnimation_up);
textViewgreen .setAnimation(mAnimation_up);
}
#Override
public void onClick(View v) {
Intent intentmenu;
switch (v.getId()) {
case R.id.textViewblue:
Toast.makeText(this, "blue", Toast.LENGTH_LONG).show();
break;
case R.id.textVieworange:
Toast.makeText(this, "orange", Toast.LENGTH_LONG).show();
break;
case R.id.textViewpink:
Toast.makeText(this, "pink", Toast.LENGTH_LONG).show();
break;
case R.id.textViewgreen:
Toast.makeText(this, "reeng", Toast.LENGTH_LONG).show();
break;
}
}
}
The problem is that when any TextViewslide for int position means when animation starts the onClickListener works only at its XML position not for its updated position.
Please suggest me the solution for that, how can I detect the updated position for to detect onClickListener on TextView.
Thanks.
Maybe that's a bit complicated to make it. As you're using View animation system, the behavior is under expectation. Although the Views are translated to other places, they're just being
drawn there. From the system's perspective, they're in fact always at the original place where you define in xml file. If you want the click event at the new place, you should use the new property animation system.
The onClickListener is not working because views are shifted from their original place due to animation. To solve this create a same layout below your animation and initially make its Gravity as GONE and when your animation finishes up make its Gravity as Visible and your animation layout visibility as Gone.
also be careful that all the id's of animation and original layout would not be same.

Animating the linear layout

I am implementing the listview with one image and framelayout(conatining button and linearLayout) , I want to animate the linearLayout on click of image. Is it possible? I have written following code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="2dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/icon" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<LinearLayout
android:id="#+id/ll1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/a"
android:orientation="vertical" >
<TextView
android:id="#+id/from_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
<TextView
android:id="#+id/from_user_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
</FrameLayout>
Please help me.
You can call animate method in onClickListener of imageview like..
ivmg.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
startLayoutAnimation(myLayout);
return false;
}
});
And define method as...
public void startLayoutAnimation(LinearLayout mLayout)
{
System.out.println("Inside startAnimation()");
Animation scaleAnim = new ScaleAnimation(0, 2, 0, 2);
scaleAnim.setDuration(5000);
scaleAnim.setRepeatCount(1);
scaleAnim.setInterpolator(new AccelerateInterpolator());
scaleAnim.setRepeatMode(Animation.REVERSE);
/* Animation rotateAnim = new RotateAnimation(0, 360);
rotateAnim.setDuration(5000);
rotateAnim.setRepeatCount(1);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);*/
Animation rotateAnim = new RotateAnimation(0, 360, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF);
rotateAnim.setDuration(5000);
rotateAnim.setRepeatCount(1);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(scaleAnim);
animationSet.addAnimation(rotateAnim);
mLayout.startAnimation(animationSet);
}
}
I haven't tried animating layout yet. So you will have to check whether its working or not. Hope it helps

Categories

Resources