編輯:Android開發實例
方法一:
- package com.example.handlerdemo;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private static final int MSG_SUCCESS = 0;// 獲取圖片成功的標識
- private static final int MSG_FAILURE = 1;// 獲取圖片失敗的標識
- ImageView mImageView;
- Handler mHandler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MSG_SUCCESS:
- mImageView.setImageBitmap((Bitmap) msg.obj);// imageview顯示從網絡獲取到的logo
- break;
- case MSG_FAILURE:
- Toast.makeText(getApplication(), "下載失敗", Toast.LENGTH_LONG)
- .show();
- break;
- }
- }
- };protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mImageView = (ImageView) findViewById(R.id.imageView1);// 顯示圖片的ImageView
- }
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button1:
- mImageView.setImageBitmap(null);
- new Thread(new Runnable() {
- public void run() {
- HttpClient hc = new DefaultHttpClient();
- HttpGet hg = new HttpGet(
- "http://www.baidu.com/img/baidu_sylogo1.gif");// 獲取百度的LOGO
- final Bitmap bm;
- try {
- HttpResponse hr = hc.execute(hg);
- bm = BitmapFactory.decodeStream(hr.getEntity()
- .getContent());
- } catch (Exception e) {
- mHandler.obtainMessage(MSG_FAILURE).sendToTarget();// 獲取圖片失敗
- return;
- }
- mHandler.obtainMessage(MSG_SUCCESS, bm).sendToTarget();// 獲取圖片成功,向ui線程發送MSG_SUCCESS標識和bitmap對象
- }
- }).start();
- break;
- }
- }
- }
不過這個方法有個小問題 老是提示會內存洩露
方法二:
- package com.example.handlerdemo;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- ImageView mImageView;
- Handler mHandler02 = new Handler();
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mImageView = (ImageView) findViewById(R.id.imageView1);// 顯示圖片的ImageView
- }
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button2:
- mImageView.setImageBitmap(null);
- new Thread(new Runnable() {
- public void run() {
- HttpClient hc = new DefaultHttpClient();
- HttpGet hg = new HttpGet(
- "http://www.baidu.com/img/baidu_sylogo1.gif");// 獲取百度的LOGO
- try {
- HttpResponse hr = hc.execute(hg);
- final Bitmap bm = BitmapFactory.decodeStream(hr.getEntity()
- .getContent());
- mHandler02.post(new Runnable() {
- public void run() {
- mImageView.setImageBitmap(bm);// imageview顯示從網絡獲取到的logo
- }
- });
- } catch (Exception e) {
- mHandler02.post(new Runnable() {
- public void run() {
- Toast.makeText(getApplication(), "下載失敗",
- Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- }).start();
- break;
- default:
- break;
- }
- }
- }
用方法二是不會出內存洩露的警告的
效果圖:
csdn下載地址:http://download.csdn.net/detail/wenwei19861106/4868799
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
前文介紹了Android中MediaPlayer用法的時候稍微介紹了SurfaceView,SurfaceView由於可以直接從內存或者DMA等硬件接口取得圖像數
Android應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我
寫這篇文章,做份備忘,簡單滴展示一個帶進度條的Webview示例,進度條位於Webview上面. 示例圖如下: 主Activity代碼: 代碼如下: packa