Skip to content

Commit

Permalink
Merge pull request #3 from shts/feature/add-pause-resume
Browse files Browse the repository at this point in the history
pause and resume impl
  • Loading branch information
shts committed Oct 23, 2017
2 parents 706922b + 692881c commit e73a02b
Show file tree
Hide file tree
Showing 23 changed files with 313 additions and 146 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,6 +3,7 @@
/local.properties
/.idea/workspace.xml
/.idea/libraries
/.idea/dictionaries
.DS_Store
/build
/captures
Expand Down
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions README.md
Expand Up @@ -64,16 +64,26 @@ public class YourActivity extends AppCompatActivity implements StoriesProgressVi
}
```

Skip story
Skip and Reverse story
---

<img src="image/skip-reverse.gif" width=200 />
<img src="image/skip-reverse.gif" width=200 />

```java
storiesProgressView.skip();
storiesProgressView.reverse();
storiesProgressView.reverse();
```

Pause and Resume story
---
<img src="image/pause-resume.gif" width=200 />

```java
storiesProgressView.pause();
storiesProgressView.resume();
```


Install
---

Expand All @@ -93,7 +103,7 @@ Add the dependency

```
dependencies {
compile 'com.github.shts:StoriesProgressView:1.2.0'
compile 'com.github.shts:StoriesProgressView:2.0.0'
}
```
Expand All @@ -102,7 +112,7 @@ License
---

```
Copyright (C) 2017 Shota Saito
Copyright (C) 2017 Shota Saito(shts)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down

This file was deleted.

Expand Up @@ -2,7 +2,9 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import jp.shts.android.storiesprogressview.StoriesProgressView;
Expand All @@ -24,30 +26,45 @@ public class MainActivity extends AppCompatActivity implements StoriesProgressVi
R.drawable.sample6,
};

// private final long[] durations = new long[]{
// 500L, 1000L, 1500L, 4000L, 5000L, 1000,
// };
private final long[] durations = new long[]{
500L, 1000L, 1500L, 4000L, 5000L, 1000,
};

long pressTime = 0L;
long limit = 500L;

private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
pressTime = System.currentTimeMillis();
storiesProgressView.pause();
return false;
case MotionEvent.ACTION_UP:
long now = System.currentTimeMillis();
storiesProgressView.resume();
return limit < now - pressTime;
}
return false;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

storiesProgressView = (StoriesProgressView) findViewById(R.id.stories);
storiesProgressView.setStoriesCount(PROGRESS_COUNT);
storiesProgressView.setStoryDuration(1200L);
storiesProgressView.setStoryDuration(3000L);
// or
// storiesProgressView.setStoriesCountWithDurations(durations);
storiesProgressView.setStoriesListener(this);
storiesProgressView.startStories();

image = (ImageView) findViewById(R.id.image);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
storiesProgressView.skip();
}
});
image.setImageResource(resources[counter]);

// bind reverse view
Expand All @@ -58,6 +75,7 @@ public void onClick(View v) {
storiesProgressView.reverse();
}
});
reverse.setOnTouchListener(onTouchListener);

// bind skip view
View skip = findViewById(R.id.skip);
Expand All @@ -67,6 +85,7 @@ public void onClick(View v) {
storiesProgressView.skip();
}
});
skip.setOnTouchListener(onTouchListener);
}

