Layout expand animation - android

I have done the layout expand animation with the following code. It works fine when we invoke the animation by clicking a text view, but the same is not happening when I try to do that by clicking a layout. Can anyone help me in solving this?
LinearLayout hello= (LinearLayout)findViewById(R.id.lin_hello);
final RelativeLayout rel=(RelativeLayout)findViewById(R.id.rel_nah_hide);
hello.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
expand=!expand;
Animation a=expand(rel, expand);
rel.setAnimation(a);
a.start();
}
});
public static Animation expand(final View v, final boolean expand) {
try {
Method m = v.getClass().getDeclaredMethod("onMeasure", int.class, int.class);
m.setAccessible(true);
m.invoke(v,
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(((View)v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST)
);
} catch (Exception e) {
e.printStackTrace();
}
final int initialHeight = v.getMeasuredHeight();
if (expand) {
v.getLayoutParams().height = 0;
} else {
v.getLayoutParams().height = initialHeight;
}
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight = 0;
if (expand) {
newHeight = (int) (initialHeight * interpolatedTime);
} else {
newHeight = (int) (initialHeight * (1 - interpolatedTime));
}
v.getLayoutParams().height = newHeight;
v.requestLayout();
if (interpolatedTime == 1 && !expand)
v.setVisibility(View.GONE);
}
#Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(SPEED_ANIMATION_TRANSITION);
a.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationEnd(Animation arg0) {
animWorkingFlag=false;
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
animWorkingFlag=true;
}
});
return a;
}
and following is the xml layout,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/lin_hello"
android:layout_height="50dp"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/hello"
android:layout_height="50dp"
android:layout_width="fill_parent"
android:text="Nah"
android:gravity="center"
android:background="#F03535"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/rel_nah_hide"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="#+id/txt_nah_hide1"
android:layout_height="50dp"
android:layout_width="fill_parent"
android:text="Incorrect Name/Address"
android:gravity="center"
android:textColor="#color/white"/>
<TextView
android:id="#+id/txt_nah_hide2"
android:layout_height="50dp"
android:layout_width="fill_parent"
android:text="Venue is closed"
android:gravity="center"
android:textColor="#color/white"
android:layout_below="#id/txt_nah_hide1"/>
<TextView
android:id="#+id/txt_nah_hide3"
android:layout_height="50dp"
android:layout_width="fill_parent"
android:text="Venue is duplicate"
android:gravity="center"
android:textColor="#color/white"
android:layout_below="#id/txt_nah_hide2"/>
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="28dp"
android:text="heelo2222"/>
</LinearLayout>

Use next part code: ->
View hello= findViewById(R.id.lin_hello);
final RelativeLayout rel=(RelativeLayout)findViewById(R.id.rel_nah_hide);
hello.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
expand=!expand;
Animation a=expand(rel, expand);
rel.setAnimation(a);
a.start();
}
});

Related

collapse and expand animation issue android

When i click first time this animation collapse and expand itself suddenly, afterwards when click then it goes fine. problem is why this animation expand itself first time. Any expert here?
this is my xml code
<TextView
android:drawableRight="#drawable/ic_arrow_down"
android:background="#FFF12222"
android:textColor="#060606"
android:textSize="20sp"
android:text="Required Field"
android:id="#+id/section_required_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="vertical"
android:id="#+id/layout_required_fields"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_marginTop="10dp"
android:id="#+id/tile_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="16sp"
android:textColor="#060606"/>
<EditText
android:id="#+id/title"
android:layout_width="270sp"
android:layout_height="wrap_content"
android:text="abcd"
android:layout_marginTop="2dp"
android:background="#drawable/rounded_edittext"
android:textSize="15sp"/>
<TextView
android:id="#+id/description_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:text="Description"
android:textSize="15sp"
android:textColor="#060606"/>
<EditText
android:id="#+id/video_description"
android:layout_width="270sp"
android:layout_height="wrap_content"
android:layout_marginTop="2sp"
android:text="abcd"
android:background="#drawable/rounded_edittext"
android:textSize="15sp"/>
<TextView
android:id="#+id/category_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:text="Category"
android:textSize="15sp"
android:textColor="#060606"/>
<TextView
android:id="#+id/video_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2sp"
android:text="abcd"
android:textSize="15sp"
android:textColor="#060606"/>
<TextView
android:id="#+id/tags_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:text="Tags"
android:textSize="15sp"
android:textColor="#060606"/>
<EditText
android:id="#+id/tags"
android:layout_width="270sp"
android:layout_height="wrap_content"
android:layout_marginTop="2sp"
android:text="abcd"
android:textSize="15sp"
android:background="#drawable/rounded_edittext"
android:textColor="#060606"/>
</LinearLayout>
this is my java class for animation
public class AnimationUtils {
public static void expand(final View v) {
v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
Animation a = new Animation()
{
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? ViewGroup.LayoutParams.WRAP_CONTENT
: (int)(targetHeight * interpolatedTime);
v.requestLayout();
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
public static void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation()
{
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
}
and this is my java class code
tvRequiredField = (TextView) findViewById(R.id.section_required_field);
requiredFieldsLayout = (LinearLayout) findViewById(R.id.layout_required_fields);
tvRequiredField.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.isSelected()) {
AnimationUtils.collapse(requiredFieldsLayout);
v.setSelected(false);
}
else {
AnimationUtils.expand(requiredFieldsLayout);
v.setSelected(true);
}
}
});
i got my mistake
tvRequiredField = (TextView) findViewById(R.id.section_required_field);
requiredFieldsLayout = (LinearLayout) findViewById(R.id.layout_required_fields);
tvRequiredField.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.isSelected()) {//this should be expand instead of collapse
AnimationUtils.collapse(requiredFieldsLayout);
v.setSelected(false);
}
else {//this should be collapse instead of expand
AnimationUtils.expand(requiredFieldsLayout);
v.setSelected(true);
}
}
});

