当前位置:   article > 正文

android ImageView网络图片加载、动态设置尺寸、圆角.._android imageview动态设置网络图片

android imageview动态设置网络图片

第一种方法转载自:android ImageView网络图片加载、动态设置尺寸、圆角.. - L-H - 博客园

  1. package com.example.hangliao.oneyuanrecharge.helper;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Bitmap.Config;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Canvas;
  6. import android.graphics.Matrix;
  7. import android.graphics.Paint;
  8. import android.graphics.PorterDuff.Mode;
  9. import android.graphics.PorterDuffXfermode;
  10. import android.graphics.Rect;
  11. import android.graphics.RectF;
  12. import android.os.Handler;
  13. import android.os.Message;
  14. import android.widget.ImageView;
  15. import java.io.InputStream;
  16. import java.net.HttpURLConnection;
  17. import java.net.URL;
  18. public class ImageViewLoadAndSetSizeAndCircle {
  19. private PicHandler pic_hdl;
  20. private ImageView imgView;
  21. private String url;
  22. private int widht;
  23. private int height;
  24. private int CircleSize;
  25. /**
  26. * 通过图片url路径获取图片并显示到对应控件上
  27. *
  28. * @param imgView
  29. * @param url
  30. */
  31. public void setImage(ImageView imgView, String url,int widht,int height,int CircleSize) {
  32. this.url = url;
  33. this.imgView = imgView;
  34. this.widht=widht;
  35. this.height=height;
  36. this.CircleSize=CircleSize;
  37. pic_hdl = new PicHandler();
  38. Thread t = new LoadPicThread();
  39. t.start();
  40. }
  41. class LoadPicThread extends Thread {
  42. @Override
  43. public void run() {
  44. Bitmap img = getUrlImage(url);
  45. System.out.println(img + "---");
  46. Message msg = pic_hdl.obtainMessage();
  47. msg.what = 0;
  48. msg.obj = img;
  49. pic_hdl.sendMessage(msg);
  50. }
  51. }
  52. class PicHandler extends Handler {
  53. @Override
  54. public void handleMessage(Message msg) {
  55. Bitmap myimg = (Bitmap) msg.obj;
  56. imgView.setImageBitmap(myimg);
  57. }
  58. }
  59. public Bitmap getUrlImage(String url) {
  60. Bitmap img = null;
  61. try {
  62. URL picurl = new URL(url);
  63. HttpURLConnection conn = (HttpURLConnection) picurl
  64. .openConnection();
  65. conn.setConnectTimeout(6000);
  66. conn.setDoInput(true);
  67. conn.setUseCaches(false);
  68. conn.connect();
  69. InputStream is = conn.getInputStream();
  70. img = BitmapFactory.decodeStream(is);
  71. is.close();
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. img=zoomImage(img,this.widht,this.height);
  76. return img;
  77. }
  78. public Bitmap zoomImage(Bitmap bgimage, double newWidth,
  79. double newHeight) {
  80. // 获取这个图片的宽和高
  81. float width = bgimage.getWidth();
  82. float height = bgimage.getHeight();
  83. // 创建操作图片用的matrix对象
  84. Matrix matrix = new Matrix();
  85. // 计算宽高缩放率
  86. float scaleWidth = ((float) newWidth) / width;
  87. float scaleHeight = ((float) newHeight) / height;
  88. // 缩放图片动作
  89. matrix.postScale(scaleWidth, scaleHeight);
  90. Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
  91. (int) height, matrix, true);
  92. if (CircleSize>0) //需要圆角
  93. {
  94. bitmap=bitmapRound(bitmap,CircleSize);
  95. }
  96. else //不需要圆角
  97. {
  98. }
  99. return bitmap;
  100. }
  101. private Bitmap bitmapRound(Bitmap mBitmap,float CircleSize){
  102. Bitmap bitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_4444);
  103. Canvas canvas = new Canvas(bitmap);
  104. Paint paint = new Paint();
  105. paint.setAntiAlias(true);
  106. //设置矩形大小
  107. Rect rect = new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight());
  108. RectF rectf = new RectF(rect);
  109. // 相当于清屏
  110. canvas.drawARGB(0, 0, 0, 0);
  111. //画圆角
  112. canvas.drawRoundRect(rectf, CircleSize, CircleSize, paint);
  113. // 取两层绘制,显示上层
  114. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  115. // 把原生的图片放到这个画布上,使之带有画布的效果
  116. canvas.drawBitmap(mBitmap, rect, rect, paint);
  117. return bitmap;
  118. }
  119. }

使用方法:

  1. ImageView headerImage = ( ImageView ) convertView.findViewById(R.id.headerImage);
  2. /*
  3. * 下面方法参数说明:
  4. * 1=加载图片的ImageView
  5. * 2=图片下载地址
  6. * 3=动态设置的图片宽度
  7. * 4=动态设置的图片高度
  8. * 5=圆角尺寸,这里如果不圆角参数填0
  9. *
  10. *
  11. * */
  12. new ImageViewLoadAndSetSizeAndCircle().setImage(headerImage,"http://www.taopic.com/uploads/allimg/110928/41-11092PSF482.jpg",70,70,35);

第二种方式 简单直接粗暴

使用glide的override改变图片尺寸   

另外高斯模糊不可以和glide的centerCrop属性一起使用  

  1. private void gaosimohu() {
  2. int height= ScreenUtils.getScreenHeight();
  3. Glide.with(this).load(headIconUri)
  4. .apply(RequestOptions.bitmapTransform(new jp.wasabeef.glide.transformations.BlurTransformation(1, 30))) //高斯模糊
  5. .override(height,height) //指定宽高
  6. .into(placeholderView);
  7. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/73676?site
推荐阅读
相关标签
  

闽ICP备14008679号