@Override
Expand All @@ -76,7 +95,7 @@ public void onNext() {

@Override
public void onPrev() {
if (counter - 1 < 0) return;
if ((counter - 1) < 0) return;
image.setImageResource(resources[--counter]);
}

Expand Down
Binary file modified app/src/main/res/drawable/sample1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable/sample2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable/sample3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable/sample4.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable/sample5.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/drawable/sample6.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Expand Up @@ -32,6 +32,8 @@

<jp.shts.android.storiesprogressview.StoriesProgressView
android:id="@+id/stories"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_gravity="top"
Expand Down

This file was deleted.

Binary file modified image/capture.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified image/image.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/pause-resume.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified image/skip-reverse.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,173 @@
package jp.shts.android.storiesprogressview;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.Transformation;
import android.widget.FrameLayout;

final class PausableProgressBar extends FrameLayout {

/***
* progress満了タイマーのデフォルト時間
*/
private static final int DEFAULT_PROGRESS_DURATION = 2000;

private View frontProgressView;
private View maxProgressView;

private PausableScaleAnimation animation;
private long duration = DEFAULT_PROGRESS_DURATION;
private Callback callback;

interface Callback {
void onStartProgress();
void onFinishProgress();
}

public PausableProgressBar(Context context) {
this(context, null);
}

public PausableProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public PausableProgressBar(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater.from(context).inflate(R.layout.pausable_progress, this);
frontProgressView = findViewById(R.id.front_progress);
maxProgressView = findViewById(R.id.max_progress); // work around
}

public void setDuration(long duration) {
this.duration = duration;
}

public void setCallback(@NonNull Callback callback) {
this.callback = callback;
}

void setMax() {
finishProgress(true);
}

void setMin() {
finishProgress(false);
}

void setMinWithoutCallback() {
maxProgressView.setBackgroundResource(R.color.progress_secondary);

maxProgressView.setVisibility(VISIBLE);
if (animation != null) {
animation.setAnimationListener(null);
animation.cancel();
}
}

private void finishProgress(boolean isMax) {
if (isMax) maxProgressView.setBackgroundResource(R.color.progress_max_active);
maxProgressView.setVisibility(isMax ? VISIBLE : GONE);
if (animation != null) {
animation.setAnimationListener(null);
animation.cancel();
if (callback != null) {
callback.onFinishProgress();
}
}
}

public void startProgress() {
maxProgressView.setVisibility(GONE);

animation = new PausableScaleAnimation(0, 1, 1, 1, Animation.ABSOLUTE, 0, Animation.RELATIVE_TO_SELF, 0);
animation.setDuration(duration);
animation.setInterpolator(new LinearInterpolator());
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
frontProgressView.setVisibility(View.VISIBLE);
if (callback != null) callback.onStartProgress();
}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
if (callback != null) callback.onFinishProgress();
}
});
animation.setFillAfter(true);
frontProgressView.startAnimation(animation);
}

public void pauseProgress() {
if (animation != null) {
animation.pause();
}
}

public void resumeProgress() {
if (animation != null) {
animation.resume();
}
}

void clear() {
if (animation != null) {
animation.setAnimationListener(null);
animation.cancel();
animation = null;
}
}

private class PausableScaleAnimation extends ScaleAnimation {

private long mElapsedAtPause = 0;
private boolean mPaused = false;

PausableScaleAnimation(float fromX, float toX, float fromY,
float toY, int pivotXType, float pivotXValue, int pivotYType,
float pivotYValue) {
super(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType,
pivotYValue);
}

@Override
public boolean getTransformation(long currentTime, Transformation outTransformation, float scale) {
if (mPaused && mElapsedAtPause == 0) {
mElapsedAtPause = currentTime - getStartTime();
}
if (mPaused) {
setStartTime(currentTime - mElapsedAtPause);
}
return super.getTransformation(currentTime, outTransformation, scale);
}

/***
* pause animation
*/
void pause() {
if (mPaused) return;
mElapsedAtPause = 0;
mPaused = true;
}

/***
* resume animation
*/
void resume() {
mPaused = false;
}
}
}

7 comments on commit e73a02b

@langaliamayank
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello is it possible to add .mp4 format ? If yes then how to achieve this please help me for the same.

@AnirudhLoya
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any success with .mp4?

@langaliamayank
Copy link

@langaliamayank langaliamayank commented on e73a02b Jan 25, 2018 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnirudhLoya
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MayankSoni Can you help with the code or push your changes?

@langaliamayank
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnirudhLoya I have done few changes but sorry its not free.

@gulzar1996
Copy link

@gulzar1996 gulzar1996 commented on e73a02b Jan 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnirudhLoya I have added video and image support it has some issues but it works and yes its open source https://github.com/gulzar1996/InstaStories

@AnirudhLoya
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gulzar1996 Really appreciate your work & contribution. Its fun people use open source code from github do some changes & and then don't want to contribute back to the community.

Please sign in to comment.