给图片加上圆角效果好看多了。



加圆角,Drawable,Bitmap,BitmapDrawable,字节数组之间的相互转换。

Java代码
  1. publicclassImageUtil{
  2. publicstaticInputStreamgetRequest(Stringpath)throwsException{
  3. URLurl=newURL(path);
  4. HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
  5. conn.setRequestMethod("GET");
  6. conn.setConnectTimeout(5000);
  7. if(conn.getResponseCode()==200){
  8. returnconn.getInputStream();
  9. }
  10. returnnull;
  11. }
  12. publicstaticbyte[]readInputStream(InputStreaminStream)throwsException{
  13. ByteArrayOutputStreamoutSteam=newByteArrayOutputStream();
  14. byte[]buffer=newbyte[4096];
  15. intlen=0;
  16. while((len=inStream.read(buffer))!=-1){
  17. outSteam.write(buffer,0,len);
  18. }
  19. outSteam.close();
  20. inStream.close();
  21. returnoutSteam.toByteArray();
  22. }
  23. publicstaticDrawableloadImageFromUrl(Stringurl){
  24. URLm;
  25. InputStreami=null;
  26. try{
  27. m=newURL(url);
  28. i=(InputStream)m.getContent();
  29. }catch(MalformedURLExceptione1){
  30. e1.printStackTrace();
  31. }catch(IOExceptione){
  32. e.printStackTrace();
  33. }
  34. Drawabled=Drawable.createFromStream(i,"src");
  35. returnd;
  36. }
  37. publicstaticDrawablegetDrawableFromUrl(Stringurl)throwsException{
  38. returnDrawable.createFromStream(getRequest(url),null);
  39. }
  40. publicstaticBitmapgetBitmapFromUrl(Stringurl)throwsException{
  41. byte[]bytes=getBytesFromUrl(url);
  42. returnbyteToBitmap(bytes);
  43. }
  44. publicstaticBitmapgetRoundBitmapFromUrl(Stringurl,intpixels)throwsException{
  45. byte[]bytes=getBytesFromUrl(url);
  46. Bitmapbitmap=byteToBitmap(bytes);
  47. returntoRoundCorner(bitmap,pixels);
  48. }
  49. publicstaticDrawablegeRoundDrawableFromUrl(Stringurl,intpixels)throwsException{
  50. byte[]bytes=getBytesFromUrl(url);
  51. BitmapDrawablebitmapDrawable=(BitmapDrawable)byteToDrawable(bytes);
  52. returntoRoundCorner(bitmapDrawable,pixels);
  53. }
  54. publicstaticbyte[]getBytesFromUrl(Stringurl)throwsException{
  55. returnreadInputStream(getRequest(url));
  56. }
  57. publicstaticBitmapbyteToBitmap(byte[]byteArray){
  58. if(byteArray.length!=0){
  59. returnBitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
  60. }
  61. else{
  62. returnnull;
  63. }
  64. }
  65. publicstaticDrawablebyteToDrawable(byte[]byteArray){
  66. ByteArrayInputStreamins=newByteArrayInputStream(byteArray);
  67. returnDrawable.createFromStream(ins,null);
  68. }
  69. publicstaticbyte[]Bitmap2Bytes(Bitmapbm){
  70. ByteArrayOutputStreambaos=newByteArrayOutputStream();
  71. bm.compress(Bitmap.CompressFormat.PNG,100,baos);
  72. returnbaos.toByteArray();
  73. }
  74. publicstaticBitmapdrawableToBitmap(Drawabledrawable){
  75. Bitmapbitmap=Bitmap
  76. .createBitmap(
  77. drawable.getIntrinsicWidth(),
  78. drawable.getIntrinsicHeight(),
  79. drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888
  80. :Bitmap.Config.RGB_565);
  81. Canvascanvas=newCanvas(bitmap);
  82. drawable.setBounds(0,0,drawable.getIntrinsicWidth(),
  83. drawable.getIntrinsicHeight());
  84. drawable.draw(canvas);
  85. returnbitmap;
  86. }
  87. /**
  88. *图片去色,返回灰度图片
  89. *@parambmpOriginal传入的图片
  90. *@return去色后的图片
  91. */
  92. publicstaticBitmaptoGrayscale(BitmapbmpOriginal){
  93. intwidth,height;
  94. height=bmpOriginal.getHeight();
  95. width=bmpOriginal.getWidth();
  96. BitmapbmpGrayscale=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
  97. Canvasc=newCanvas(bmpGrayscale);
  98. Paintpaint=newPaint();
  99. ColorMatrixcm=newColorMatrix();
  100. cm.setSaturation(0);
  101. ColorMatrixColorFilterf=newColorMatrixColorFilter(cm);
  102. paint.setColorFilter(f);
  103. c.drawBitmap(bmpOriginal,0,0,paint);
  104. returnbmpGrayscale;
  105. }
  106. /**
  107. *去色同时加圆角
  108. *@parambmpOriginal原图
  109. *@parampixels圆角弧度
  110. *@return修改后的图片
  111. */
  112. publicstaticBitmaptoGrayscale(BitmapbmpOriginal,intpixels){
  113. returntoRoundCorner(toGrayscale(bmpOriginal),pixels);
  114. }
  115. /**
  116. *把图片变成圆角
  117. *@parambitmap需要修改的图片
  118. *@parampixels圆角的弧度
  119. *@return圆角图片
  120. */
  121. publicstaticBitmaptoRoundCorner(Bitmapbitmap,intpixels){
  122. Bitmapoutput=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Config.ARGB_8888);
  123. Canvascanvas=newCanvas(output);
  124. finalintcolor=0xff424242;
  125. finalPaintpaint=newPaint();
  126. finalRectrect=newRect(0,0,bitmap.getWidth(),bitmap.getHeight());
  127. finalRectFrectF=newRectF(rect);
  128. finalfloatroundPx=pixels;
  129. paint.setAntiAlias(true);
  130. canvas.drawARGB(0,0,0,0);
  131. paint.setColor(color);
  132. canvas.drawRoundRect(rectF,roundPx,roundPx,paint);
  133. paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
  134. canvas.drawBitmap(bitmap,rect,rect,paint);
  135. returnoutput;
  136. }
  137. /**
  138. *使圆角功能支持BitampDrawable
  139. *@parambitmapDrawable
  140. *@parampixels
  141. *@return
  142. */
  143. publicstaticBitmapDrawabletoRoundCorner(BitmapDrawablebitmapDrawable,intpixels){
  144. Bitmapbitmap=bitmapDrawable.getBitmap();
  145. bitmapDrawable=newBitmapDrawable(toRoundCorner(bitmap,pixels));
  146. returnbitmapDrawable;
  147. }
  148. }
