当前位置: 代码迷 >> Android >> (转摘)Android腾讯菲薄单薄客户端开发七:图片加圆角以及时间处理工具类
  详细解决方案

(转摘)Android腾讯菲薄单薄客户端开发七:图片加圆角以及时间处理工具类

热度:81   发布时间:2016-05-01 17:18:53.0
(转摘)Android腾讯微薄客户端开发七:图片加圆角以及时间处理工具类
给图片加上圆角效果好看多了。



Java代码 复制代码?收藏代码
  1. public?class?ImageUtil?{ ??
  2. ??
  3. ????public?static?InputStream?getRequest(String?path)?throws?Exception?{ ??
  4. ????????URL?url?=?new?URL(path); ??
  5. ????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection(); ??
  6. ????????conn.setRequestMethod("GET"); ??
  7. ????????conn.setConnectTimeout(5000); ??
  8. ????????if?(conn.getResponseCode()?==?200){ ??
  9. ????????????return?conn.getInputStream(); ??
  10. ????????} ??
  11. ????????return?null; ??
  12. ????} ??
  13. ??
  14. ????public?static?byte[]?readInputStream(InputStream?inStream)?throws?Exception?{ ??
  15. ????????ByteArrayOutputStream?outSteam?=?new?ByteArrayOutputStream(); ??
  16. ????????byte[]?buffer?=?new?byte[4096]; ??
  17. ????????int?len?=?0; ??
  18. ????????while?((len?=?inStream.read(buffer))?!=?-1)?{ ??
  19. ????????????outSteam.write(buffer,?0,?len); ??
  20. ????????} ??
  21. ????????outSteam.close(); ??
  22. ????????inStream.close(); ??
  23. ????????return?outSteam.toByteArray(); ??
  24. ????} ??
  25. ???? ??
  26. ????public?static?Drawable?loadImageFromUrl(String?url){ ??
  27. ????????URL?m; ??
  28. ????????InputStream?i?=?null; ??
  29. ????????try?{ ??
  30. ????????????m?=?new?URL(url); ??
  31. ????????????i?=?(InputStream)?m.getContent(); ??
  32. ????????}?catch?(MalformedURLException?e1)?{ ??
  33. ????????????e1.printStackTrace(); ??
  34. ????????}?catch?(IOException?e)?{ ??
  35. ????????????e.printStackTrace(); ??
  36. ????????} ??
  37. ????????Drawable?d?=?Drawable.createFromStream(i,?"src"); ??
  38. ????????return?d; ??
  39. ????} ??
  40. ???? ??
  41. ????public?static?Drawable?getDrawableFromUrl(String?url)?throws?Exception{ ??
  42. ?????????return?Drawable.createFromStream(getRequest(url),null); ??
  43. ????} ??
  44. ???? ??
  45. ????public?static?Bitmap?getBitmapFromUrl(String?url)?throws?Exception{ ??
  46. ????????byte[]?bytes?=?getBytesFromUrl(url); ??
  47. ????????return?byteToBitmap(bytes); ??
  48. ????} ??
  49. ???? ??
  50. ????public?static?Bitmap?getRoundBitmapFromUrl(String?url,int?pixels)?throws?Exception{ ??
  51. ????????byte[]?bytes?=?getBytesFromUrl(url); ??
  52. ????????Bitmap?bitmap?=?byteToBitmap(bytes); ??
  53. ????????return?toRoundCorner(bitmap,?pixels); ??
  54. ????}? ??
  55. ???? ??
  56. ????public?static?Drawable?geRoundDrawableFromUrl(String?url,int?pixels)?throws?Exception{ ??
  57. ????????byte[]?bytes?=?getBytesFromUrl(url); ??
  58. ????????BitmapDrawable?bitmapDrawable?=?(BitmapDrawable)byteToDrawable(bytes); ??
  59. ????????return?toRoundCorner(bitmapDrawable,?pixels); ??
  60. ????}? ??
  61. ???? ??
  62. ????public?static?byte[]?getBytesFromUrl(String?url)?throws?Exception{ ??
  63. ?????????return?readInputStream(getRequest(url)); ??
  64. ????} ??
  65. ???? ??
  66. ????public?static?Bitmap?byteToBitmap(byte[]?byteArray){ ??
  67. ????????if(byteArray.length!=0){? ??
  68. ????????????return?BitmapFactory.decodeByteArray(byteArray,?0,?byteArray.length);? ??
  69. ????????}? ??
  70. ????????else?{? ??
  71. ????????????return?null;? ??
  72. ????????}?? ??
  73. ????} ??
  74. ???? ??
  75. ????public?static?Drawable?byteToDrawable(byte[]?byteArray){ ??
  76. ????????ByteArrayInputStream?ins?=?new?ByteArrayInputStream(byteArray); ??
  77. ????????return?Drawable.createFromStream(ins,?null); ??
  78. ????} ??
  79. ???? ??
  80. ????public?static?byte[]?Bitmap2Bytes(Bitmap?bm){? ??
  81. ????????ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream(); ??
  82. ????????bm.compress(Bitmap.CompressFormat.PNG,?100,?baos); ??
  83. ????????return?baos.toByteArray(); ??
  84. ????} ??
  85. ???? ??
  86. ????public?static?Bitmap?drawableToBitmap(Drawable?drawable)?{ ??
  87. ??
  88. ????????Bitmap?bitmap?=?Bitmap ??
  89. ????????????????.createBitmap( ??
  90. ????????????????????????drawable.getIntrinsicWidth(), ??
  91. ????????????????????????drawable.getIntrinsicHeight(), ??
  92. ????????????????????????drawable.getOpacity()?!=?PixelFormat.OPAQUE???Bitmap.Config.ARGB_8888 ??
  93. ????????????????????????????????:?Bitmap.Config.RGB_565); ??
  94. ????????Canvas?canvas?=?new?Canvas(bitmap); ??
  95. ????????drawable.setBounds(0,?0,?drawable.getIntrinsicWidth(), ??
  96. ????????????????drawable.getIntrinsicHeight()); ??
  97. ????????drawable.draw(canvas); ??
  98. ????????return?bitmap; ??
  99. ????} ??
  100. ???? ??
  101. ????????/** ?
  102. ??????????*?图片去色,返回灰度图片 ?
  103. [email protected] ?
  104. [email protected] ?
  105. ?????????*/??
  106. ????????public?static?Bitmap?toGrayscale(Bitmap?bmpOriginal)?{ ??
  107. ????????????int?width,?height; ??
  108. ????????????height?=?bmpOriginal.getHeight(); ??
  109. ????????????width?=?bmpOriginal.getWidth();???? ??
  110. ???? ??
  111. ????????????Bitmap?bmpGrayscale?=?Bitmap.createBitmap(width,?height,?Bitmap.Config.RGB_565); ??
  112. ????????????Canvas?c?=?new?Canvas(bmpGrayscale); ??
  113. ????????????Paint?paint?=?new?Paint(); ??
  114. ????????????ColorMatrix?cm?=?new?ColorMatrix(); ??
  115. ????????????cm.setSaturation(0); ??
  116. ????????????ColorMatrixColorFilter?f?=?new?ColorMatrixColorFilter(cm); ??
  117. ????????????paint.setColorFilter(f); ??
  118. ????????????c.drawBitmap(bmpOriginal,?0,?0,?paint); ??
  119. ????????????return?bmpGrayscale; ??
  120. ????????} ??
  121. ???????? ??
  122. ???????? ??
  123. ????????/** ?
  124. ?????????*?去色同时加圆角 ?
  125. [email protected] ?
  126. [email protected] ?
  127. [email protected] ?
  128. ?????????*/??
  129. ????????public?static?Bitmap?toGrayscale(Bitmap?bmpOriginal,?int?pixels)?{ ??
  130. ????????????return?toRoundCorner(toGrayscale(bmpOriginal),?pixels); ??
  131. ????????} ??
  132. ???????? ??
  133. ????????/** ?
  134. ?????????*?把图片变成圆角 ?
  135. [email protected] ?
  136. [email protected] ?
  137. [email protected] ?
  138. ?????????*/??
  139. ????????public?static?Bitmap?toRoundCorner(Bitmap?bitmap,?int?pixels)?{ ??
  140. ???? ??
  141. ????????????Bitmap?output?=?Bitmap.createBitmap(bitmap.getWidth(),?bitmap.getHeight(),?Config.ARGB_8888); ??
  142. ????????????Canvas?canvas?=?new?Canvas(output); ??
  143. ???? ??
  144. ????????????final?int?color?=?0xff424242; ??
  145. ????????????final?Paint?paint?=?new?Paint(); ??
  146. ????????????final?Rect?rect?=?new?Rect(0,?0,?bitmap.getWidth(),?bitmap.getHeight()); ??
  147. ????????????final?RectF?rectF?=?new?RectF(rect); ??
  148. ????????????final?float?roundPx?=?pixels; ??
  149. ???? ??
  150. ????????????paint.setAntiAlias(true); ??
  151. ????????????canvas.drawARGB(0,?0,?0,?0); ??
  152. ????????????paint.setColor(color); ??
  153. ????????????canvas.drawRoundRect(rectF,?roundPx,?roundPx,?paint); ??
  154. ???? ??
  155. ????????????paint.setXfermode(new?PorterDuffXfermode(Mode.SRC_IN)); ??
  156. ????????????canvas.drawBitmap(bitmap,?rect,?rect,?paint); ??
  157. ???? ??
  158. ????????????return?output; ??
  159. ????????} ??
  160. ???? ??
  161. ???????? ??
  162. ???????/** ?
  163. ?????????*?使圆角功能支持BitampDrawable ?
  164. [email protected] ?
  165. [email protected] ?
  166. [email protected] ?
  167. ?????????*/??
  168. ????????public?static?BitmapDrawable?toRoundCorner(BitmapDrawable?bitmapDrawable,?int?pixels)?{ ??
  169. ????????????Bitmap?bitmap?=?bitmapDrawable.getBitmap(); ??
  170. ????????????bitmapDrawable?=?new?BitmapDrawable(toRoundCorner(bitmap,?pixels)); ??
  171. ????????????return?bitmapDrawable; ??
  172. ????????} ??
  173. ???? ??
  174. }??
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. public?class?TimeUtil?{ ??
  2. ???? ??
  3. ????public?static?String?converTime(long?timestamp){ ??
  4. ????????long?currentSeconds?=?System.currentTimeMillis()/1000; ??
  5. ????????long?timeGap?=?currentSeconds-timestamp;//与现在时间相差秒数 ??
  6. ????????String?timeStr?=?null; ??
  7. ????????if(timeGap>24*60*60){//1天以上 ??
  8. ????????????timeStr?=?timeGap/(24*60*60)+"天前"; ??
  9. ????????}else?if(timeGap>60*60){//1小时-24小时 ??
  10. ????????????timeStr?=?timeGap/(60*60)+"小时前"; ??
  11. ????????}else?if(timeGap>60){//1分钟-59分钟 ??
  12. ????????????timeStr?=?timeGap/60+"分钟前"; ??
  13. ????????}else{//1秒钟-59秒钟 ??
  14. ????????????timeStr?=?"刚刚"; ??
  15. ????????} ??
  16. ????????return?timeStr; ??
  17. ????} ??
  18. ???? ??
  19. ????public?static?String?getStandardTime(long?timestamp){ ??
  20. ????????SimpleDateFormat?sdf?=?new?SimpleDateFormat("MM月dd日?HH:mm"); ??
  21. ????????Date?date?=?new?Date(timestamp*1000); ??
  22. ????????sdf.format(date); ??
  23. ????????return?sdf.format(date); ??
  24. ????} ??
  25. }??
  26. http://helloandroid.iteye.com/blog/1136297
  相关解决方案