<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_normal" /> <!-- default -->
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
</selector>
XML file in your project res/drawable/ folder
引用 ImageButton ( android:src attribute).
http://developer.android.com/intl/de/reference/android/widget/ImageButton.html
http://developer.android.com/intl/fr/reference/android/graphics/drawable/StateListDrawable.html
styles 不能代码修改 theme可以动态修改
http://developer.android.com/intl/fr/guide/topics/ui/themes.html
res/drawable/my_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/btn_pressed" />
<item
android:state_pressed="false"
android:drawable="@drawable/btn_normal" />
</selector>
Button如此设置android:background="@drawable/my_button".
final Runnable r = new Runnable()
{
public void run()
{
tv.append("Hello World");
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);
Thread thread = new Thread()
{
@Override
public void run() { try {
while(true) {
sleep(1000);
handler.post(r);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
2.多线程图片
class DownloadImage extends AsyncTask<Void, Void, Drawable>{
@Override
protected Drawable doInBackground(Void... params) {
return Util.getImageFromURL(/blog_article/imageURL/index.html);
}
@Override
protected void onPostExecute( Drawable d ) {
getImageIcon().setImageDrawable(d);
}
}
new DownloadImage().execute();
http://developer.android.com/intl/zh-CN/reference/android/os/AsyncTask.html
private static void downloadFile(String url, File outputFile) {
try {
URL u = new URL(/blog_article/url/index.html);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
fos.write(buffer);
fos.flush();
fos.close();
} catch(FileNotFoundException e) {
return; // swallow a 404
} catch (IOException e) {
return; // swallow a 404
}
}