public class ImageUtil {public static InputStream getRequest(String path) throws Exception {URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);if (conn.getResponseCode() == 200){return conn.getInputStream();}return null;}public static byte[] readInputStream(InputStream inStream) throws Exception {ByteArrayOutputStream outSteam = new ByteArrayOutputStream();byte[] buffer = new byte[4096];int len = 0;while ((len = inStream.read(buffer)) != -1) {outSteam.write(buffer, 0, len);}outSteam.close();inStream.close();return outSteam.toByteArray();}public static Drawable loadImageFromUrl(String url){        URL m;        InputStream i = null;        try {            m = new URL(url);            i = (InputStream) m.getContent();        } catch (MalformedURLException e1) {            e1.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        Drawable d = Drawable.createFromStream(i, "src");        return d;    }public static Drawable getDrawableFromUrl(String url) throws Exception{ return Drawable.createFromStream(getRequest(url),null);}public static Bitmap getBitmapFromUrl(String url) throws Exception{byte[] bytes = getBytesFromUrl(url);return byteToBitmap(bytes);}public static Bitmap getRoundBitmapFromUrl(String url,int pixels) throws Exception{byte[] bytes = getBytesFromUrl(url);Bitmap bitmap = byteToBitmap(bytes);return toRoundCorner(bitmap, pixels);} public static Drawable geRoundDrawableFromUrl(String url,int pixels) throws Exception{byte[] bytes = getBytesFromUrl(url);BitmapDrawable bitmapDrawable = (BitmapDrawable)byteToDrawable(bytes);return toRoundCorner(bitmapDrawable, pixels);} public static byte[] getBytesFromUrl(String url) throws Exception{ return readInputStream(getRequest(url));}public static Bitmap byteToBitmap(byte[] byteArray){if(byteArray.length!=0){             return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);         }         else {             return null;         }  }public static Drawable byteToDrawable(byte[] byteArray){ByteArrayInputStream ins = new ByteArrayInputStream(byteArray);return Drawable.createFromStream(ins, null);}public static byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.PNG, 100, baos);return baos.toByteArray();}public static Bitmap drawableToBitmap(Drawable drawable) {Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);Canvas canvas = new Canvas(bitmap);drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());drawable.draw(canvas);return bitmap;} /**      * 图片去色,返回灰度图片      * @param bmpOriginal 传入的图片     * @return 去色后的图片     */    public static Bitmap toGrayscale(Bitmap bmpOriginal) {        int width, height;        height = bmpOriginal.getHeight();        width = bmpOriginal.getWidth();            Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);        Canvas c = new Canvas(bmpGrayscale);        Paint paint = new Paint();        ColorMatrix cm = new ColorMatrix();        cm.setSaturation(0);        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);        paint.setColorFilter(f);        c.drawBitmap(bmpOriginal, 0, 0, paint);        return bmpGrayscale;    }            /**     * 去色同时加圆角     * @param bmpOriginal 原图     * @param pixels 圆角弧度     * @return 修改后的图片     */    public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {        return toRoundCorner(toGrayscale(bmpOriginal), pixels);    }        /**     * 把图片变成圆角     * @param bitmap 需要修改的图片     * @param pixels 圆角的弧度     * @return 圆角图片     */    public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);        Canvas canvas = new Canvas(output);        final int color = 0xff424242;        final Paint paint = new Paint();        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());        final RectF rectF = new RectF(rect);        final float roundPx = pixels;        paint.setAntiAlias(true);        canvas.drawARGB(0, 0, 0, 0);        paint.setColor(color);        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));        canvas.drawBitmap(bitmap, rect, rect, paint);        return output;    }       /**     * 使圆角功能支持BitampDrawable     * @param bitmapDrawable      * @param pixels      * @return     */    public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable, int pixels) {        Bitmap bitmap = bitmapDrawable.getBitmap();        bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));        return bitmapDrawable;    }}



