Bitmap 创建、转换、圆角、设置透明度

指定一个色值生成bitmap

 

   public Bitmap getBackGroundBitmap(int color) {

                     Paint p = new Paint();

                     p.setColor(Color.RED);

                     Bitmap bitmap = Bitmap.createBitmap(190, 110, Bitmap.Config.ARGB_8888);

                     Canvas canvas = new Canvas(bitmap);

                     canvas.drawColor(android.R.color.transparent);

                     canvas.drawColor(color);

                     return bitmap;

       }

 

设置bitmap的透明度

 

   public static Bitmap getAlplaBitmap(Bitmap sourceImg, int number) {

          int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];

          sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg.getWidth(), sourceImg.getHeight());

          number = number * 255 / 100;

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

                argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF);

          }

           sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);

        return sourceImg;

   }

 

drawable转换成bitmap

 

   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;

   }

 

bitmap圆角  

   public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

           Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),

               bitmap.getHeight(), Config.ARGB_8888);

           Canvas canvas = new Canvas(output);

           final int color = Color.RED

           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 = 6;

           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;

   }

 

 

 

 

posted @   渣娃  阅读(5673)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示