Created
October 2, 2016 23:58
-
-
Save jsaund/656176d378ac763c9590438d7b9021b3 to your computer and use it in GitHub Desktop.
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
| public class MainActivity extends AppCompatActivity { | |
| private static final String IMAGE_URL = "https://www.android.com/static/2016/img/logo-android-green_2x.png"; | |
| private static final int MSG_SHOW_PROGRESS = 1; | |
| private static final int MSG_SHOW_IMAGE = 2; | |
| private ProgressBar progressIndicator; | |
| private ImageView imageView; | |
| private Handler handler; | |
| class ImageFetcher implements Runnable { | |
| final String url; | |
| ImageFetcher(String url) { | |
| this.url = url; | |
| } | |
| @Override | |
| public void run() { | |
| handler.obtainMessage(MSG_SHOW_PROGRESS).sendToTarget(); | |
| // download image -- code removed | |
| final Bitmap bitmap = BitmapFactory.decodeStream(inputstream); | |
| handler.obtainMessage(MSG_SHOW_IMAGE, bitmap).sendToTarget(); | |
| } | |
| } | |
| class UIHandler extends Handler { | |
| @Override | |
| public void handleMessage(Message msg) { | |
| switch (msg.what) { | |
| case MSG_SHOW_PROGRESS: { | |
| progressIndicator.setVisibility(View.VISIBLE); | |
| break; | |
| } | |
| case MSG_SHOW_IMAGE: { | |
| progressIndicator.setVisibility(View.GONE); | |
| imageView.setImageBitmap((Bitmap) msg.obj); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| handler = new UIHandler(); | |
| final Thread workerThread = new Thread(new ImageFetcher(IMAGE_URL)); | |
| workerThread.start(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment