Android 对图像的操作

如果要两张Bitmap合成一张Bitmap图像,代码如下:

 

public static Bitmap createBitmap( Bitmap src, Bitmap watermark ) {

         if(>null ) {

             return null;

         }

         int>

         int>

         int>

         int>

         //create the new blank bitmap

         Bitmap newb = Bitmap.createBitmap(w,>ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图

         Canvas cv = new Canvas( newb );//创建一个Canvas的实例。

     

         cv.drawBitmap( src, 0,0, null );//把第一张图像画在画布上。

      

         cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null );// 将第二张图像画在画布上。

     

         cv.save( Canvas.ALL_SAVE_FLAG );//保存画布

         //store

         cv.restore();//存储

         return newb;

    }

 

参数:src代表其中一张图像,watermark代表令一张图像。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

设置Bitmap图片的透明度:

 

public  static BitmapsetAlpha(Bitmap sourceImg, int number) {

      

int[]> new int[sourceImg.getWidth()*sourceImg.getHeight()];//获取代表图片的矩阵

sourceImg.getPixels(argb, 0,sourceImg.getWidth(), 0, 0,sourceImg.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值

       number = number * 255 / 100;

       for (int i = 0; i < argb.length; i++) {

       argb[i] = (number << 24) |(argb[i] & 0x00FFFFFF);// 修改最高2位的值

       }

       sourceImg = Bitmap.createBitmap(argb,sourceImg.getWidth(), sourceImg.getHeight(), Bitmap.Config.ARGB_8888);//根据新的矩阵创建图片

 

        

 

       return sourceImg;//返回图片

       }

 

参数:sourceImg要设置透明度的图片,number,要设置的透明度的数值

 

 

 

 

 

 

改变图皮的大小:

 

public static Bitmap decodeBitmap(String path, int displayWidth, int displayHeight){

       BitmapFactory.Options op = new BitmapFactory.Options();//获取图片的Options对象,用于存储图片的信息

       op.inJustDecodeBounds = true;

       Bitmap bmp = BitmapFactory.decodeFile(path, op); //根据图片的路径,将图片的信息存放到 Options对象中。

       //获取比例大小

       int>int)Math.ceil(op.outWidth/(float)displayWidth);

       int>int)Math.ceil(op.outHeight/(float)displayHeight);

       //如果超出指定大小,则缩小相应的比例

       if(wRatio > 1 && hRatio > 1){

           if(wRatio > hRatio){

              op.inSampleSize = wRatio;

           }else{

              op.inSampleSize = hRatio;

           }

       }

       op.inJustDecodeBounds = false;

       bmp = BitmapFactory.decodeFile(path, op);

       return Bitmap.createScaledBitmap(bmp, displayWidth,displayHeight, true);//返回处理后的图像

    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

由Drawable转化成Bitmap

public   static  Bitmap drawableToBitmap(Drawable drawable) {

       if(drawable==null) return null;

        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;

    }  

   

 

 

由Bitmap转化成Drawable

    public static Drawable bitmapToDrawable(Bitmap bitmap)

    {

       Drawable drawable = new BitmapDrawable(bitmap);

       return drawable;

    }

 

 

 

 

 

按照比例放大缩小图片:

 

view sourceprint?

01

publicstaticBitmap zoomBitmap(Bitmap bitmap,intw,inth){   

02

        intwidth = bitmap.getWidth();   

 

03

        intheight = bitmap.getHeight();   

04

        Matrix matrix = newMatrix();   

 

05

        floatscaleWidht = ((float)w / width);   

06

        floatscaleHeight = ((float)h / height);   

 

07

        matrix.postScale(scaleWidht, scaleHeight);   

08

        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);   

 

09

        returnnewbmp;   

10

    }

获得圆角图片的方法:

 

 

view sourceprint?

01

publicstaticBitmap getRoundedCornerBitmap(Bitmap bitmap,floatroundPx){   

02

             

 

03

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);   

04

        Canvas canvas = newCanvas(output);   

 

05

      

06

        finalintcolor = 0xff424242;   

 

07

        finalPaint paint = newPaint();   

08

        finalRect rect = newRect(0, 0, bitmap.getWidth(), bitmap.getHeight());   

 

09

        finalRectF rectF = newRectF(rect);   

10

      

 

11

        paint.setAntiAlias(true);   

12

        canvas.drawARGB(0, 0, 0, 0);   

 

13

        paint.setColor(color);   

14

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);   

 

15

      

16

        paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));   

 

17

        canvas.drawBitmap(bitmap, rect, rect, paint);   

18

      

 

19

        returnoutput;   

20

    }

获得带倒影的图片方法 

 

 

view sourceprint?

01

publicstaticBitmap createReflectionImageWithOrigin(Bitmap bitmap){   

02

       finalintreflectionGap = 4;   

 

03

       intwidth = bitmap.getWidth();   

04

       intheight = bitmap.getHeight();   

 

05

            

06

       Matrix matrix = newMatrix();   

 

07

       matrix.preScale(1, -1);   

08

            

 

09

       Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height/2, width, height/2, matrix, false);   

10

            

 

11

       Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);   

12

            

 

13

       Canvas canvas = newCanvas(bitmapWithReflection);   

14

       canvas.drawBitmap(bitmap, 0, 0, null);   

 

15

       Paint deafalutPaint = newPaint();   

16

       canvas.drawRect(0, height,width,height + reflectionGap, deafalutPaint);   

 

17

            

18

       canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);   

 

19

            

20

       Paint paint = newPaint();   

 

21

       LinearGradient shader = newLinearGradient(0, bitmap.getHeight(), 0,

view sourceprint?

01

     bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);   

02

        paint.setShader(shader);   

 

03

        // Set the Transfer mode to be porter duff and destination in   

04

        paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));   

 

05

        // Draw a rectangle using the paint with our linear gradient   

06

        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()   

 

07

                + reflectionGap, paint);   

08

      

 

09

         returnbitmapWithReflection;   

10

    }

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值