Skip to content

Instantly share code, notes, and snippets.

View FlaviusPopescu's full-sized avatar

Flavius Popescu FlaviusPopescu

View GitHub Profile
@FlaviusPopescu
FlaviusPopescu / ImageDownloaderTask.java
Last active April 28, 2016 23:21
Internet and Background Threads
public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private static final String TAG = "ImageDownloaderTask";
private final OnImageDownloadedListener mListener;
public ImageDownloaderTask(OnImageDownloadedListener listener) {
mListener = listener;
}
@Override
protected Bitmap doInBackground(String... params) {
@FlaviusPopescu
FlaviusPopescu / MainActivity.java
Created April 28, 2016 23:51
Internet and Background Tasks
public class MainActivity extends AppCompatActivity {
final String IMAGE_URL = "https://pixabay.com/static/uploads/photo/2014/12/22/00/05/spindle-576757_960_720.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = (ImageView) findViewById(R.id.headerimage);
new ImageDownloaderTask(new ImageDownloaderTask.OnImageDownloadedListener() {
@FlaviusPopescu
FlaviusPopescu / ImageUtil.java
Created April 28, 2016 23:55
Internet and Background Threads
public class ImageUtil {
private static final String TAG = "ImageUtil";
public static Bitmap getImage(String urlSpec) {
Bitmap result = null;
try {
byte[] posterBytes = getBytes(urlSpec);
result = BitmapFactory.decodeByteArray(posterBytes, 0, posterBytes.length);
} catch (IOException e) {
Log.e(TAG, "getImage: ", e);
/**
* A helper view class useful in playing animation in a more memory efficient way, since Bitmaps
* are preloaded on a background handlerThread and reused, while the main thread "plays" the frames.
* To specify which animation frames should be played you need to provide an implementation of
* FramesDataSource.
*/
public class FrameAnimationView extends FrameLayout {
private static final String TAG = FrameAnimationView.class.getSimpleName();
private final ImageView imageView;
private final Handler animationHandler;