2014-11-15 28 views
0

我面臨一個奇怪的問題,我已經使用RelativeLayout生成了我的佈局文件。然而,當閱讀其中一個孩子的頂部時,它被返回爲零,這導致我的整個動畫邏輯進行折騰。RelativeLayout的小孩頂部即將爲零

我的想法是將它們從屏幕底部移動到初始位置。下面是我的代碼,來自你的任何幫助都將不勝感激。

我的佈局文件。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:paddingLeft="35dp" 
    android:paddingRight="35dp" > 

    <LinearLayout 
     android:id="@+id/msgs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="35dp" 
     android:layout_marginTop="25dp" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="20dp" 
      android:gravity="center_horizontal" 
      android:text="@string/share" 
      android:textColor="@android:color/black" 
      android:textSize="26dp" 
      android:typeface="monospace" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="48dp" 
      android:layout_gravity="top" 
      android:maxLines="3" 
      android:text="@string/share_message" 
      android:textColor="@android:color/black" 
      android:textSize="14dp" 
      android:typeface="sans" /> 
    </LinearLayout> 

     <ImageView 
     android:id="@+id/view_twitter" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_above="@id/msgs" 
     android:layout_marginEnd="25dp" 
     android:scaleType="fitCenter" 
     android:src="@drawable/circle_twitter" /> 


    <ImageView 
     android:id="@+id/view_facebook" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/msgs" 
     android:layout_marginEnd="25dp" 
     android:scaleType="fitCenter" 
     android:layout_toStartOf="@id/view_twitter" 
     android:src="@drawable/facebook_circle" /> 



    <ImageView 
     android:id="@+id/view_google" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/msgs" 
     android:layout_marginEnd="25dp" 
     android:layout_toEndOf="@id/view_twitter" 
     android:scaleType="fitCenter" 
     android:src="@drawable/google_circle" /> 

</RelativeLayout> 

我的片段代碼,其中正在動畫他們

import android.animation.Animator; 
import android.animation.Animator.AnimatorListener; 
import android.animation.AnimatorSet; 
import android.animation.ObjectAnimator; 
import android.graphics.Point; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.Display; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.animation.AccelerateDecelerateInterpolator; 

public class IntroFragmentThird extends Fragment{ 

    View google; 
    View twitter; 
    View facebook; 
    View messagePanel; 
    int screenHeight; 
    int screenWidth; 
    int offset; 
    float beginPoint; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     Display display = getActivity().getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     screenWidth = size.x; 
     screenHeight = size.y; 
     offset = screenHeight + 200; // start from 200px below screen height 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.intro3_layout, container, false); 
     google = view.findViewById(R.id.view_google); 
     facebook = view.findViewById(R.id.view_facebook); 
     twitter = view.findViewById(R.id.view_twitter); 
     messagePanel = view.findViewById(R.id.msgs); 

     google.setVisibility(View.GONE); // Initial view visibility will be gone 
     facebook.setVisibility(View.GONE); 
     twitter.setVisibility(View.GONE); 

     beginPoint = twitter.getTop(); // this is coming as ZERO 

     return view; 
    } 

    @Override 
    public void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     System.out.println("#### Top : " + beginPoint); 
     playScreenAnimation(); 
    } 

    public void playScreenAnimation(){ 

     facebook.clearAnimation(); 
     facebook.setVisibility(View.GONE); 

     facebook.clearAnimation(); 
     facebook.setVisibility(View.GONE); 

     facebook.clearAnimation(); 
     facebook.setVisibility(View.GONE); 




     ObjectAnimator anim1 = ObjectAnimator.ofFloat(facebook, "y", offset , beginPoint); 
     anim1.setInterpolator(new AccelerateDecelerateInterpolator()); 
     anim1.setDuration(1000); 
     anim1.setStartDelay(1000); 
     anim1.addListener(new AnimatorListener(){ 

      @Override 
      public void onAnimationStart(Animator animation) { 
       facebook.setVisibility(View.VISIBLE); 
      } 

      @Override 
      public void onAnimationEnd(Animator animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationCancel(Animator animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationRepeat(Animator animation) { 
       // TODO Auto-generated method stub 

      } 

     }); 

     ObjectAnimator anim2 = ObjectAnimator.ofFloat(twitter, "y", offset , beginPoint); 
     anim2.setInterpolator(new AccelerateDecelerateInterpolator()); 
     anim2.setDuration(1000); 
     anim2.addListener(new AnimatorListener(){ 

      @Override 
      public void onAnimationStart(Animator animation) { 
       twitter.setVisibility(View.VISIBLE); 

      } 

      @Override 
      public void onAnimationEnd(Animator animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationCancel(Animator animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationRepeat(Animator animation) { 
       // TODO Auto-generated method stub 

      } 

     }); 

     ObjectAnimator anim3 = ObjectAnimator.ofFloat(google, "y", offset , beginPoint); 
     anim3.setInterpolator(new AccelerateDecelerateInterpolator()); 
     anim3.setDuration(1000); 
     anim3.setStartDelay(1000); 
     anim3.addListener(new AnimatorListener(){ 

      @Override 
      public void onAnimationStart(Animator animation) { 
       google.setVisibility(View.VISIBLE); 
      } 

      @Override 
      public void onAnimationEnd(Animator animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationCancel(Animator animation) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onAnimationRepeat(Animator animation) { 
       // TODO Auto-generated method stub 

      } 

     }); 


     AnimatorSet set = new AnimatorSet(); 
     set.playTogether(anim2,anim1,anim3); 
     set.start(); 
    } 

} 

這裏是屏幕截圖,視圖(這三個FB,G +,微博界正在動畫)

enter image description here

初始狀態
+0

使用INVISIBLE insted GONE n試試... –

+0

確定讓我試試!! – Techfist

+0

沒有它沒有工作仍然頂部是作爲零@MSGadag – Techfist

回答

0

好吧我解決了它,而不是使用對象動畫,我結束了使用視圖動畫,並考慮到相對位置動畫視圖。

facebook.clearAnimation(); 
    facebook.setVisibility(View.INVISIBLE); 

    google.clearAnimation(); 
    google.setVisibility(View.INVISIBLE); 

    twitter.clearAnimation(); 
    twitter.setVisibility(View.INVISIBLE); 

    TranslateAnimation anim1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0 
      ,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0); 
    anim1.setDuration(1000); 
    anim1.setInterpolator(new AccelerateDecelerateInterpolator()); 
    anim1.setAnimationListener(new AnimationListener(){ 

     @Override 
     public void onAnimationStart(Animation animation) { 
      facebook.setVisibility(View.VISIBLE); 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

    }); 



    TranslateAnimation anim2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0 
      ,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0); 
    anim2.setDuration(750); 
    //anim2.setStartOffset(500); 
    anim2.setInterpolator(new AccelerateDecelerateInterpolator()); 
    anim2.setAnimationListener(new AnimationListener(){ 

     @Override 
     public void onAnimationStart(Animation animation) { 
      twitter.setVisibility(View.VISIBLE); 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

    }); 



    TranslateAnimation anim3 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0 
      ,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0); 
    anim3.setDuration(750); 
    anim3.setStartOffset(500); 
    anim3.setInterpolator(new AccelerateDecelerateInterpolator()); 
    anim3.setAnimationListener(new AnimationListener(){ 

     @Override 
     public void onAnimationStart(Animation animation) { 
      google.setVisibility(View.VISIBLE); 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

    }); 

    twitter.startAnimation(anim2); 
    facebook.startAnimation(anim1); 
    google.startAnimation(anim3); 
相關問題