Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smooth scrolling of images #585

Open
sermyagin opened this issue Aug 28, 2024 · 0 comments
Open

Smooth scrolling of images #585

sermyagin opened this issue Aug 28, 2024 · 0 comments

Comments

@sermyagin
Copy link

How can I implement smooth movement (scrolling with preview) .

`import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class ImageViewerActivity extends AppCompatActivity {
private ImageView previewImageView;
private SubsamplingScaleImageView imageView;
private String mediaPath;
private List mediaPaths2;
private Uri mediaUri;
private LinearLayout photoButtonBar;
private SharedPreferences sharedPreferences;
private static final int DELETE_REQUEST_CODE = 1001;
private static final int REQUEST_CODE_OPEN_DOCUMENT = 1;

private static final int RENAME_REQUEST_CODE = 1002;
private Uri selectedUri;
private ImageView backButton;
private static final String DIALOG_STATE_KEY = "dialog_state_key";
private boolean isDialogShowing = false;
private GestureDetector gestureDetector;
private int currentIndex;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_viewer);

    if (savedInstanceState != null) {
        isDialogShowing = savedInstanceState.getBoolean(DIALOG_STATE_KEY, false);
    }

    sharedPreferences = getSharedPreferences(KEY.PREFS_NAME, MODE_PRIVATE);
    if (sharedPreferences.getBoolean(KEY.KEY_BRIGHTNESS, false)) {
        setBrightnessToMax();
    }

    photoButtonBar = findViewById(R.id.photo_button_bar);
    imageView = findViewById(R.id.image_view);
    backButton = findViewById(R.id.imageback);
    backButton.setOnClickListener(v -> finish());

    // Initialize GestureDetector and set onTouchListener for the imageView
    gestureDetector = new GestureDetector(this, new SwipeGestureDetector());
    imageView.setOnTouchListener((v, event) -> gestureDetector.onTouchEvent(event));

    // Retrieve the list of media paths from the Intent
    mediaPaths2 = getIntent().getStringArrayListExtra("MEDIA_PATHS");

    if (mediaPaths2 == null || mediaPaths2.isEmpty()) {
        Toast.makeText(this, "No media paths provided", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    // Получаем текущий путь медиафайла
    mediaPath = getIntent().getStringExtra("MEDIA_PATH");
    if (mediaPath != null) {
        currentIndex = mediaPaths2.indexOf(mediaPath);

        if (currentIndex == -1) {
            Toast.makeText(this, "Media path not found in list", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
    } else {
        Toast.makeText(this, "Media path not provided", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    // Продолжайте с загрузкой изображения
    mediaUri = getMediaUri(mediaPath);
    if (mediaUri != null) {
        displayImage(mediaUri);
    } else {
        Toast.makeText(this, "Image URI not found", Toast.LENGTH_SHORT).show();
        finish();
    }

    imageView.setOnClickListener(v -> togglePhotoButtonBarVisibility());

    if (isDialogShowing) {
        showPhotoInfo();
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
}

private class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener {
    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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


private void onSwipeRight() {
    if (currentIndex > 0) {
        currentIndex--;
        mediaUri = getMediaUri(mediaPaths2.get(currentIndex));
        displayImage(mediaUri);
    } else {
        Toast.makeText(this, "No previous media", Toast.LENGTH_SHORT).show();
    }
}

private void onSwipeLeft() {
    if (currentIndex < mediaPaths2.size() - 1) {
        currentIndex++;
        mediaUri = getMediaUri(mediaPaths2.get(currentIndex));
        displayImage(mediaUri);
    } else {
        Toast.makeText(this, "No next media", Toast.LENGTH_SHORT).show();
    }
}


private void displayImage(Uri uri) {
    imageView.setImage(ImageSource.uri(uri));
}`

`

<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
    android:id="@+id/image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />



<include
    android:id="@+id/photo_button_bar"
    layout="@layout/photo_view_button_bar"
    android:visibility="gone" />
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant