<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                今天太偷懶了,只學了AsyncTask的一點點內容。今晚還要去上課。加油努力吧。 下面這個代碼只是用AsyncTask實現一個很簡單的事情“從網上下載圖片顯示到手機上”,但是作為AsyncTask是一個不錯的示例。 順便講一下,AsyncTask的重要知識點包括他的三個參數(第一個參數:傳入doInBackground()方法的參數類型,第二個參數:傳入onProgressUpdate()方法的參數類型?, 第三個參數:傳入onPostExecute()方法的參數類型,也是doInBackground()方法返回的類型。) 另外還有三四個重要的函數,其中三個會在下面的代碼里面出現。 吐槽一下:之前錯了兩三次全是因為圖片資源錯誤了,淚奔。、 ~~~ package com.example.myasynctask; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private Button button; private ImageView iv; private String path = "http://cdn.duitang.com/uploads/item/201310/17/20131017194234_eZfFU.thumb.600_0.png"; private ProgressDialog dia; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.bt); iv = (ImageView) this.findViewById(R.id.iv); dia = new ProgressDialog(this); dia.setTitle("提示信息"); dia.setMessage("正在下載..."); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new myTask().execute(path); } }); } public class myTask extends AsyncTask<String, Void, Bitmap> { @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); dia.show(); } @Override protected Bitmap doInBackground(String... params) { // TODO Auto-generated method stub HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(params[0]); Bitmap bitmap = null; try { HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200 ) { HttpEntity httpEntity = httpResponse.getEntity(); byte[] data = EntityUtils.toByteArray(httpEntity); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bitmap; } @Override protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub super.onPostExecute(result); iv.setImageBitmap(result); dia.dismiss(); } } } ~~~ 一個簡簡單單的布局 ~~~ <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="30dp" android:gravity="center_vertical" android:text="下載圖片" /> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="156dp" android:src="@drawable/ic_launcher" /> </RelativeLayout> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看