Created
October 10, 2017 13:35
-
-
Save Redman1037/416bef8bf6f9dddc265dc755b9a4e616 to your computer and use it in GitHub Desktop.
GIF ProgressDialog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.eightfolds.sampleprogress; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Movie; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import java.io.IOException; | |
import java.io.InputStream; | |
public class EasyGifView extends View { | |
private Movie mMovie; | |
private long lngMovie; | |
private int intId = 0; | |
private long lngWidth = 0; | |
private long lngHeight = 0; | |
private long lngDuration = 0; | |
private float ratioWidth; | |
private float ratioHeight; | |
public EasyGifView(Context context) throws IOException { | |
super(context); | |
} | |
public EasyGifView(Context context, AttributeSet attrSet) throws IOException{ | |
super(context, attrSet); | |
} | |
public EasyGifView(Context context, AttributeSet attrSet, int newStyle) throws IOException { | |
super(context, attrSet, newStyle); | |
} | |
public void setGifFromResource(int resourceId) | |
{ | |
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null); | |
InputStream inputStream = getContext().getResources().openRawResource(resourceId); | |
mMovie = Movie.decodeStream(inputStream); | |
this.intId = resourceId; | |
this.lngWidth = mMovie.width(); | |
this.lngHeight = mMovie.height(); | |
this.lngDuration = mMovie.duration(); | |
} | |
public int getGifFromResource(){ | |
return intId; | |
} | |
public long getGifWidth(){ | |
return lngWidth; | |
} | |
public long getGifHeight(){ | |
return lngHeight; | |
} | |
public long getGifDuration(){ | |
return lngDuration; | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
if (mMovie != null) { | |
int width; | |
int height; | |
width = mMovie.width(); | |
height = mMovie.height(); | |
if (width <= 0) width = 1; | |
if (height <= 0) height = 1; | |
int paddingLeft = getPaddingLeft(); | |
int paddingRight = getPaddingRight(); | |
int paddingTop = getPaddingTop(); | |
int paddingBottom = getPaddingBottom(); | |
int widthSize; | |
int heightSize; | |
width += paddingLeft + paddingRight; | |
height += paddingTop + paddingBottom; | |
width = Math.max(width, getSuggestedMinimumWidth()); | |
height = Math.max(height, getSuggestedMinimumHeight()); | |
widthSize = resolveSizeAndState(width, widthMeasureSpec, 0); | |
heightSize = resolveSizeAndState(height, heightMeasureSpec, 0); | |
ratioWidth = (float) widthSize / width; | |
ratioHeight = (float) heightSize / height; | |
setMeasuredDimension(widthSize, heightSize); | |
} else { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
if (mMovie == null) { | |
return; | |
} | |
long lngCurrentTime = android.os.SystemClock.uptimeMillis(); | |
if (lngMovie == 0) { | |
lngMovie = lngCurrentTime; | |
} | |
int rTime = (int)((lngCurrentTime - lngMovie) % mMovie.duration()); | |
mMovie.setTime(rTime); | |
canvas.scale(Math.min(ratioWidth, ratioHeight), Math.min(ratioWidth, ratioHeight)); | |
mMovie.draw(canvas,0,0); | |
this.invalidate(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.eightfolds.sampleprogress; | |
import android.app.Dialog; | |
import android.content.Context; | |
import android.graphics.Color; | |
import android.graphics.drawable.ColorDrawable; | |
import android.view.Gravity; | |
import android.view.ViewGroup; | |
import android.widget.LinearLayout; | |
import android.widget.RelativeLayout; | |
import java.io.IOException; | |
/** | |
* Created by Manohar on 28/9/17. | |
*/ | |
public class MyProgressDialog { | |
Dialog dialog; | |
EasyGifView easyGifView; | |
public MyProgressDialog(Context context) { | |
dialog = new Dialog(context); | |
dialog.setCancelable(false); | |
RelativeLayout relativeLayout = new RelativeLayout(context); | |
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); | |
relativeLayout.setLayoutParams(layoutParams); | |
try { | |
easyGifView = new EasyGifView(context); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
RelativeLayout.LayoutParams layoutParams_progress = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); | |
layoutParams_progress.addRule(RelativeLayout.CENTER_IN_PARENT); | |
LinearLayout.LayoutParams linearlayoutParams_progress = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); | |
linearlayoutParams_progress.gravity = Gravity.CENTER; | |
easyGifView.setLayoutParams(layoutParams_progress); | |
relativeLayout.addView(easyGifView); | |
dialog.getWindow().setContentView(relativeLayout, layoutParams); | |
dialog.getWindow().setBackgroundDrawable( | |
new ColorDrawable(Color.TRANSPARENT)); | |
} | |
public void setGif(int resourceId) { | |
easyGifView.setGifFromResource(resourceId); | |
} | |
public void show() { | |
if (!dialog.isShowing() && dialog != null) { | |
dialog.show(); | |
} | |
} | |
public void dismiss() { | |
if (dialog.isShowing() && dialog != null) { | |
dialog.dismiss(); | |
} | |
} | |
public void setCancelable(boolean cancelable) { | |
dialog.setCancelable(cancelable); | |
} | |
public void setCanceledOnTouchOutside(boolean flag) { | |
dialog.setCanceledOnTouchOutside(flag); | |
} | |
public boolean isShowing() { | |
return dialog.isShowing(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copy MyProgressDialog ,EasyGifView to project | |
MyProgressDialog dialog=new MyProgressDialog(this); | |
dialog.setGif(R.drawable.loading); //set your gif here | |
dialog.show(); //to show dialog | |
dialog.dismiss(); //to dismiss dialog |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment