11

基本的に、私はこの質問を再質問していますが、アンドロイドに実装するためです。

ユーザーが静止画像のフィルター間をスワイプできるようにしようとしています。アイデアは、フィルターがその上をスクロールしている間、画像が所定の位置に留まるということです。Snapchat は最近、この機能を実装したバージョンをリリースしました。このビデオの 1:05 で、私が達成しようとしていることを正確に示しています。

リストをオーバーレイで埋めて、onFling でページングし、onDraw で描画しようとしましたが、アニメーションが失われます。ViewPager でこれを行う方法はありますか?

編集: 要求に応じて、オーバーレイ ビュー ページングの実装を提供しました。画像ビューの上にある透明なpng画像でviewpagerを埋めます。また、Xamarin Android を使用しているため、このコードは C# です。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 レイアウト内に配置します。画像の保存(この質問の範囲外であるため投稿していません)は簡単です。背景ビットマップ、オーバーレイビットマップを作成し、キャンバスを使用してオーバーレイを背景に描画し、次に書き込みますファイル。

4

3 に答える 3

1

私は自分自身で似たようなことに取り組んできました。

あなたの特定のユースケースでは、キャンバスを使用し、フィルターをアルファブレンドして、フリングで一番上の画像として使用します。

アルファ ブレンディングを行うには、最初のイメージ (オリジナル) のアルファ ペイントを 255 に設定し、2 番目のイメージ (フィルタ) のアルファ ペイントを 128 などに設定します。

画像のサイズのフィルターが必要なだけで、2番目の画像を描画するときにその位置をシフトします。それでおしまい。

非常に高速で、非常に古いデバイスでも問題なく動作します。

実装例は次のとおりです。

    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 調整用の疑似コードがあります。これらの関数はテストする必要があるためです。将来これを実装することになった人は、自由に回答を編集してそれらの機能を提供できます。

于 2015-08-20T10:43:05.760 に答える
-1

このアプリケーションでは、アンドロイドのアニメーション機能を使用して、アニメーションの値を必要なフィルターに設定するのが最も簡単だと思います。したがって、独自のアニメーションを作成して、配列を反復処理する変更されたフィルターを作成します。

于 2015-08-13T18:45:47.090 に答える