简体   繁体   English

Android视差头效果

[英]Android parallax header effect

I want to recreate UI features such as the new G+ app, or the airbnb interface where the parallax effect is invoked solemnly on the header picture. 我想重新创建UI功能,例如新的G +应用程序,或在标题图片上庄重调用视差效果的airbnb界面。 My app uses (both) ListView and Gridview under different constraints, I was going over @CFlex tutorial for recreating it but couldn't achieve the effect. 我的应用程序在不同的约束下使用了ListView和Gridview(两者),我遍历了@CFlex 教程以重新创建它,但无法达到效果。

Main.axml: 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: 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"/>

The app is running on mono powered by Xamarin's framework. 该应用程序在Xamarin框架支持的mono上运行。 Any help's appreciated! 任何帮助表示赞赏!

我制作了一个满足您需要的开源库-https: //github.com/nirhart/ParallaxScroll,您可以在此处查看示例-https: //play.google.com/store/apps/details?id=com .nirhart.parallaxscrollexample

I think the easiest way to do parallax with a header is the next: 我认为使用标头进行视差的最简单方法是下一个:

-Use ObservableScrollView that contains inside an ImageView that is your header image. -使用ObservableScrollView,它包含在ImageView内部,该图像是您的标题图像。 -Afterwards, on your java code of the activity that uses this layout, manage the ScrollView behaviour, by implementing ScrollViewCallBacks. -然后,在使用该布局的活动的Java代码上,通过实现ScrollViewCallBacks来管理ScrollView行为。 And finally, to achieve the parallax effect, use imageView.setTranslation, like next: 最后,要实现视差效果,请使用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) {

        }
    });

Pay special attention to the attribute "i" that is the value of the scrolling movement. 请特别注意滚动运动值“ i”。 And use 0.5f or more to reach the proper effect during scrolling time. 并在滚动期间使用0.5f或更大的分辨率以达到适当的效果。 Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM