2014-01-27 136 views
0

我想做一个TranslateAnimation但不能正常工作布局apear和disapear但没有做任何动画。我怎么能解决这个问题? Java代码是这样的:滑出滚动布局动画android

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; 
       } 
      }); 

     } 
    } 

} 

布局是这样的:

<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> 

我没有找到任何解决方案。我希望它能像谷歌搜索应用程序底部显示的选项一样工作。

回答

0

的问题是,当我问对于第一次的高度视图是不可见的高度的值是0:

我这样做:

ImageButton minusLetterIB = (ImageButton) findViewById(R.id.post_view_activity_minus_leters_ib); 

    hidenOptionsHeight = minusLetterIB.getDrawable().getIntrinsicHeight(); 

,改变了动画隐藏显示的布局:

@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); 
        } 
       }); 
      } 
     } 

    } 

} 
1
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);