2014-02-20 3 views
0

Я знаю, как оживить вид на весь экран и оживить его первоначальный размер. вот ссылка для этого Re sizing through animationанимировать представление из определенной точки, чтобы заполнить весь экран, и размер его обратно в эту точку

Но проблема в том, что эта техника работает только тогда, когда мой взгляд находится в начале экрана.

Что я хочу, так это то, что если мой взгляд на высоту и ширину (50,50) помещается в центр экрана под какой-то кнопкой, и я нажимаю этот вид, он должен анимировать, чтобы заполнить весь экран, а при повторном нажатии он оживляет назад к его первоначальному размеру (50,50)

ответ

5

Если вы используете LinearLayout, попробуйте установить AnimationListener для этой анимации и соответствующим образом переключить видимость кнопки. Установите видимость кнопки на View.GONE onAnimationStart, когда «расширяется» вид, и View.VISIBLE onAnimationEnd, когда он «сворачивает» его. Использование RelativeLayout также может быть решением этой проблемы.

Для анимации:

public class ExpandCollapseViewAnimation extends Animation { 
int targetWidth; 
int targetHeight; 
int initialWidth; 
int initialHeight; 
boolean expand; 
View view; 

public ExpandCollapseViewAnimation(View view, int targetWidth, int targetHeight ,boolean expand) { 
    this.view = view; 
    this.targetWidth = targetWidth; 
    this.targetHeight = targetHeight; 
    this.initialWidth = view.getWidth(); 
    this.initialHeight = view.getHeight(); 
    this.expand = expand; 
} 

@Override 
protected void applyTransformation(float interpolatedTime, Transformation t) { 
    int newWidth, newHeight; 
    if (expand) { 
     newWidth = this.initialWidth 
       + (int) ((this.targetWidth - this.initialWidth) * interpolatedTime); 
     newHeight = this.initialHeight 
       + (int) ((this.targetHeight - this.initialHeight) * interpolatedTime); 
    } else { 
     newWidth = this.initialWidth 
       - (int) ((this.initialWidth - this.targetWidth) * interpolatedTime); 
     newHeight = this.initialHeight 
       - (int) ((this.initialHeight - this.targetHeight) * interpolatedTime); 
    } 

    view.getLayoutParams().width = newWidth; 
    view.getLayoutParams().height = newHeight; 
    view.requestLayout(); 
} 

@Override 
public void initialize(int width, int height, int parentWidth, 
     int parentHeight) { 
    super.initialize(width, height, parentWidth, parentHeight); 
} 

@Override 
public boolean willChangeBounds() { 
    return true; 
} 

} 

и расположение XML:

<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="match_parent" 
android:orientation="vertical"> 

<Button android:id="@+id/btn" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_gravity="center_horizontal" 
    android:text="Test"/> 


<View android:id="@+id/centered_view" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:clickable="true" 
    android:layout_gravity="center_horizontal" 
    android:background="#FF0000"/> 

</LinearLayout> 

Этот код работает:

animatedView.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if (!expand) expandView(); 
      else collapseView(); 
     } 
    }); 

private void expandView() { 
    expand = true; 
    animatedView.clearAnimation(); 
    Display display = this.getWindowManager().getDefaultDisplay(); 
    int maxWidth = display.getWidth(); 
    int maxHeight = display.getHeight(); 
    ExpandCollapseViewAnimation animation = new ExpandCollapseViewAnimation(animatedView, maxWidth,maxHeight, expand); 
    animation.setDuration(500); 
    animation.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 
      btn.setVisibility(View.GONE); 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) {  
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
     } 
    }); 
    animatedView.startAnimation(animation); 
    animatedView.invalidate(); 
} 

private void collapseView() { 
    expand = false; 
    animatedView.clearAnimation(); 
    ExpandCollapseViewAnimation animation = new ExpandCollapseViewAnimation(
      animatedView, dpToPx(this, 50),dpToPx(this, 50), expand); 
    animation.setDuration(500); 
    animation.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      btn.setVisibility(View.VISIBLE); 
     } 
    }); 
    animatedView.startAnimation(animation); 
    animatedView.invalidate(); 
} 
+0

Благодаря братан ты почти решена моя problem.I будет голосовать как ответ, когда я получу достаточную репутацию – hussi

Смежные вопросы