时间类:

Java代码
  1. publicclassTimeUtil{
  2. publicstaticStringconverTime(longtimestamp){
  3. longcurrentSeconds=System.currentTimeMillis()/1000;
  4. longtimeGap=currentSeconds-timestamp;//与现在时间相差秒数
  5. StringtimeStr=null;
  6. if(timeGap>24*60*60){//1天以上
  7. timeStr=timeGap/(24*60*60)+"天前";
  8. }elseif(timeGap>60*60){//1小时-24小时
  9. timeStr=timeGap/(60*60)+"小时前";
  10. }elseif(timeGap>60){//1分钟-59分钟
  11. timeStr=timeGap/60+"分钟前";
  12. }else{//1秒钟-59秒钟
  13. timeStr="刚刚";
  14. }
  15. returntimeStr;
  16. }
  17. publicstaticStringgetStandardTime(longtimestamp){
  18. SimpleDateFormatsdf=newSimpleDateFormat("MM月dd日HH:mm");
  19. Datedate=newDate(timestamp*1000);
  20. sdf.format(date);
  21. returnsdf.format(date);
  22. }
  23. }

更多相关文章

  1. Android(安卓)gallery实现图片的左右循环旋转源码分享
  2. Theme.Holo主题 中tab同时显示图片和文字
  3. 我的Android进阶之旅------>android Matrix图片随意的放大缩小,
  4. android 通过文件名获取SD卡视频缩略图
  5. 关于android 获得 图片的方式
  6. Android编程示例之——人像检测
  7. Android(安卓)-- 图片异步上传到PHP服务器
  8. Android(安卓)-- 打开本地图片且显示路径
  9. Java乔晓松-android使用ImageSwitcher布局的电子相册&服务器获取

随机推荐

  1. 缩放图片 : matrix.setScale(0.5f,0.5f);
  2. 禁止Android中的返回键
  3. BottomNavigationView+ViewPager打造底部
  4. 内存优化二
  5. 根据IP地址定位城市
  6. ExoPlayer2.5 的简单使用
  7. smartrefreshlayout 只开启纯滚动模式
  8. Android-AsyncTask源码学习
  9. android 监听app进入后台以及从后台进入
  10. Android拆分Bitmap完整示例