zoukankan      html  css  js  c++  java
  • android 加载网络图片

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center"
            android:text="加载" />
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
    </LinearLayout>

    网络类

    package com.example.viewwebimagedemo.utils;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class HttpUtils {
    
        private final static String URL_PATH = "http://images.cnblogs.com/cnblogs_com/liuning8023/588559/o_Android.jpg";
    
        public HttpUtils() {
            // TODO Auto-generated constructor stub
        }
    
        public static InputStream getImageViewInputStream() throws IOException {
            InputStream inputStream = null;
    
            URL url = new URL(URL_PATH);
            if (url != null) {
                HttpURLConnection httpURLConnection = (HttpURLConnection) url
                        .openConnection();
                httpURLConnection.setConnectTimeout(3000);
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setDoInput(true);
                int response_code = httpURLConnection.getResponseCode();
                if (response_code == 200) {
                    inputStream = httpURLConnection.getInputStream();
                }
            }
            return inputStream;
        }
    }
    package com.example.viewwebimagedemo;
    
    import java.io.InputStream;
    
    import com.example.viewwebimagedemo.R;
    import com.example.viewwebimagedemo.utils.HttpUtils;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        private Button button;
        private ImageView imageView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            button = (Button) this.findViewById(R.id.btn);
            imageView = (ImageView) this.findViewById(R.id.imageView);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread(access).start();
                }
            });
        }
           // 另一个线程从网络获得图片
        private Runnable access = new Runnable() {
            @Override
            public void run() {
                getImg();
            }
        };
    
        // 获得图片
        public void getImg() {
            try {
                InputStream inputStream = HttpUtils.getImageViewInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                Looper.prepare();// 必须调用此方法,要不然会报错
                Message msg = new Message();
                msg.what = 0;
                msg.obj = bitmap;
                handler.sendMessage(msg);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "获取图片错误", 1).show();
            }
        }
    
    
        
    
    
        // 更新UI信息
        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0) {
                    if (msg.obj == null) {
                        Toast.makeText(getApplicationContext(), "图片不存在", 1).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "加载图片...", 1)
                                .show();
                        setImg((Bitmap) msg.obj);
                    }
                }
            }
        };
           // 加载图片
        private void setImg(Bitmap bm) {
            imageView.setImageBitmap(bm);
        }
    }

    效果:

    记得加入权限:  <uses-permission android:name="android.permission.INTERNET" />

  • 相关阅读:
    颜色转换
    颜色转换、随机、16进制转换、HSV
    ColorTransform调整显示对象的颜色值
    unity+统计代码总行数
    Unity加载json数据
    打不开unity编辑器界面,每次默认打开上一次项目,并且报错Error loading file:///C%3A%2FUsers%2FT01017%2FAppData%2FRoaming%2FUnity%2FPackages%2Fnode_modules%2F
    Unity 同一Text文本修改不同的字体大小和字体颜色,加空格
    Unity 限时使用 限制试用时间和使用次数
    unity更改文字透明度
    unity+动画状态机
  • 原文地址:https://www.cnblogs.com/laopo/p/5826482.html
Copyright © 2011-2022 走看看