Subversion Repositories SmartDukaan

Rev

Rev 21569 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21569 Rev 22122
Line 1... Line 1...
1
package com.saholic.profittill;
1
package com.saholic.profittill;
2
 
2
 
3
import android.content.Context;
3
import android.content.Context;
4
import android.graphics.Canvas;
4
import android.graphics.Canvas;
5
import android.graphics.Color;
-
 
6
import android.graphics.Movie;
5
import android.graphics.Movie;
-
 
6
import android.net.Uri;
-
 
7
import android.os.SystemClock;
7
import android.util.AttributeSet;
8
import android.util.AttributeSet;
-
 
9
import android.util.Log;
8
import android.view.View;
10
import android.view.View;
9
 
11
 
-
 
12
import java.io.FileNotFoundException;
10
import java.io.InputStream;
13
import java.io.InputStream;
11
 
14
 
12
/**
15
/**
13
 * Created by rajender on 24/5/17.
16
 * Created by rajender on 24/5/17.
14
 */
17
 */
15
public class GIFView extends View {
18
public class GIFView extends View {
16
 
19
 
-
 
20
    private InputStream mInputStream;
17
    public Movie mMovie;
21
    private Movie mMovie;
-
 
22
    private int mWidth, mHeight;
18
    public long movieStart;
23
    private long mStart;
19
    public GIFView(Context context) {
24
    private Context mContext;
20
 
25
 
-
 
26
    public GIFView(Context context) {
21
        super(context);
27
        super(context);
22
        initializeView();
28
        this.mContext = context;
23
    }
29
    }
24
 
30
 
25
    public GIFView(Context context, AttributeSet attrs) {
31
    public GIFView(Context context, AttributeSet attrs) {
26
        super(context, attrs);
32
        this(context, attrs, 0);
27
        initializeView();
33
    }
28
 
34
 
-
 
35
    public GIFView(Context context, AttributeSet attrs, int defStyleAttr) {
-
 
36
        super(context, attrs, defStyleAttr);
-
 
37
        this.mContext = context;
-
 
38
        if (attrs.getAttributeName(1).equals("background")) {
-
 
39
            int id = Integer.parseInt(attrs.getAttributeValue(1).substring(1));
-
 
40
            setGifImageResource(id);
-
 
41
        }
29
    }
42
    }
-
 
43
 
-
 
44
 
-
 
45
    private void init() {
-
 
46
        setFocusable(true);
30
    public GIFView(Context context, AttributeSet attrs, int defStyle) {
47
        mMovie = Movie.decodeStream(mInputStream);
-
 
48
        mWidth = mMovie.width();
31
        super(context, attrs, defStyle);
49
        mHeight = mMovie.height();
-
 
50
 
32
        initializeView();
51
        requestLayout();
33
    }
52
    }
34
    private void initializeView() {
53
    @Override
35
        InputStream is = getContext().getResources().openRawResource(R.drawable.progressbar);
54
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
36
        mMovie = Movie.decodeStream(is);
55
        setMeasuredDimension(mWidth, mHeight);
37
    }
56
    }
38
 
57
 
39
    @Override
58
    @Override
40
    protected void onDraw(Canvas canvas) {
59
    protected void onDraw(Canvas canvas) {
41
 
60
 
42
        canvas.drawColor(Color.TRANSPARENT);
-
 
43
        super.onDraw(canvas);
-
 
44
        long now = android.os.SystemClock.uptimeMillis();
61
        long now = SystemClock.uptimeMillis();
-
 
62
 
45
        if (movieStart == 0) {
63
        if (mStart == 0) {
46
            movieStart = now;
64
            mStart = now;
47
        }
65
        }
-
 
66
 
48
        if (mMovie != null) {
67
        if (mMovie != null) {
-
 
68
 
-
 
69
            int duration = mMovie.duration();
-
 
70
            if (duration == 0) {
-
 
71
                duration = 1000;
-
 
72
            }
-
 
73
 
49
            int relTime = (int) ((now - movieStart) % mMovie.duration());
74
            int relTime = (int) ((now - mStart) % duration);
-
 
75
 
50
            mMovie.setTime(relTime);
76
            mMovie.setTime(relTime);
51
            mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height());
-
 
52
            this.invalidate();
-
 
53
        }}
-
 
54
    private int gifId;
-
 
55
    public void setGIFResource(int resId) {
-
 
56
        this.gifId = resId;
-
 
57
        initializeView();
-
 
58
    }
-
 
59
 
77
 
60
    public int getGIFResource() {
78
            mMovie.draw(canvas, 0, 0);
61
        return this.gifId;
79
            invalidate();
-
 
80
        }
-
 
81
    }
62
 
82
 
-
 
83
    public void setGifImageResource(int id) {
-
 
84
        mInputStream = mContext.getResources().openRawResource(id);
-
 
85
        init();
63
    }}
86
    }
-
 
87
    public void setGifImageUri(Uri uri) {
-
 
88
        try {
-
 
89
            mInputStream = mContext.getContentResolver().openInputStream(uri);
-
 
90
            init();
-
 
91
        } catch (FileNotFoundException e) {
-
 
92
            Log.e("GIfImageView", "File not found");
-
 
93
        }
-
 
94
    }
-
 
95
}
64
 
96