安卓开发:SmartImageView简单实现和应用
程序员文章站
2022-05-22 13:43:52
通常从服务器端获取的图片是URL地址,如果简单地通过URL地址获取图片? 有一个开源项目:SmartImageView,做到了这个功能,同时还有其他功能,下载不便,过于庞大 这里自己实现它的这个简单功能 代码: 两个重载方法: 1:明确URL地址正确、不会失误,直接调用 2:防止图片URL出错,设置 ......
通常从服务器端获取的图片是URL地址,如果简单地通过URL地址获取图片?
有一个开源项目:SmartImageView,做到了这个功能,同时还有其他功能,下载不便,过于庞大
这里自己实现它的这个简单功能
代码:
package org.dreamtech.smartimageview; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.widget.ImageView; public class MySmartImageView extends ImageView { protected static final int REQUESTSUCCESS = 1; protected static final int REQUESTFAIL = 2; protected static final int ERROR = 3; @SuppressLint("HandlerLeak") private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case REQUESTSUCCESS: Bitmap bitmap = (Bitmap) msg.obj; MySmartImageView.this.setImageBitmap(bitmap); break; case REQUESTFAIL: int default_resource = (Integer) msg.obj; MySmartImageView.this.setBackgroundResource(default_resource); break; case ERROR: int resource = (Integer) msg.obj; MySmartImageView.this.setBackgroundResource(resource); break; } }; }; // The construction methods of the parent class public MySmartImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MySmartImageView(Context context, AttributeSet attrs) { super(context, attrs); } public MySmartImageView(Context context) { super(context); } // A method of displaying pictures // path:The parameters of URL transmission public void setImageUrl(final String path) { new Thread() { public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if (code == 200) { InputStream in = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(in); Message msg = Message.obtain(); msg.obj = bitmap; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } // A method of displaying pictures(Overloading method) // path:The parameters of URL transmission // resource:Default resources(If you can't find a resource through this URL) public void setImageUrl(final String path, final int resource) { new Thread() { public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if (code == 200) { InputStream in = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(in); Message msg = Message.obtain(); msg.what = REQUESTSUCCESS; msg.obj = bitmap; handler.sendMessage(msg); } else { Message msg = Message.obtain(); msg.what = REQUESTFAIL; msg.obj = resource; handler.sendMessage(msg); } } catch (Exception e) { Message msg = Message.obtain(); msg.what = ERROR; msg.obj = resource; handler.sendMessage(msg); } }; }.start(); } }
两个重载方法:
1:明确URL地址正确、不会失误,直接调用
2:防止图片URL出错,设置默认资源,传两个参数
测试下:
package org.dreamtech.smartimageview; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MySmartImageView iv = (MySmartImageView) findViewById(R.id.iv); iv.setImageUrl( "http://fanyi.bdstatic.com/static/translation/img/header/logo_cbfea26.png", R.drawable.default_ic); } }
布局:
<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" > <org.dreamtech.smartimageview.MySmartImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
这里是一个正确地URL地址,结果如下:
接下来,我把URL地址改成错误的:
结果:
好的,完成!
上一篇: 最省钱的手部护理方法