自定义超轻便的SmartImageView小框架

开发过程中往往需要获取网络图片资源显示在布局当中,每次重复的造轮子让人很揪心,因此我整理了一个超轻便的SmartImageView小框架,留给有需要的你!

  • 这个框架只有一个功能,就是通过一个url获取一张网络图片并显示,如果url无效则显示R.drawable下的ic_launcher.

效果图预览

自定义超轻便的SmartImageView小框架_第1张图片

具体代码实现


  • 1.写在包com.yashiro.smartimageview.ui中的代码

Java代码SmartImageView.java:

package com.yashiro.smartimageview.ui;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
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 SmartImageView extends ImageView {

    protected static final int SUCESS = 1;
    protected static final int ERROR = 2;

    private Handler handler = new Handler() {

    public void handleMessage(android.os.Message msg) {
        switch (msg.what) {
        case SUCESS:
            Bitmap bitmap = (Bitmap) msg.obj;
            setImageBitmap(bitmap);
            break;

        case ERROR:
            int res = (Integer) msg.obj;
            setImageResource(res);
            break;
            }
        };
    };

    public SmartImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SmartImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SmartImageView(Context context) {
        super(context);
    }

    /**
     * 设置一个网络的图片路径,直接把网络上的图片给显示出来
     * 
     * @param url
     */
    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 is = conn.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        Message msg = Message.obtain();
                        msg.obj = bitmap;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };
        }.start();
    }

    /**
     * 设置一个网络的图片路径,直接把网络上的图片给显示出来
     * 
     * @param url资源的路径
     * @param errorres
     *            如果图片资源下载失败,显示什么样的默认图片
     */
    public void setImageUrl(final String path, final int errorres) {
        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 is = conn.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        Message msg = Message.obtain();
                        msg.what = SUCESS;
                        msg.obj = bitmap;
                        handler.sendMessage(msg);
                    } else {
                        Message msg = Message.obtain();
                        msg.what = ERROR;
                        msg.obj = errorres;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = Message.obtain();
                    msg.what = ERROR;
                    msg.obj = errorres;
                    handler.sendMessage(msg);
                }
            };
        }.start();
    }
}
  • 2.写在包com.yashiro.myimageview中的代码

Java代码MainActivity.java:

package com.yashiro.myimageview;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.yashiro.smartimageview.ui.SmartImageView;

public class MainActivity extends Activity {
    private SmartImageView siv;
    private EditText et_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_url=(EditText) findViewById(R.id.et_url);
        siv=(SmartImageView) findViewById(R.id.siv);
    }

    public void read(View view){
        String path = et_url.getText().toString().trim();
        if (TextUtils.isEmpty(path)) {
            Toast.makeText(MainActivity.this, "请输入一个网络图片链接", Toast.LENGTH_SHORT).show();
            return;
        }
        //读取设置路径,若路径不存在则默认显示drawble下的ic_launcher.
        siv.setImageUrl(path, R.drawable.ic_launcher);
    }


}
  • 3.写在 res/layout/activity_main.xml的布局文件
<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" >

    <EditText
        android:id="@+id/et_url"
        android:layout_marginTop="5dip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入一个图片链接"
        android:text="https://www.baidu.com/img/bd_logo1.png" />
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="read"
        android:text="获取图片"
        />
    <com.yashiro.smartimageview.ui.SmartImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/siv"
        />

LinearLayout>
  • 4.清单文件 AndroidManifest.xml 的写法.添加访问网络的权限
<uses-permission android:name="android.permission.INTERNET"/>

小结:

如果你只需要通过一个URL获取一张网络图片,不需要其他的附加功能,这款超轻便的SmartImageView就非常适合你!!

因为它真的只有一个功能!!! 哈哈~~~

你可能感兴趣的:(Android,框架)