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



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;    }}


public class TimeUtil {public static String converTime(long timestamp){long currentSeconds = System.currentTimeMillis()/1000;long timeGap = currentSeconds-timestamp;//与现在时间相差秒数String timeStr = null;if(timeGap>24*60*60){//1天以上timeStr = timeGap/(24*60*60)+"天前";}else if(timeGap>60*60){//1小时-24小时timeStr = timeGap/(60*60)+"小时前";}else if(timeGap>60){//1分钟-59分钟timeStr = timeGap/60+"分钟前";}else{//1秒钟-59秒钟timeStr = "刚刚";}return timeStr;}public static String getStandardTime(long timestamp){SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");Date date = new Date(timestamp*1000);sdf.format(date);return sdf.format(date);}}
  • 大小: 2.1 KB
  • 查看图片附件

更多相关文章

  1. Android(安卓)ImageView部分圆图处理组件 xutils兼容问题
  2. 【Android】ScrollView内动态添加图片间距过大的问题
  3. android 背景图片的设置
  4. android skia 解析gif图片
  5. Android(安卓)平铺背景图片
  6. Android实现图片毛玻璃背景效果
  7. Android上图片压缩方式
  8. Android从相册选择一个图片、剪切、上传
  9. Android(安卓)ListView控件显示数据库中图片

随机推荐

  1. Translation001——android
  2. Android MVC模式你真的明白了吗??
  3. Android(安卓)2016新技术
  4. android 控件抖动
  5. Android GreenDAO ORM的使用(一) 生成DAO
  6. Android:Material Design - Style - Colo
  7. Android(安卓)的RIL驱动模块
  8. android 显示gif图片
  9. 局域网场景下Android客户端实现同数据库
  10. Learning about Android Graphics Subsys