【问题标题】:I want to transform a normal rectangular image to a circular image programmatically我想以编程方式将普通矩形图像转换为圆形图像
【发布时间】:2012-11-02 16:06:32
【问题描述】:

我想以编程方式将普通矩形图像转换为圆形图像,但不使用 xml,请参阅此链接 Navigation Drawer (Google+ vs. YouTube)。到目前为止,我关注了这个链接How to set bitmap in circular imageview?,但我只是让它在我的图像上画出黑圈。我该怎么办?

【问题讨论】:

    标签: android android-image bmp


    【解决方案1】:
    Bitmap bitmap = BitmapFactory.decodeResource(convertView.getResources(), R.drawable.ic_launcher);
                    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    
                    BitmapShader shader = new BitmapShader (bitmap,  TileMode.CLAMP, TileMode.CLAMP);
                    Paint paint = new Paint();
                    paint.setShader(shader);
                    paint.setAntiAlias(true);
    
                    Canvas c = new Canvas(circleBitmap);
                    c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint);
    
                    imgProfilePic.setImageBitmap(circleBitmap);
    

    【讨论】:

      【解决方案2】:
      public Bitmap dstBmp;
      Bitmap getRoundedBitmapnow(Bitmap bitmap) 
      {
       //         convert rectangle to square
              if (bitmap.getWidth() >= bitmap.getHeight()){
      
                dstBmp = Bitmap.createBitmap(
                bitmap, 
                bitmap.getWidth()/2 - bitmap.getHeight()/2,
                   0,
                   bitmap.getHeight(), 
                   bitmap.getHeight()
                   );
      
              }else{
      
                dstBmp = Bitmap.createBitmap(
                bitmap,
                   0, 
                   bitmap.getHeight()/2 - bitmap.getWidth()/2,
                   bitmap.getWidth(),
                   bitmap.getWidth() 
                   );
              }
              //          create circle
              Bitmap output = Bitmap.createBitmap(dstBmp.getWidth(),
              dstBmp.getHeight(), Config.ARGB_8888);
              Canvas canvas = new Canvas(output);
      
              final int color = 0xff424242;
              final Paint paint = new Paint();
      
              final Rect rect1 = new Rect(0, 0, dstBmp.getWidth(), dstBmp.getHeight());
              final RectF rectF1 = new RectF(rect1);
      
      
              paint.setAntiAlias(true);
              canvas.drawARGB(0, 0, 0, 0);
              paint.setColor(color);
              canvas.drawOval(rectF1, paint);
      
              paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
              canvas.drawBitmap(dstBmp, rect1, rect1, paint);
      
              return output;
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-10-08
        • 2013-03-22
        • 1970-01-01
        • 2014-05-09
        • 1970-01-01
        • 2012-03-24
        • 2011-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多