Android code hide animating button

What i need to make an animated button like this, that contains other buttons when clicked.
it begins
Begin
And transform
Clicked
We use the fabAction Library:
compile 'com.github.shell-software:fab:1.0.5'
https://github.com/shell-software/fab
Our XML is like this:
<RelativeLayout
android:id="#+id/news_item_actions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/fab_margin"
android:layout_marginBottom="#dimen/fab_margin"
android:orientation="vertical"
android:visibility="visible"
>
<com.software.shell.fab.ActionButton
android:id="#+id/news_item_share_sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="0dp"
app:button_color="#color/share_sms"
app:image="#drawable/share_button_sms"
/>
<com.software.shell.fab.ActionButton
android:id="#+id/news_item_share_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="70dp"
app:button_color="#color/share_email"
app:image="#drawable/share_button_mail"
/>
<com.software.shell.fab.ActionButton
android:id="#+id/news_item_share_twitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="140dp"
app:button_color="#color/share_twitter"
app:image="#drawable/share_button_twitter"
/>
<com.software.shell.fab.ActionButton
android:id="#+id/news_item_share_facebook"
android:layout_width="36dp"
android:layout_height="36dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="210dp"
app:button_color="#color/share_facebook"
app:image="#drawable/share_button_facebook"
/>
<com.software.shell.fab.ActionButton
android:id="#+id/news_item_share_dummy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="280dp"
app:button_color="#color/share_cancel"
app:image="#drawable/share_button_cancel"
/>
</RelativeLayout>
Our java code is like this:
final ActionButton actionButton = (ActionButton) findViewById(R.id.news_item_action_button);
final ActionButton actionCancelButton = (ActionButton) findViewById(R.id.news_item_action_cancel_button);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expand(findViewById(R.id.news_item_share_facebook), 1);
expand(findViewById(R.id.news_item_share_twitter), 2);
expand(findViewById(R.id.news_item_share_email), 3);
expand(findViewById(R.id.news_item_share_sms), 4);
actionCancelButton.setVisibility(View.VISIBLE);
actionButton.setVisibility(View.GONE);
}
});
actionCancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
collapse(findViewById(R.id.news_item_share_facebook), 1);
collapse(findViewById(R.id.news_item_share_twitter), 2);
collapse(findViewById(R.id.news_item_share_email), 3);
collapse(findViewById(R.id.news_item_share_sms), 4);
actionCancelButton.setVisibility(View.GONE);
actionButton.setVisibility(View.VISIBLE);
}
});
public void expand(final View v, int pos) {
v.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams paramsTemp = (RelativeLayout.LayoutParams)v.getLayoutParams();
float d = getResources().getDisplayMetrics().density;
final int expectedMove = paramsTemp.topMargin - mMargins[pos-1];//(int)((ACTION_SIZE*pos) * d);
final int[] topMargin = {paramsTemp.topMargin,mMargins[pos-1]};
Animation a = new Animation()
{
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)v.getLayoutParams();
if(interpolatedTime == 1){
params.setMargins(params.leftMargin, topMargin[1],params.rightMargin,params.bottomMargin);
v.setLayoutParams(params);
}else{
int addedMargin = (int)(expectedMove * interpolatedTime);
int marginTop = topMargin[0] - addedMargin;
params.setMargins(params.leftMargin, marginTop,params.rightMargin,params.bottomMargin);
v.setLayoutParams(params);
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setInterpolator(new DecelerateInterpolator(1.0f));
a.setDuration(750);
v.startAnimation(a);
}
/*
* This method is use to animate the collapse of share actions in this activity.
* #param (final View v) The view which will be expanded.
*/
public void collapse(final View v, int pos) {
RelativeLayout.LayoutParams paramsTemp = (RelativeLayout.LayoutParams)v.getLayoutParams();
final int[] topMargin = {paramsTemp.topMargin};
float d = getResources().getDisplayMetrics().density;
final int expectedMove = (int)((ACTION_SIZE*pos) * d);
Animation a = new Animation()
{
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if(interpolatedTime == 1){
v.setVisibility(View.GONE);
}else{
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)v.getLayoutParams();
int addedMargin = (int)(expectedMove * interpolatedTime);
int marginTop = topMargin[0] + addedMargin;
params.setMargins(params.leftMargin, marginTop,params.rightMargin,params.bottomMargin);
v.setLayoutParams(params);
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setInterpolator(new AccelerateInterpolator(1.0f));
a.setDuration(750);
v.startAnimation(a);
}
That is how we make the animation work. Hope it works.

Android View animation view stuttering

I have a LinearLayout with some views init (transparent grey background, TextView, View, ...). And I want to animate that with a slideIn, slideOut animation (defined in xml).
When the first animation is finished, the second animation starts. During animation you can see a stuttering.
I nearly checked all posts, no one helped !
There is no complicated code.
Load animation from xml and start animation. If the first stops, the other starts.
here is my layout of the view, which will animated:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/overlay_deal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="#dimen/spacing_small">
<TextView
android:id="#+id/text_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="2dp"
android:textAppearance="#style/TextAp.Dyn.Fixed.Small"
android:textColor="#color/white"
android:textStyle="bold" />
<TextView
android:id="#+id/text_header_promo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="#string/Corporate_Label_Title"
android:textAppearance="#style/TextAp.Dyn.Fixed.Small"
android:textColor="#color/white" />
</LinearLayout>
<View
android:id="#+id/text_header_underline"
android:layout_width="match_parent"
android:layout_height="3dp" />
<View
android:id="#+id/underline_spacer"
android:layout_width="match_parent"
android:layout_height="15dp"
android:background="#color/white"
android:visibility="gone" />
</LinearLayout>
This is the code:
private void initAnimations(Context context, final View defaultMoodBannerView) {
final int slideInFromTopAnimationId = R.anim.slide_in_from_top;
final int slideOutToTopAnimationId = R.anim.slide_out_to_top;
final int animationDuration = context.getResources().getInteger(android.R.integer.config_mediumAnimTime);
animationDefaultMoodBannerChangeToCorporate = AnimationUtils.loadAnimation(context, slideOutToTopAnimationId);
animationDefaultMoodBannerChangeToCorporate.setDuration(animationDuration);
animationDefaultMoodBannerSlideInFromTop = AnimationUtils.loadAnimation(context, slideInFromTopAnimationId);
animationDefaultMoodBannerSlideInFromTop.setDuration(animationDuration);
animationCorporateMoodBannerChangeToDefault = AnimationUtils.loadAnimation(context, slideOutToTopAnimationId);
animationCorporateMoodBannerChangeToDefault.setDuration(animationDuration);
animationCorporateMoodBannerSlideInFromTop = AnimationUtils.loadAnimation(context, slideInFromTopAnimationId);
animationCorporateMoodBannerSlideInFromTop.setDuration(animationDuration);
animationCorporateMoodBannerSlideOutToTop = AnimationUtils.loadAnimation(context, slideOutToTopAnimationId);
animationCorporateMoodBannerSlideOutToTop.setDuration(animationDuration);
animationDefaultMoodBannerChangeToCorporate.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if (corporateMoodBannerView != null) {
if (defaultMoodBannerView != null) {
defaultMoodBannerView.setVisibility(View.GONE);
}
corporateMoodBannerView.setVisibility(View.VISIBLE);
corporateMoodBannerView.startAnimation(animationDefaultMoodBannerSlideInFromTop);
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
animationCorporateMoodBannerChangeToDefault.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if (defaultMoodBannerView != null) {
if (corporateMoodBannerView != null) {
corporateMoodBannerView.setVisibility(View.GONE);
}
defaultMoodBannerView.setVisibility(View.VISIBLE);
defaultMoodBannerView.startAnimation(animationDefaultMoodBannerSlideInFromTop);
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}

Sliding in out layout animation on scroll android

I am trying to do a TranslateAnimation but don't work properly the layout apear and disapear but didn't do any animation. How could I solve this?
The java code is this:
private void initComponents() {
ImageView postIV = (ImageView) findViewById(R.id.post_view_activity_image_iv);
CropBottonTransformation cropBottomTransformation = new CropBottonTransformation();
Picasso.with(context).load(bundle.getString(Constants.POST_IMAGE))
.centerCrop().fit().placeholder(R.drawable.home_placeholder)
.into(postIV);
TextView postTitleTV = (TextView) findViewById(R.id.post_view_activity_title_tv);
postTitleTV.setText(bundle.getString(Constants.POST_TITLE));
ObservableScrollView scroll = (ObservableScrollView) findViewById(R.id.post_view_activity_scroll);
scroll.setScrollViewListener(this);
hidenOptionsLL = (LinearLayout) findViewById(R.id.post_view_activity_slide_options);
hidenOptionsHeight = hidenOptionsLL.getHeight();
slideRL = (RelativeLayout) findViewById(R.id.post_view_activity_slide_rl);
}
...
#Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
int oldx, int oldy) {
if (y > oldy) {
if (sliding == false) {
// Animation animation =
// AnimationUtils.loadAnimation(context,
// R.anim.slide_in_out);
Animation animation = new TranslateAnimation(0, 0,
hidenOptionsHeight, 0);
animation.setInterpolator(new AccelerateInterpolator(1.0f));
animation.setDuration(600);
hidenOptionsLL.startAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
sliding = true;
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
sliding = false;
hidenOptionsLL.setVisibility(View.GONE);
}
});
}
} else if (y < oldy) {
if (sliding == false) {
// Animation animation =
// AnimationUtils.loadAnimation(context,
// R.anim.slide_in_out);
Animation animation = new TranslateAnimation(0, 0, 0,
hidenOptionsHeight);
hidenOptionsLL.setVisibility(View.VISIBLE);
animation.setInterpolator(new AccelerateInterpolator(1.0f));
animation.setDuration(600);
hidenOptionsLL.startAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
sliding = true;
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
sliding = false;
}
});
}
}
}
The layout is this:
<LinearLayout
android:id="#+id/post_view_activity_slide_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="horizontal"
android:visibility="gone" >
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:src="#android:drawable/arrow_down_float" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:src="#android:drawable/arrow_up_float" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:src="#android:drawable/ic_delete" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:src="#android:drawable/btn_radio" />
</LinearLayout>
I don't find any solution. I want that it works like the options that appear on the bottom of the google search app.
Slideinleft.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%p" android:toXDelta="0%p"
android:duration="#android:integer/config_shortAnimTime"/>
Slideinright.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-100%p" android:toXDelta="0%p"
android:duration="#android:integer/config_shortAnimTime" />
Slideoutleft.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="#android:integer/config_shortAnimTime" />
Slideoutright.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="100%p"
android:duration="#android:integer/config_shortAnimTime" />
use when go one activity to other.
overridePendingTransition(R.anim.slide_in_right,
R.anim.slide_out_right);
It sounds like this library might help you:
https://github.com/flavienlaurent/discrollview?utm_source=Android+Weekly&utm_campaign=2daec38d18-Android_Weekly_86&utm_medium=email&utm_term=0_4eb677ad19-2daec38d18-337293613
But otherwise, you're going to want to set fillEnabled fillBefore and fillAfter to true
The problem is that when I ask for the height at first time the view isn't visible an the value of the height was 0:
I did this:
ImageButton minusLetterIB = (ImageButton) findViewById(R.id.post_view_activity_minus_leters_ib);
hidenOptionsHeight = minusLetterIB.getDrawable().getIntrinsicHeight();
and changed the animation to hide an show the layout:
#Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
int oldx, int oldy) {
if (oldy - y > 20) {
if (sliding == false) {
if (slideRL.getVisibility() == View.GONE) {
Animation animation = new TranslateAnimation(0, 0,
-hidenOptionsHeight, 0);
animation.setInterpolator(new AccelerateInterpolator(1.0f));
animation.setDuration(400);
slideRL.startAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
sliding = true;
slideRL.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
sliding = false;
if (firstTimeSlideUp) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
(int) hidenOptionsHeight);
FrameLayout extraLL = new FrameLayout(context);
extraLL.setBackgroundColor(getResources()
.getColor(android.R.color.white));
scrollLL.addView(extraLL, 0, params);
firstTimeSlideUp = false;
}
}
});
}
}
} else if (oldy - y < -20) {
if (sliding == false) {
Log.d("hiding view", "y " + y + " oldy " + oldy);
if (slideRL.getVisibility() == View.VISIBLE) {
Animation animation = new TranslateAnimation(0, 0, 0,
-hidenOptionsHeight);
animation.setInterpolator(new AccelerateInterpolator(1.0f));
animation.setDuration(400);
slideRL.startAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
sliding = true;
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
sliding = false;
slideRL.setVisibility(View.GONE);
}
});
}
}
}
}

