SimpleThreadingExample.java
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 |
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class SimpleThreadingExample extends Activity { private static final String TAG = "SimpleThreadingExample"; private Bitmap mBitmap; private ImageView mIView; private int mDelay = 5000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mIView = (ImageView) findViewById(R.id.imageView); final Button loadButton = (Button) findViewById(R.id.loadButton); loadButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { loadIcon(); } }); final Button otherButton = (Button) findViewById(R.id.otherButton); otherButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(SimpleThreadingExample.this, "I'm Working", Toast.LENGTH_SHORT).show(); } }); } private void loadIcon() { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(mDelay); } catch (InterruptedException e) { Log.e(TAG, e.toString()); } mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.painter); // This doesn't work in Android mIView.setImageBitmap(mBitmap); } }).start(); } } |
main.xml
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 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="300dp" android:scaleType="centerInside" android:contentDescription="@string/image_desc_string"> </ImageView> <Button android:id="@+id/loadButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/load_icon_string" android:textSize="24sp" > </Button> <Button android:id="@+id/otherButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/other_button_string" android:textSize="24sp" > </Button> </LinearLayout> |