2012-06-19 10 views
6

Tôi đang cố gắng trượt trong một đoạn trong Android. Quá trình chuyển đổi và trượt hoạt động như mong đợi, nó trượt chính xác nơi tôi muốn. Tuy nhiên, trước khi xem được thực hiện, có một nền trắng, nơi quan điểm sẽ được ở vị trí cuối cùng của nó. Nó giống như nó đã được phân bổ không gian cho nó. Nhưng tôi muốn nó trượt qua các chế độ xem hiện tại của tôi, vì vậy nền trắng không được hiển thị. Bất kỳ ý tưởng về điều này? Tôi đang sử dụng trình xem hoạt ảnh với tệp xml trong res/anim.Nền trắng khi các đoạn hoạt ảnh trong android

Dưới đây là một mã nhỏ:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 

<translate 
    android:duration="100" 
    android:fromYDelta="100%" 
    android:toYDelta="0" /> 

<alpha 
    android:duration="100" 
    android:fromAlpha="0.0" 
    android:toAlpha="1.0" /> 

android.support.v4.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 

fragmentTransaction.setCustomAnimations(R.anim.slide_in_down, 0); 
fragmentTransaction.show(fragmentToSlideIn); 
fragmentTransaction.commit(); 

Vì vậy, tại sao lại có một nền trắng vào vị trí cuối cùng của xem, trước khi nó đã đạt đến vị trí đó?

+0

u tìm thấy giải pháp cho việc này? ngay cả khi tôi đang gặp phải vấn đề tương tự – Mohit

Trả lời

0

http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator Bạn nên sử dụng objectAnimator, đây là mẫu mã trượt đoạn từ phải sang trái:

private View mPanel1; 
    private View mPanel2; 
    private View mLayout; 
    boolean isCollapsed; 
    private int LayoutWidth; 
    private int Panel1Width; 
    private int Panel2Width; 
    private static final TimeInterpolator sCollapseInterpolator = new DecelerateInterpolator(2.5F); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mLayout = findViewById(R.id.linearlayout); 
    mPanel1 = findViewById(R.id.fragment1); 
    mPanel2 = findViewById(R.id.fragment2); 
} 
    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
     // Measure width of layout and mPanel1 which is FragmentList. This is needed for ObjectAnimator 
     // to create transition animation 
     Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     LayoutWidth = size.x; 
     Panel1Width = mPanel1.getWidth(); 
     Panel2Width = mPanel2.getWidth(); 

    } 
    public void slide(){ 
     if(isCollapsed){ 
         PropertyValuesHolder[] arrayOfPropertyValuesHolder = new PropertyValuesHolder[2]; 
         arrayOfPropertyValuesHolder[0] = PropertyValuesHolder.ofInt("PanelLeft", Panel1Width * -1, 0); 
         arrayOfPropertyValuesHolder[1] = PropertyValuesHolder.ofInt("Panel2W", LayoutWidth, LayoutWidth - Panel1Width); 
         ObjectAnimator localObjectAnimator = ObjectAnimator.ofPropertyValuesHolder(this, arrayOfPropertyValuesHolder).setDuration(400); 
         localObjectAnimator.setInterpolator(sCollapseInterpolator); 
         localObjectAnimator.start(); 
        } 
        else{ 
         PropertyValuesHolder[] arrayOfPropertyValuesHolder = new PropertyValuesHolder[2]; 
         arrayOfPropertyValuesHolder[0] = PropertyValuesHolder.ofInt("PanelLeft", 0, Panel1Width/-1); 
         arrayOfPropertyValuesHolder[1] = PropertyValuesHolder.ofInt("Panel2W", LayoutWidth - Panel1Width, LayoutWidth); 
         ObjectAnimator localObjectAnimator = ObjectAnimator.ofPropertyValuesHolder(this, arrayOfPropertyValuesHolder).setDuration(400); 
         localObjectAnimator.setInterpolator(sCollapseInterpolator); 
         localObjectAnimator.start(); 
        } 
        isCollapsed = !isCollapsed; 
    } 

     public int getPanelLeft() { 
      return ((ViewGroup.MarginLayoutParams) mLayout.getLayoutParams()).leftMargin; 
     } 

     public void setPanelLeft(int paramInt) { 
      ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mLayout.getLayoutParams(); 
      lp.leftMargin = paramInt; 
      mLayout.setLayoutParams(lp); 
     } 

     public int getPanel2W() { 
      return ((ViewGroup.MarginLayoutParams) mPanel2.getLayoutParams()).width; 
     } 

     public void setPanel2W(int paramInt) { 
      ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mPanel2.getLayoutParams(); 
      lp.width = paramInt; 
      mPanel2.setLayoutParams(lp); 
     } 

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/linearlayout" > 

    <fragment 
     android:id="@+id/fragment1" 
     android:name="package.your.fragment1" 
     android:layout_width="320dp" 
     android:layout_height="match_parent"/> 

<fragment 
     android:id="@+id/fragment2" 
     android:name="package.your.fragment2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 
+1

Lưu ý rằng [ObjectAnimator] (http://developer.android.com/reference/android/animation/ObjectAnimator.html) chỉ được thêm vào trong ** API Level 11 **. – jenzz

+0

Bạn đã tìm được giải pháp cho điều đó chưa? Tôi có cùng một vấn đề với quan điểm bình thường. –