android - onClickListener causes a reset of my animation

I have an Activity with some elements like ImageView, Button, ToggleButton, ... . And a subview (LinearLayout) that contains an HorizontalScrollView of ImageView.
The subview is an element that I want to hide / show with an animation.
My animation works successfully. But when I touch a ToggleButton or I apply a Filter, the subview is reseted and back to its origin position.
I have deduce that the subview is replaced in its origin when an element of the view visually change.
But I don't understand why...
The Activity class
public class CameraActivity extends Activity implements PictureCallback
{
private ToggleButton flashButton;
private Button filterScrollButton;
private LinearLayout filterScrollView;
private LinearLayout filterScrollViewLayout;
private Boolean filtersIsOpened = false;
private ImageView filterImageView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
flashButton = (ToggleButton)findViewById(R.id.button_flash);
filterScrollButton = (Button)findViewById(R.id.button_open_filters);
filterScrollView = (LinearLayout)findViewById(R.id.camera_scroll_filters);
filterScrollViewLayout = (LinearLayout)findViewById(R.id.camera_scroll_filters_layout);
}
...
private void initScrollFilters()
{
String[] filters = getResources().getStringArray(R.array.array_filters);
for (final String string : filters)
{
ImageView v = new ImageView(CameraActivity.this);
int imageFilterId = -1;
if (string != null && !string.isEmpty())
{
final int imageId = getResources().getIdentifier("#drawable/filter_" + string, null, getPackageName());
imageFilterId = getResources().getIdentifier("#drawable/filter_" + string, null, getPackageName());
v.setImageDrawable(getResources().getDrawable(imageId));
}
final int finalImageFilterId = imageFilterId;
v.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
Log.d(string + " filter image is touched");
CameraActivity.this.cameraManager.setImageFilter(finalImageFilterId); // Apply the new filter into filterImageView
}
});
filterScrollViewLayout.addView(v, 100, 100);
}
}
private void initListeners()
{
// Flash
flashButton.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// camera parameters is changed
}
});
// Filter scroll view
filterScrollButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Log.d("You click the filter scroll button men!!");
final float direction = (CameraActivity.this.filtersIsOpened) ? -1 : 1;
final float yDelta = -100;
final Animation animation = new TranslateAnimation(0, 0, 0, yDelta * direction);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation)
{
TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
anim.setDuration(1);
CameraActivity.this.filterScrollView.startAnimation(anim);
int top = (int) (CameraActivity.this.filterScrollView.getTop() + (yDelta * direction));
CameraActivity.this.filterScrollView.setTop(top);
}
});
animation.setDuration(500);
CameraActivity.this.filterScrollView.startAnimation(animation);
CameraActivity.this.filtersIsOpened = ! CameraActivity.this.filtersIsOpened;
}
});
}
...
}
The xml view
<FrameLayout 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"
tools:context=".CameraActivity" >
<CameraPreview
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/camera_preview_filter"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:rotation="90"
android:scaleType="center"
android:contentDescription="#string/content_desc_overlay" />
<LinearLayout
android:id="#+id/camera_scroll_filters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="-40dp"
android:paddingTop="40dp"
android:orientation="vertical"
android:clickable="false"
android:longClickable="false" >
<Button
android:id="#+id/button_open_filters"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_gravity="top|center_horizontal"
android:text="Filters" >
</Button>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF00FF00"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/camera_scroll_filters_layout"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:background="#FF000000" >
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:onClick="onCaptureClick"
android:text="#string/button_capture_text" />
</FrameLayout>

Categories

Resources