`

Android 图片加圆角以及时间处理工具类

 
阅读更多

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



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

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(00, drawable.getIntrinsicWidth(),   
  96.                 drawable.getIntrinsicHeight());   
  97.         drawable.draw(canvas);   
  98.         return bitmap;   
  99.     }   
  100.        
  101.         /**  
  102.           * 图片去色,返回灰度图片  
  103.           * @param bmpOriginal 传入的图片  
  104.          * @return 去色后的图片  
  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, 00, paint);   
  119.             return bmpGrayscale;   
  120.         }   
  121.            
  122.            
  123.         /**  
  124.          * 去色同时加圆角  
  125.          * @param bmpOriginal 原图  
  126.          * @param pixels 圆角弧度  
  127.          * @return 修改后的图片  
  128.          */  
  129.         public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {   
  130.             return toRoundCorner(toGrayscale(bmpOriginal), pixels);   
  131.         }   
  132.            
  133.         /**  
  134.          * 把图片变成圆角  
  135.          * @param bitmap 需要修改的图片  
  136.          * @param pixels 圆角的弧度  
  137.          * @return 圆角图片  
  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(00, bitmap.getWidth(), bitmap.getHeight());   
  147.             final RectF rectF = new RectF(rect);   
  148.             final float roundPx = pixels;   
  149.        
  150.             paint.setAntiAlias(true);   
  151.             canvas.drawARGB(0000);   
  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.          * @param bitmapDrawable   
  165.          * @param pixels   
  166.          * @return  
  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. }  
分享到:
评论

相关推荐

    安卓头像制作图片圆角剪裁相关-兼容Android7.0的图片选择工具类.rar

    这个工具类通常会涉及到用户选择图片、处理图片(如圆角剪裁)、显示预览以及保存结果等核心功能。在"安卓头像制作图片圆角剪裁相关-兼容Android7.0的图片选择工具类.rar"中,我们可能找到以下关键知识点: 1. **...

    Android-RoundRectLayout圆角矩形的绘制方案解决任意圆角和圆形图片

    在Android应用开发中,UI设计往往需要实现各种各样的图形效果,其中圆角矩形和圆形图片是最常见的需求之一。`RoundRectLayout`是专门为解决此类问题而设计的一个自定义布局,它允许开发者轻松地创建具有任意圆角和...

    android工具类 26个实用工具类

    5. **图片处理工具类**:可能包括图片的加载、缩放、裁剪、圆角处理等功能,如`ImageUtil`,可以优化图片加载的性能,避免内存溢出。 6. **文件操作工具类**:如`FileUtil`,提供文件的创建、删除、复制、移动、...

    Android图片处理工具类,包括: 图片查看、照片墙、bitmap转存、圆角、剪切、图片加载缓存、图片压缩等

    Android图片处理工具类,包括: 图片查看、照片墙、bitmap转存、圆角、剪切、图片加载缓存、图片压缩等 [注:本内容来自网络,在此分享仅为帮助有需要的网友,如果侵犯了您的权利,麻烦联系我,我会第一时间删除,...

    android开发必备工具类

    - `Glide`或`Picasso`是常见的图片加载库,通过工具类可以实现图片的加载、裁剪、圆角处理等功能,并能处理内存和磁盘缓存,防止内存溢出。 4. **权限管理工具类**: - 针对Android 6.0(API 23)以上的运行时...

    Glide加载圆形图片 自定义圆角 和对指定角加载圆角

    在Android开发中,图片加载库Glide是广泛使用的工具,它能够高效地处理图片的加载、缓存和显示。在一些设计中,我们可能需要将图片显示为圆形或者具有特定圆角的效果,以达到更好的视觉体验。本篇将详细介绍如何使用...

    Android处理视图圆角和色彩的工具类

    Android代码处理视图圆角和色彩的工具类,供大家参考,具体内容如下 一直都用的.XML文件处理圆角与色彩或色彩渐变,觉得很不方便,后来发现了GradientDrawable这个类,就整了个工具类,用起来觉得挺方便 效果图: ...

    android 开发常用到的工具类集锦

    3. **图片处理工具类**:在Android应用中,图片加载和处理是必不可少的,比如图片的缩放、裁剪、圆角转换等。 Glide 和 Picasso 是两个常用的图片加载库,它们不仅能够优化内存管理,还能实现图片的缓存策略,提高...

    Android第三方轮播框架youth圆角处理两种方式

    还可能有一个`BannerUtils`工具类,包含了处理圆角的函数。在实现圆角效果时,通常有两种方式: 1. 自定义视图:通过继承`ImageView`或者框架提供的轮播项视图,重写`onDraw()`方法,使用`Canvas`的`drawRoundRect...

    android 常用快速开发集成工具类

    5. **图片处理**:如图片裁剪、缩放、圆角处理,以及像Glide、Picasso这样的图片加载库的封装,优化图片显示。 6. **权限管理**:针对Android 6.0(API 23)及以上版本的动态权限申请的工具类,简化权限申请流程。 ...

    Android-可以指定圆角的ViewGroup

    在Android开发中,创建具有指定圆角的ViewGroup是一个常见的需求,这可以帮助开发者设计出更加美观和具有现代感的用户界面。"Android-可以指定圆角的ViewGroup"这个主题聚焦于如何在Android应用中实现自定义的布局,...

    Android-圆角背景的SpannableString后期会加入些许封装

    3. **复用性**:创建一个通用的工具类或组件,使得开发者可以通过简单的API调用就能轻松地为任何文本添加圆角背景。 4. **性能优化**:考虑到性能,避免在每次更新时都重新绘制Drawable,可以利用缓存机制提高效率。...

    android 图象处理工具类

    android端对图片的一些处理功能: 图片去色,返回灰度图片 去色同时加圆角 把图片变成圆角 使圆角功能支持BitampDrawable 读取路径中的图片,然后将其转化为缩放后的bitmap 保存图片为PNG 保存图片为JPEG 图片合成

    Android常用工具类

    4. **图片处理工具类**: - `ImageLoader`:用于加载、缓存和显示图片,可能包括从网络、本地文件系统或资源加载图片,并支持图片的缩放、裁剪和圆角处理。 5. **文件操作工具类**: - `FileUtils`:提供了文件的...

    Android应用源码之Image(图片工具类大全)_源码.zip

    这个名为"Android应用源码之Image(图片工具类大全)_源码.zip"的压缩包文件显然包含了用于处理图片的各种工具类,这些工具类可以帮助开发者高效地管理和操作应用中的图像资源。下面我们将深入探讨其中可能涉及的关键...

    自定义圆角图片Demo

    这个"自定义圆角图片Demo"项目就是一个很好的实践,它涵盖了如何自定义一个ImageView来实现图片显示为圆角,以及如何处理可能出现的锯齿问题。在本文中,我们将深入探讨这两个关键知识点。 首先,让我们来看看如何...

    android ImageView网络图片加载、动态设置尺寸、圆角(绝对好用)

    在Android开发中,ImageView是用于显示图像的常见组件,它广泛应用于各种场景,如用户头像、应用图标等。在实际应用中,我们不仅需要显示本地资源...在实际项目中,这样的类可以作为通用的工具类,方便在多个地方复用。

    Android图片圆角化的代码

    在Android开发中,为了提升应用界面的美观度和用户体验,我们常常需要对图片进行圆角化处理。这个程序就是专门针对这一需求而设计的,它能够将原本矩形的图片转换为具有圆角的图像。圆角的大小可以根据开发者的需求...

    Android-安卓常用工具类

    4. **图片加载工具类**:如`Glide`、`Picasso`等,它们用于高效地加载和显示网络或本地的图片,支持缓存、裁剪、圆角处理等功能,避免内存溢出。 5. **权限管理工具类**:在Android 6.0及以上版本,需要动态申请...

Global site tag (gtag.js) - Google Analytics