編輯:關於Android編程
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.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; public class MainActivity extends Activity { private Button startDownloadBtn; private ImageView resultImageView; // private static final String IMAGE_URL = http://e.hiphotos.baidu.com/image/pic/item/e850352ac65c10389f9a68c3b0119313b17e89cf.jpg; private static final String IMAGE_URL = http://d.hiphotos.baidu.com/image/pic/item/e61190ef76c6a7ef9544b321fefaaf51f3de6652.jpg; private DownloadThread downloadThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { startDownloadBtn = (Button) findViewById(R.id.startDownloadBtn); startDownloadBtn.setOnClickListener(new mOnclickListener()); resultImageView = (ImageView) findViewById(R.id.resultImageView); downloadThread = new DownloadThread(IMAGE_URL); } class mOnclickListener implements View.OnClickListener{ @Override public void onClick(View v) { //開啟新線程下載 downloadThread.start(); } } class DownloadThread extends Thread{ private String imageurl; private MyHandler myHandler = new MyHandler(); private Message msg = null; public DownloadThread( String url) { this.imageurl = url; } @Override public void run() { try { URL url = new URL(imageurl); URLConnection connection = url.openConnection(); connection.connect(); InputStream is = connection.getInputStream(); Log.i(LENGTH, LENGTH----------------- + connection.getContentLength()); Bitmap bitmap = BitmapFactory.decodeStream(is); is.close(); //輸入流關閉後說明已經獲取了圖像,這時候可以通過handler傳送數據 msg = new Message(); msg.obj = bitmap; //Handler 不能在另外一個線程任務裡面實例化 myHandler.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); } } } class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { //顯示圖片 Bitmap bitmap = (Bitmap)msg.obj; resultImageView.setImageBitmap(bitmap); super.handleMessage(msg); } } }
<framelayout android:layout_alignparentbottom="true" android:layout_alignparentend="true" android:layout_alignparentright="true" android:layout_height="wrap_content" android:layout_width="fill_parent"></framelayout>
當然最後不能忘記加入網絡權限。
一、直接看效果二、直接上代碼1.自定義控件部分package com.susan.project.myapplication;import android.app.Act
介紹相信開發Android的人都會有這種體會:從網上下載的demo運行的好好的,但是只要出現了滑動沖突,Demo就無法正常工作了。但是不用擔心,解決滑動沖突有固定的模式,
Android提供以下四種存儲方式:SharePreferenceSQLiteFileContentProvider如果要實現數據共享,正確的方式是使用ContentPr
ListView我們一直都在用,只不過當Adapter中的內容比較多的時候我們有時候沒辦法去設置一些組件,舉個例子:可以看到京東的故事裡面的這樣一個布局,這個布局可以說是