一。权限
1 2 3 |
<uses-permission android:name="android.permission.INTERNET"/> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<!-- 线性布局。宽、高:占满全局,对齐方向:垂直 --> <LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <!-- 图片控件。新建ID,宽:占满全局,高:比例像素,权重:独占比例1 --> <ImageView android:id="@+id/iv_icon" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <!-- 线性布局。宽:占满全局,高:自身适应,对齐:水平 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/et_url" android:layout_width="0dip" android:layout_height="wrap_content" android:text="http://www.gaohaiyan.com/wp-content/uploads/2014/08/sms_classify.png" android:singleLine="true" android:layout_weight="1" /> <!-- 按钮控件。新建ID,宽、高:自身适应,单击事件:onClick,文字尺寸:20比例像素 --> <Button android:id="@+id/btn_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go" android:onClick="onClick" android:textSize="20sp" /> </LinearLayout> </LinearLayout> |
三。主程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
/** * 使用消息机制处理断网时的意外 */ public class MainActivity extends ActionBarActivity { private static final String TAG = "MainActivity"; private final int SUCCESS = 0; private final int ERROR = 1; private EditText etUrl; private ImageView ivIcon; /** * 1.消息处理器 */ private Handler handler = new Handler() { /** * 3.接收消息 */ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Log.i(TAG, "what = " + msg.what); if(msg.what == SUCCESS) { // 当前是访问网络, 去显示图片 ivIcon.setImageBitmap((Bitmap) msg.obj); // 设置imageView显示的图片 } else if(msg.what == ERROR) { Toast.makeText(MainActivity.this, "抓去失败", 0).show(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ivIcon = (ImageView) findViewById(R.id.iv_icon); etUrl = (EditText) findViewById(R.id.et_url); } public void onClick(View v) { final String url = etUrl.getText().toString(); /** * 子线程 */ new Thread(new Runnable() { public void run() { Bitmap bitmap = getImageFromNet(url); //ivIcon.setImageBitmap(bitmap); // 只有主线程可以设置imageView显示的图片 if(bitmap != null) { Message msg = new Message(); msg.what = SUCCESS; // 消息状态=成功 msg.obj = bitmap; // 消息内容=图片 /** * 2.发送消息 */ handler.sendMessage(msg); } else { Message msg = new Message(); msg.what = ERROR; handler.sendMessage(msg); } }}).start(); } /** * 根据url连接取网络抓去图片返回 * @param url * @return url对应的图片 */ private Bitmap getImageFromNet(String url) { HttpURLConnection conn = null; try { URL mURL = new URL(url); // 创建一个url对象 // 得到http的连接对象 conn = (HttpURLConnection) mURL.openConnection(); conn.setRequestMethod("GET"); // 设置请求方法为Get conn.setConnectTimeout(5000); // 设置连接服务器的超时时间, 如果超过5秒钟, 没有连接成功, 会抛异常 conn.setReadTimeout(5000); // 设置读取数据时超时时间, 如果超过5秒, 抛异常 conn.connect(); // 开始链接 int responseCode = conn.getResponseCode(); // 得到服务器的响应码 // 访问成功 if(responseCode == 200) { InputStream is = conn.getInputStream(); // 获得服务器返回的流数据 Bitmap bitmap = BitmapFactory.decodeStream(is); // 根据 流数据 创建一个bitmap位图对象 return bitmap; } else { Log.i(TAG, "访问失败: responseCode = " + responseCode); } } catch (Exception e) { e.printStackTrace(); } finally { if(conn != null) { conn.disconnect(); // 断开连接 } } return null; } } |
四。使用smart-image-view
地址:https://github.com/loopj/android-smart-image-view
下载:[download id="199"]
1.将%android-smart-image-view-master%/src/里的com包放入工程

2.布局中更改图片展示控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <!-- -----------这里使用自定义的图片控件----------- --> <com.loopj.android.image.SmartImageView android:id="@+id/iv_icon" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/et_url" android:layout_width="0dip" android:text="http://www.gaohaiyan.com/wp-content/uploads/2014/08/sms_classify.png" android:layout_height="wrap_content" android:singleLine="true" android:layout_weight="1" /> <Button android:id="@+id/btn_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go" android:onClick="onClick" android:textSize="20sp" /> </LinearLayout> </LinearLayout> |
3.主程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class MainActivity extends ActionBarActivity { private EditText etUrl; private SmartImageView smartImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etUrl = (EditText) findViewById(R.id.et_url); smartImageView = (SmartImageView) findViewById(R.id.iv_icon); } public void onClick(View v) { // 1. 取出图片url String url = etUrl.getText().toString(); // 2. 控件自行更新 smartImageView.setImageUrl(url); } } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/160.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设