ImageView加载网络图片
aide吧
全部回复
仅看楼主
level 11
话不多说,直接上代码
2016年10月03日 11点10分 1
level 11
代码
import android.app.*;
import android.os.*;
import android.widget.*;
import java.io.*;
import java.net.*;
import android.graphics.*;
public class MainActivity extends Activity
{
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView=(ImageView) findViewById(R.id.mainImageView1);
final String url="https://m.baidu.com/static/index/plus/plus_logo.png";
new Thread(){
public void run()
{
loadImage(url);
}
}.start();
}
private void loadImage(String url)
{
try
{
InputStream in=new URL(url).openStream();
final Bitmap bmp=BitmapFactory.decodeStream(in);
in.close();
runOnUiThread(new Runnable(){
@Override
public void run()
{
imageView.setImageBitmap(bmp);
}
});
}
catch (IOException e)
{}
}
}
2016年10月03日 11点10分 2
level 11
有什么问题请加群:
410015377
2016年10月03日 11点10分 3
level 12
[汗]
2016年10月03日 12点10分 4
level 8
[滑稽]。。。
2016年10月04日 08点10分 5
level 5
楼主,如果加入异常处理代码的话!就完美了![滑稽]
2016年10月04日 08点10分 6
level 12
目的是来宣传群的吧[狂汗]
2016年10月05日 12点10分 7
level 11
一般我都用Picasso
2016年10月05日 13点10分 8
level 13
嘿嘿
2016年10月05日 13点10分 9
level 11
主线程可以访问网络?不是4.4以上不支持了吗[疑问][疑问]
2016年10月06日 14点10分 12
level 11
主线程可以访问网络?不是4.4以上不支持了吗[疑问][疑问]
2016年10月06日 14点10分 13
不在主线程[滑稽]
2016年10月06日 15点10分
回复
文WU2564857826
:我一直以为在onCreat()里的都是主线程[泪]
2016年10月06日 15点10分
回复
dddddddyyyhe
:Thread mThread = new Thread() { @Override public void run() { /**这是我新建额额一个子线程哦**/ } }; mThread.start();
2016年10月06日 23点10分
1