MainActivity.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package org.uab.android.threading.uithread;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.ImageView;
  10. import android.widget.Toast;
  11. public class MainActivity extends Activity {
  12. Button loadImageButton;
  13. Button sayHelloButton;
  14. ImageView imageView;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. imageView = (ImageView) findViewById(R.id.imageView1);
  20. loadImageButton = (Button) findViewById(R.id.loadImage);
  21. loadImageButton.setOnClickListener(new OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. loadImage();
  25. }
  26. });
  27. sayHelloButton = (Button) findViewById(R.id.sayhello);
  28. sayHelloButton.setOnClickListener(new OnClickListener() {
  29. @Override
  30. public void onClick(View v) {
  31. Toast.makeText(MainActivity.this, "Hello class!",
  32. Toast.LENGTH_SHORT).show();
  33. }
  34. });
  35. }
  36. public void loadImage() {
  37. // Load image from drawable directory
  38. Bitmap bitmapImage = BitmapFactory.decodeResource(getResources(), R.drawable.grumpycat);
  39. // Simulate a long-running operation
  40. try {
  41. Thread.sleep(5000);
  42. } catch (InterruptedException e) {
  43. e.printStackTrace();
  44. }
  45. // Set the decoded bitmap as the ImageView's source
  46. if ( bitmapImage != null )
  47. imageView.setImageBitmap(bitmapImage);
  48. }
  49. }