2017-08-23 56 views
1

我正在製作Android應用程序,每當應用程序運行時,都會出現包含多個組件的活動屏幕。現在,這個活動有幾個不同的組件。其中一個是顯示來自畫廊的不同圖像的圖像視圖。目前,這些圖像通過觸摸手勢而改變。但我想要使用左右刷卡來實現相同的功能。這裏是我的代碼:使用左右滑動來更改圖像

import android.content.ContentResolver; 
import android.content.Context; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.graphics.BitmapFactory; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.provider.MediaStore.Images; 


import java.util.Collection; 

public class pictures extends ContentResolverAnimatedViewContainer 
{ 
    public static final String IMAGE_NAME = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera"; 
    public static final String IMAGE_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME); 
    public static String getBucketId(String path) 
    { 
     return String.valueOf(path.toLowerCase().hashCode()); } 
    static Context cc; 
    String cp; 
    int picheight; 

    public PhotoExample(final Context context) { 
     super(context, 9); 
     cc = context; 
     picheight = getResources().getDimensionPixelSize(R.dimen.photo_example_height); 
     final String[] projection = { Images.Media.DATA }; 
     final String selection = Images.Media.BUCKET_ID + " = ?"; 
     final String[] selectionArgs = { IMAGE_ID }; 
     createContentResolverLoop(Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null); } 
    @Override 
    public void onCreateData(ContentResolver contentResolver, Cursor dataCursor) { 
     final int column_data = dataCursor.getColumnIndexOrThrow(Images.Media.DATA); 
     cp = dataCursor.getString(column_data); } 

    @Override 
    public void onCreateViewContent(LayoutInflater layoutInflater, ViewGroup parentGroup, View[] containerViews, int index) { 
     containerViews[index] = layoutInflater.inflate(R.layout.example_photo, parentGroup, false); 
     ImageView photoView = (ImageView)containerViews[index].findViewById(R.id.photo); 
     photoView.setImageBitmap(decodeSampledBitmapFromFile(cp, 0, picheight)); } 

    public static Bitmap decodeSampledBitmapFromFile(String path, int Width, int Height) { 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 
     options.Size = calculateInSampleSize(options, Width, Height); 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeFile(path, options);} 

    public static int calculateInSampleSize(BitmapFactory.Options options, int Width, int Height) { 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int Size = 1; 

     if (height > reqHeight || width > reqWidth) { 
      final int height_half = height/2; 
      final int width_half = width/2; 

      while ((height_half/Size) > Height && (width_half/Size) > Width) { 
       Size = Size * 2; 
      } } 

     return Size; 
    } 


    @Override 
    public void cleanup() { } 

} 

任何想法,我應該如何實現?

謝謝。

回答

1

使用許多類似Tinder的可插拔卡片庫之一。 DuckDuckGo is your friend (or Google).

一些例子:

你有大量的選項可供選擇。

+0

這些可以在上面類本身來實現,或者我需要另一個活動? – Thomas

+0

您可以將它們添加到當前課程中。這些通常是自定義視圖,通常取代你的ImageView。查看每個庫的文檔以獲取更具體的信息。 –

+1

謝謝。我會看到每個圖書館的文檔。 – Thomas

2
import android.view.GestureDetector; 
import android.view.GestureDetector.SimpleOnGestureListener; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 

public class OnSwipeTouchListener implements OnTouchListener { 

private final GestureDetector gestureDetector; 

public OnSwipeTouchListener (Context ctx){ 
    gestureDetector = new GestureDetector(ctx, new GestureListener()); 
} 

private final class GestureListener extends SimpleOnGestureListener { 

    private static final int SWIPE_THRESHOLD = 100; 
    private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

    @Override 
    public boolean onDown(MotionEvent e) { 
     return true; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     boolean result = false; 
     try { 
      float diffY = e2.getY() - e1.getY(); 
      float diffX = e2.getX() - e1.getX(); 
      if (Math.abs(diffX) > Math.abs(diffY)) { 
       if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffX > 0) { 
         onSwipeRight(); 
        } else { 
         onSwipeLeft(); 
        } 
       } 
       result = true; 
      } 
      else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { 
        if (diffY > 0) { 
         onSwipeBottom(); 
        } else { 
         onSwipeTop(); 
        } 
       } 
       result = true; 

     } catch (Exception exception) { 
      exception.printStackTrace(); 
     } 
     return result; 
    } 
} 

public void onSwipeRight() { 
} 

public void onSwipeLeft() { 
} 

public void onSwipeTop() { 
} 

public void onSwipeBottom() { 
} 
} 

然後使用:

yourView.setOnTouchListener(new OnSwipeTouchListener() { 
public void onSwipeTop() { 
    Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show(); 
} 
public void onSwipeRight() { 
    Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show(); 
} 
public void onSwipeLeft() { 
    Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show(); 
} 
public void onSwipeBottom() { 
    Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show(); 
} 

public boolean onTouch(View v, MotionEvent event) { 
    return gestureDetector.onTouchEvent(event); 
} 
}); 
+0

如何在上面的代碼中實現這個功能? – Thomas

+0

創建一個GestureListener類,它是代碼的第一部分,代碼的第二部分代替yourView,將其替換爲您想要滑動的視圖(您的活動中的這一個) – karthik

+0

您能否提供相同的實現 – Thomas