在Android的过滤图像之间滑动

时间:2015-08-11 15:18:37

标签: java android android-viewpager imagefilter snapchat

基本上,我正在重新询问this question,但是要在android上实现它。

  

我正在尝试允许用户在静态图像上的过滤器之间滑动。   想法是在过滤器滚动时图像保持原位   它上面。 Snapchat最近发布了一个实现此功能的版本   特征。 This video shows exactly what I'm trying to accomplish at 1:05

我尝试使用叠加层填充列表并使用onFling和onDraw进行分页,但是我丢失了动画。有没有办法可以用ViewPager完成?

编辑:根据要求,我提供了覆盖视图分页的实现。它使用位于图像视图顶部的透明png图像填充viewpager。此外,此代码在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>

Fragment Overlay 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布局中。保存图像(我没有发布,因为它超出了这个问题的范围)很简单,它只是创建一个背景位图,一个覆盖位图,并使用画布将叠加层绘制到背景上,然后写入文件。

3 个答案:

答案 0 :(得分:1)

我自己也做过类似的事情。

对于您的特定用例,我只会使用画布和alpha混合滤镜,作为顶部图像。

要进行Alpha混合,请将第一张图像(原始图像)的alpha涂料设置为255,将第二张图像(过滤器)的alpha设置为128.

您只需要一个具有图像大小的滤镜,然后在绘制时移动第二个图像的位置。就是这样。

速度非常快,适用于非常非常旧的设备。

以下是一个示例实现:

    Bitmap filter,  // the filter
        original, // our original
        tempBitmap; // the bitmap which holds the canvas results 
                    // and is then drawn to the imageView
    Canvas mCanvas; // our canvas

    int x = 0;   // The x coordinate of the filter. This variable will be manipulated
                        // in either onFling or onScroll.
    void draw() {
        // clear canvas
        mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);    

        // setup paint
        paint0.setAlpha(255);   // the original needs to be fully visible
        paint1.setAlpha(128);   // the filter should be alpha blended into the original.

        // enable AA for paint
        // filter image
        paint1.setAntiAlias(true);
        paint1.setFlags(Paint.ANTI_ALIAS_FLAG);     // Apply AA to the image. Optional.
        paint1.setFlags(Paint.FILTER_BITMAP_FLAG);  // In case you scale your image, apple
                                                    // bilinear filtering. Optional.
        // original image
        paint0.setAntiAlias(true);
        paint0.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint0.setFlags(Paint.FILTER_BITMAP_FLAG);

        // draw onto the canvas    
        mCanvas.save();                

        mCanvas.drawBitmap(original, 0,0,paint0);
        mCanvas.drawBitmap(filter, x,0,paint1);    

        mCanvas.restore();

        // set the new image
        imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
    }

这是基本的onFlingonScroll实现。

private static final int SWIPE_DISTANCE_THRESHOLD = 125;
private static final int SWIPE_VELOCITY_THRESHOLD = 75;

// make sure to have implemented GestureDetector.OnGestureListener for these to work.
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    float distanceX = e2.getX() - e1.getX();
    float distanceY = e2.getY() - e1.getY();
    if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > 
            SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {

        // change picture to
        if (distanceX > 0) {
            // start left increment
        }
        else {  // the left
            // start right increment
        }
    }
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

    // checks if we're touching for more than 2f. I like to have this implemented, to prevent
    // jerky image motion, when not really moving my finger, but still touching. Optional.
    if (Math.abs(distanceY) > 2 || Math.abs(distanceX) > 2) {  
        if(Math.abs(distanceX) > Math.abs(distanceY)) {
            // move the filter left or right
        }
    }
}

注意:onScroll / onFling实现具有x调整的伪代码,因为需要测试这些函数。将来最终实现此功能的人可以随意编辑答案并提供这些功能。

答案 1 :(得分:0)

查看默认日历应用DayView的方法onDraw的实现。 根据动作变化有onFling实现和重绘内容(例如,日历网格),这模仿了投掷。

然后,您可以根据动作变化在onDraw中使用ColorFilter。它非常快。

或者,您可以将ViewSwitcher与过滤后的图像列表一起使用(或以某种方式创建过滤后的图像缓存)。为了实现“绘制图像”的可能性,您可以在ImageView中使用ViewSwitcherRelativeLayout一个在另一个之上,并在结束后在ImageView中设置新的过滤图像滚动。

答案 2 :(得分:-1)

对于这个应用程序,我觉得最容易使用androids动画功能并将动画值设置为您想要的过滤器。因此,您可以使用已更改的过滤器迭代数组来创建自己的动画。