android 图片处理

/**
    
     * 使头像变灰
    
     * @param drawable
    
     */  
    
    public static void porBecomeGrey(ImageView imageView, Drawable drawable) {  
    
        drawable.mutate();      
    
        ColorMatrix cm = new ColorMatrix();      
    
        cm.setSaturation(0);      
    
        ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);      
    
        drawable.setColorFilter(cf);   
    
        imageView.setImageDrawable(drawable);  
    

    } 


Drawable drawable = new FastBitmapDrawable(bitmap);


public byte[] getBitmapByte(Bitmap bitmap){    
    
    ByteArrayOutputStream out = new ByteArrayOutputStream();    
    
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);    
    
    try {    
    
        out.flush();    
    
        out.close();    
    
    } catch (IOException e) {    
    
        e.printStackTrace();    
    
    }    
    
    return out.toByteArray();    
    
}   


public Bitmap getBitmapFromByte(byte[] temp){    
    
    if(temp != null){    
    
        Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);    
    
        return bitmap;    
    
    }else{    
    
        return null;    
    
    }    
    
}   



/**
    
     * 将Drawable转化为Bitmap
    
     * @param drawable
    
     * @return
    
     */  


1.
    
    public static Bitmap drawableToBitmap(Drawable drawable) {  
    
        int width = drawable.getIntrinsicWidth();  
    
        int height = drawable.getIntrinsicHeight();  
    
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable  
    
                .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
    
                : Bitmap.Config.RGB_565);  
    
        Canvas canvas = new Canvas(bitmap);  
    
        drawable.setBounds(0, 0, width, height);  
    
        drawable.draw(canvas);  
    
        return bitmap;  
    
}


/**
    
    * 获取图片的倒影
    
    * @param bitmap
    
    * @return
    
    */  
    
    public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {  
    
        final int reflectionGap = 4;  
    
        int width = bitmap.getWidth();  
    
        int height = bitmap.getHeight();  
    
 
    
        Matrix matrix = new Matrix();  
    
        matrix.preScale(1, -1);  
    
 
    
        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,  
    
                width, height / 2, matrix, false);  
    
 
    
        Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  
    
                (height + height / 2), Config.ARGB_8888);  
    
 
    
        Canvas canvas = new Canvas(bitmapWithReflection);  
    
        canvas.drawBitmap(bitmap, 0, 0, null);  
    
        Paint deafalutPaint = new Paint();  
    
        canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);  
    
 
    
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
    
 
    
        Paint paint = new Paint();  
    
        LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,  
    
                bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,  
    
                0x00ffffff, TileMode.CLAMP);  
    
        paint.setShader(shader);  
    
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));  
    
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
    
                + reflectionGap, paint);  
    
        return bitmapWithReflection;  
    
    }  


/**
    
    * 把图片变成圆角  
    
    * @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;      
    
   }    


/**
    
     * 缩放图片
    
     * @param bmp
    
     * @param width
    
     * @param height
    
     * @return
    
     */  
    
    public static Bitmap PicZoom(Bitmap bmp, int width, int height) {  
    
        int bmpWidth = bmp.getWidth();  
    
        int bmpHeght = bmp.getHeight();  
    
        Matrix matrix = new Matrix();  
    
        matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);  
    
 
    
        return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);  
    
    } 


/**
    
     * @param photoPath --原图路经
    
     * @param aFile     --保存缩图
    
     * @param newWidth  --缩图宽度
    
     * @param newHeight --缩图高度
    
     */  
    
    public static boolean bitmapToFile(String photoPath, File aFile, int newWidth, int newHeight) {  
    
        BitmapFactory.Options options = new BitmapFactory.Options();  
    
        options.inJustDecodeBounds = true;  
    
        // 获取这个图片的宽和高  
    
        Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);  
    
        options.inJustDecodeBounds = false;  
    
 
    
        //计算缩放比  
    
        options.inSampleSize = reckonThumbnail(options.outWidth, options.outHeight, newWidth, newHeight);  
    
 
    
        bitmap = BitmapFactory.decodeFile(photoPath, options);  
    
 
    
        try {  
    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
    
            byte[] photoBytes = baos.toByteArray();  
    
 
    
            if (aFile.exists()) {  
    
                aFile.delete();  
    
            }  
    
            aFile.createNewFile();  
    
 
    
            FileOutputStream fos = new FileOutputStream(aFile);  
    
            fos.write(photoBytes);  
    
            fos.flush();  
    
            fos.close();  
    
 
    
            return true;  
    
        } catch (Exception e1) {  
    
            e1.printStackTrace();  
    
            if (aFile.exists()) {  
    
                aFile.delete();  
    
            }  
    
            Log.e("Bitmap To File Fail", e1.toString());  
    
            return false;  
    
        }  
    
    } 


/**
    
     * 计算缩放比
    
     * @param oldWidth
    
     * @param oldHeight
    
     * @param newWidth
    
     * @param newHeight
    
     * @return
    
     */  
    
    public static int reckonThumbnail(int oldWidth, int oldHeight, int newWidth, int newHeight) {  
    
        if ((oldHeight > newHeight && oldWidth > newWidth)  
    
                || (oldHeight <= newHeight && oldWidth > newWidth)) {  
    
            int be = (int) (oldWidth / (float) newWidth);  
    
            if (be <= 1)  
    
                be = 1;  
    
            return be;  
    
        } else if (oldHeight > newHeight && oldWidth <= newWidth) {  
    
            int be = (int) (oldHeight / (float) newHeight);  
    
            if (be <= 1)  
    
                be = 1;  
    
            return be;  
    
        }  
    
 
    
        return 1;  
    
    } 

Android边框圆角


<?xml version="1.0" encoding="utf-8"?>      
    
<shape xmlns:android="http://schema...android">        
    
    <solid android:color="#000000" />        
    
    <corners android:topLeftRadius="10dp"      
    
                    android:topRightRadius="10dp"        
    
                android:bottomRightRadius="10dp"      
    
                android:bottomLeftRadius="10dp"/>        
    
</shape>   

解释:solid的表示填充颜色,为了简单,这里用的是黑色。

而corners则是表示圆角,注意的是这里bottomRightRadius是左下角而不是右下角,bottomLeftRadius右下角。
当然上面的效果也可以像下面一样设置,如下: <corners android:radius="5dp" /> 






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值