編輯:關於Android編程
這個小例子包含知識點:
1.Thread的使用
2.使用Handle發送數據
3.獲取網絡圖片並顯示
先上代碼
package com.example.getwebimage;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView mImageView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView1=(ImageView)findViewById(R.id.imageview1);
final Handler handler=new Handler()
{
@Override
public void handleMessage(Message msg)
{
if (msg.what==1) {
Bitmap bitmap2=(Bitmap)msg.obj;
mImageView1.setImageBitmap(bitmap2);
}
}
};
new Thread()
{
public void run()
{
Bitmap bitmap1=getBitMap("http://www.playforum.cn/wp-content/uploads/2013/12/1F0454024-1.png");
Message msg=new Message();
msg.what=1;
msg.obj=bitmap1;
handler.sendMessage(msg);
}
}.start();
}
//????????
public Bitmap getBitMap(String url)
{
Bitmap bitmap=null;
URL picUrl=null;
try {
picUrl=new URL(url);
} catch (MalformedURLException e) {
// TODO: handle exception
e.printStackTrace();
}
try {
HttpURLConnection connection=(HttpURLConnection)picUrl.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream=connection.getInputStream();
bitmap=BitmapFactory.decodeStream(inputStream);
inputStream.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
return bitmap;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}Layout
這個下過來的圖片是史上最坑爹游戲攻略,大家應該都玩過這個游戲吧。。
Android自定義view進階-- 神奇的貝塞爾曲線
今天給大家介紹一個非常神奇的曲線,貝塞爾曲線。相信大家之前都有耳聞。很久之前就久聞該線大名,但是一直不是很了解,在經過一番谷歌之後,有了初步的概念:三點確定一條曲線:起點
實例探究Android開發中Fragment狀態的保存與恢復方法
我們都知道,類似 Activity, Fragment 有 onSaveInstanceState() 回調用來保存狀態。在Fragment裡面,利用onSaveInst
Android中ListView結合CheckBox實現數據批量選擇(全選、反選、全不選)
APP的開發中,會常遇到這樣的需求:批量取消(刪除)List中的數據。這就要求ListVIew支持批量選擇、全選、單選等等功能,做一個比較強大的ListView批量選擇功
Android Material Design 5.0 PickerDialog
5.0系統下的時間選擇器效果圖: 該項目兼容到3.0以下所以用第三方開源項目:actionbarsherlock,動畫效果兼容:nineoldandroids-