本质上,我正在重新提出这个问题,但是要在Android上实现。
我试图允许用户在静态图像之间滑动筛选器。 这个想法是,图像保持不变,而过滤器在其上滚动。 Snapchat最近发布了一个实现此功能的版本。这个视频显示了我想在1:05实现的内容。
我尝试使用onFling填充覆盖层列表并进行分页,然后使用onDraw进行绘制, 但是我失去了动画效果。是否有一种方法可以使用ViewPager完成此操作?
编辑:按要求,我提供了我的叠加视图分页实现方式。它使用透明的png图像填充viewpager,并位于image view的顶部。此外,此代码采用C#编写,因为我正在使用Xamarin Android。对于不熟悉C#的人来说,它与Java非常相似。
...
static List<ImageView> overlayList = new List<ImageView>();
...
public class OverlayFragmentAdapter : FragmentPagerAdapter
{
public OverlayFragmentAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm)
{
}
public override int Count
{
get { return 5; } //hardcoded temporarily
}
public override Android.Support.V4.App.Fragment GetItem(int position)
{
return new OverlayFragment ();
}
}
public class OverlayFragment : Android.Support.V4.App.Fragment
{
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate (Resource.Layout.fragment_overlay, container, false);
LinearLayout l1 = view.FindViewById<LinearLayout> (Resource.Id.overlay_container);
ImageView im = new ImageView (Activity);
im.SetImageResource (Resource.Drawable.Overlay); //Resource.Drawable.Overlay is a simple png transparency I created. R
l1.AddView (im);
overlayList.AddElement (im);
return view;
}
}
活动布局XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<ImageView
android:id="@+id/background_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout <!-- This second layout is for buttons which I have omitted from this code -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/edit_layout">
<android.support.v4.view.ViewPager
android:id="@+id/overlay_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
片段叠加 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overlay_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" />
简单来说:viewpager位于第一个imageview之上,作为背景。OnCreateView方法从资源中创建覆盖片段和覆盖图像视图,并将它们放置在overlay_container布局中。保存图像(我没有发布它,因为它超出了这个问题的范围)很简单,它只是创建一个背景位图、一个覆盖位图,并使用画布将覆盖绘制到背景上,然后写入文件。