2013-12-17 134 views
1

我想要重新創建UI功能,例如新的G +應用程序,或者在標題圖片上嚴肅地調用視差效果的airbnb界面。 我的應用程序在不同的約束條件下使用(兩者)ListView和Gridview,我正在通過@CFlex tutorial重新創建它,但無法達到效果。Android視差標頭效果

Main.axml:

namespace ParallaxTest 
{ 
[Activity(Label = "ParallaxTest", MainLauncher = true, Icon = "@drawable/icon")] 
public class ParallaxTest : Activity, AbsListView.IOnScrollListener 
{ 
    int count = 1; 

    public GridView _gridView; 
    public CustomImageView _imageView; 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     // Get our button from the layout resource, 
     // and attach an event to it 
     _gridView = new GridView(this) 
     { 
      VerticalFadingEdgeEnabled = false, 
      LayoutParameters = 
       new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 
        LinearLayout.LayoutParams.MatchParent), 
      CacheColorHint = Color.Transparent, 
     }; 
     _gridView.SetOnScrollListener(this); 
     _gridView.Adapter = new GridViewAdapter(); 
     _gridView.VerticalFadingEdgeEnabled = false; 
     _gridView.SetColumnWidth(Resources.DisplayMetrics.WidthPixels/2); 
     _gridView.SetNumColumns(2); 
     _gridView.SetVerticalSpacing(2); 
     _gridView.SetHorizontalSpacing(2); 
     _gridView.StretchMode = StretchMode.NoStretch; 
     _gridView.SetGravity(GravityFlags.Center); 
     _gridView.SetBackgroundColor(Color.Pink); 
     _gridView.SetOnScrollListener(this); 
     _imageView = new CustomImageView(this) 
     { 
      LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MatchParent, 400, GravityFlags.Center) 
     }; 
     _imageView.SetBackgroundColor(Color.White); 
     FindViewById<FrameLayout>(Resource.Id.top_picture).AddView(_imageView); 
     FindViewById<LinearLayout>(Resource.Id.items).AddView(_gridView); 


    } 

    public void OnScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
    { 
     if (visibleItemCount == 0) return; 
     if (firstVisibleItem != 0) return; 
     _imageView.SetCurrentTranslation(_gridView.GetChildAt(0).Top); 
    } 

    public void OnScrollStateChanged(AbsListView view, ScrollState scrollState) 
    { 
     //throw new NotImplementedException(); 
    } 
} 

public class CustomImageView : ImageView 
{ 
    protected CustomImageView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) 
    { 
    } 

    public CustomImageView(Context context) : base(context) 
    { 
    } 

    public CustomImageView(Context context, IAttributeSet attrs) : base(context, attrs) 
    { 
    } 

    public CustomImageView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) 
    { 
    } 

    public int mCurrentTranslation; 

    public void SetCurrentTranslation(int currentTranslation) { 
     mCurrentTranslation = currentTranslation; 
     Invalidate(); 
    } 

    public override void Draw(Canvas canvas) 
    { 
     canvas.Save(); 
     canvas.Translate(0, -mCurrentTranslation/2); 
     base.Draw(canvas); 
     canvas.Restore(); 
    } 

} 

}

Main.axml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#000000" 
      android:id="@+id/items"> 
<FrameLayout 
android:id="@+id/top_picture" 
android:layout_width="match_parent" 
android:layout_height="300dp" 
android:background="#ffffff"/> 

該應用在單電由Xamarin的框架運行。 任何幫助的讚賞!

回答

0

我認爲做視差的頭最簡單的方法是下一個:

- 使用包含在ImageView中的ObservableScrollView,它是您的標題圖像。 - 之後,在您使用此佈局的活動的java代碼中,通過實現ScrollViewCallBacks來管理ScrollView行爲。最後,實現了視差效果,使用imageView.setTranslation,旁邊似:

scrollView.setScrollViewCallbacks(new ObservableScrollViewCallbacks() { 
     @Override 
     public void onScrollChanged(int i, boolean b, boolean b1) { 
      imageView.setTranslationY(i*0.5f); 
     } 

     @Override 
     public void onDownMotionEvent() { 

     } 

     @Override 
     public void onUpOrCancelMotionEvent(ScrollState scrollState) { 

     } 
    }); 

要特別注意屬性「我」,也就是滾動運動的價值。在滾動時間內使用0.5f或更多來達到適當的效果。希望它有幫助