Reputation: 863
I have a custom view in which I play GIF which is actually a Loader. Whenever I do background work, I set GIF View visibility to ON and set its visibility to gone when work is done. Following is my Custom GifLoader java code.
public class GifLoader extends View {
private InputStream gifInputStream;
private Movie gifMovie;
private int movieWidth, movieHeight;
private long movieDuration;
private long mMovieStart;
public GifLoader(Context context) {
super(context);
init(context);
}
public GifLoader(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GifLoader(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
setFocusable(true);
gifInputStream = context.getResources()
.openRawResource(R.drawable.loading);
gifMovie = Movie.decodeStream(gifInputStream);
movieWidth = gifMovie.width();
movieHeight = gifMovie.height();
movieDuration = gifMovie.duration();
}
@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
setMeasuredDimension(movieWidth, movieHeight);
}
public int getMovieWidth(){
return movieWidth;
}
public int getMovieHeight(){
return movieHeight;
}
public long getMovieDuration(){
return movieDuration;
}
@Override
protected void onDraw(Canvas canvas) {
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (gifMovie != null) {
int dur = gifMovie.duration();
if (dur == 0) {
dur = 100;
}
int relTime = (int)((now - mMovieStart) % dur);
gifMovie.setTime(relTime);
gifMovie.draw(canvas, 0, 0);
invalidate();
}
}
}
This is my XML code.
<yas.life.utils.GifLoader
android:visibility="gone"
android:layout_centerInParent="true"
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Now the problem is, GIFview is running while its visibility is gone, whenever I set its visibility to visible the progress is already running, not from its initial state. What I want to do is the GIFloader should start from its initial state. Is there anyway to restart this Custom GIFview whenever I set visibility to visible or any other way to achieve this.
Upvotes: 2
Views: 2813
Reputation: 483
You can check the view visibility state in onDraw()
and set gif time to 0 when view is gone
Upvotes: 0
Reputation: 2322
Try to redraw your view calling invalidate()
https://developer.android.com/reference/android/view/View.html#invalidate()
Upvotes: 3