github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

kaeppler / droid-fu

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 29
    • 3
  • Source
  • Commits
  • Network (3)
  • Issues (2)
  • Downloads (1)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (5)
    • gh-pages
    • google-mapmaker-cache
    • master ✓
    • maven-support
    • notifications
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A utility library for your daily Android needs — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

depend on Signpost 1.2 
kaeppler (author)
Fri Jan 29 10:16:20 -0800 2010
commit  4e11109604c55a94fc32b585c31aae7ae9cea046
tree    a695bdb9fd0808d128e007e3598fc5d67954968a
parent  f20304d8977502e1f895253c9c966adad4d6c7f7
droid-fu / src / main / java / com / github / droidfu / widgets / WebImageView.java src/main/java/com/github/droidfu/widgets/WebImageView.java
100755 212 lines (177 sloc) 7.075 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* Copyright (c) 2009 Matthias Käppler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
package com.github.droidfu.widgets;
 
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ViewSwitcher;
import android.widget.ImageView.ScaleType;
 
import com.github.droidfu.DroidFu;
import com.github.droidfu.imageloader.ImageLoader;
import com.github.droidfu.imageloader.ImageLoaderHandler;
 
/**
* An image view that fetches its image off the web using the supplied URL.
* While the image is being downloaded, a progress indicator will be shown.
*
* @author Matthias Kaeppler
*/
public class WebImageView extends ViewSwitcher {
 
    private String imageUrl;
 
    private boolean isLoaded;
 
    private ProgressBar loadingSpinner;
 
    private ImageView imageView;
 
    private ScaleType scaleType = ScaleType.CENTER_CROP;
 
    private Drawable progressDrawable;
 
    /**
* @param context
* the view's current context
* @param imageUrl
* the URL of the image to download and show
* @param autoLoad
* Whether the download should start immediately after creating the
* view. If set to false, use {@link #loadImage()} to manually
* trigger the image download.
*/
    public WebImageView(Context context, String imageUrl, boolean autoLoad) {
        super(context);
        initialize(context, imageUrl, null, autoLoad);
    }
 
    /**
* @param context
* the view's current context
* @param imageUrl
* the URL of the image to download and show
* @param progressDrawable
* the drawable to be used for the {@link ProgressBar} which is
* displayed while the image is loading
* @param autoLoad
* Whether the download should start immediately after creating the
* view. If set to false, use {@link #loadImage()} to manually
* trigger the image download.
*/
    public WebImageView(Context context, String imageUrl, Drawable progressDrawable,
            boolean autoLoad) {
        super(context);
        initialize(context, imageUrl, progressDrawable, autoLoad);
    }
 
    public WebImageView(Context context, AttributeSet attributes) {
        super(context, attributes);
        // TypedArray styles = context.obtainStyledAttributes(attributes,
        // R.styleable.GalleryItem);
        int progressDrawableId = attributes.getAttributeResourceValue(DroidFu.XMLNS,
            "progressDrawable", 0);
        Drawable progressDrawable = null;
        if (progressDrawableId > 0) {
            progressDrawable = context.getResources().getDrawable(progressDrawableId);
        }
        initialize(context, attributes.getAttributeValue(DroidFu.XMLNS, "imageUrl"),
            progressDrawable, attributes.getAttributeBooleanValue(DroidFu.XMLNS, "autoLoad", true));
        // styles.recycle();
    }
 
    private void initialize(Context context, String imageUrl, Drawable progressDrawable,
            boolean autoLoad) {
        this.imageUrl = imageUrl;
        this.progressDrawable = progressDrawable;
 
        ImageLoader.initialize(context);
 
        // ScaleAnimation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
        // 125.0f, preferredItemHeight / 2.0f);
        // anim.setDuration(500L);
 
        AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
        anim.setDuration(500L);
        setInAnimation(anim);
 
        addLoadingSpinnerView(context);
        addImageView(context);
 
        if (autoLoad && imageUrl != null) {
            loadImage();
        }
    }
 
    private void addLoadingSpinnerView(Context context) {
        loadingSpinner = new ProgressBar(context);
        loadingSpinner.setIndeterminate(true);
        if (this.progressDrawable == null) {
            this.progressDrawable = loadingSpinner.getIndeterminateDrawable();
        } else {
            loadingSpinner.setIndeterminateDrawable(progressDrawable);
            if (progressDrawable instanceof AnimationDrawable) {
                ((AnimationDrawable) progressDrawable).start();
            }
        }
 
        LayoutParams lp = new LayoutParams(progressDrawable.getIntrinsicWidth(),
                progressDrawable.getIntrinsicHeight());
        lp.gravity = Gravity.CENTER;
 
        addView(loadingSpinner, 0, lp);
    }
 
    private void addImageView(Context context) {
        imageView = new ImageView(context);
        imageView.setScaleType(scaleType);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        addView(imageView, 1, lp);
    }
 
    /**
* Use this method to trigger the image download if you had previously set
* autoLoad to false.
*/
    public void loadImage() {
        if (imageUrl == null) {
            throw new IllegalStateException(
                    "image URL is null; did you forget to set it for this view?");
        }
        ImageLoader.start(imageUrl, new DefaultImageLoaderHandler());
    }
 
    public boolean isLoaded() {
        return isLoaded;
    }
 
    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }
 
    public void setProgressDrawable(Drawable progressDrawable) {
        this.progressDrawable = progressDrawable;
    }
 
    /**
* Often you have resources which usually have an image, but some don't. For
* these cases, use this method to supply a placeholder drawable which will
* be loaded instead of a web image.
*
* @param imageResourceId
* the resource of the placeholder image drawable
*/
    public void setNoImageDrawable(int imageResourceId) {
        imageView.setImageDrawable(getContext().getResources().getDrawable(imageResourceId));
        setDisplayedChild(1);
    }
 
    @Override
    public void reset() {
        super.reset();
 
        this.setDisplayedChild(0);
    }
 
    private class DefaultImageLoaderHandler extends ImageLoaderHandler {
 
        public DefaultImageLoaderHandler() {
            super(imageView);
        }
 
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isLoaded = true;
 
            setDisplayedChild(1);
        }
    }
}
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server