【问题标题】:How to re-shape image into circular image in android?如何在android中将图像重新塑造成圆形图像?
【发布时间】:2014-05-03 00:36:12
【问题描述】:

在android中我设计了一个这样的屏幕(图1)

我从服务器获取的这张个人资料照片。暂时我正在尝试使用虚拟图像。现在我需要像这样显示相同的图像(图 2)。

是否有任何编码来实现这一点?提前致谢。

【问题讨论】:

标签: android xml android-layout android-view android-xml


【解决方案1】:

您可以在 Util 类中创建一个 util 方法为 getRoundedCornerBitmap(),如下所示...

 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 = 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 = bitmap.getWidth();

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

更多详情可以关注Rounded corner bitmaps on Android

【讨论】:

    【解决方案2】:

    在先前答案的帮助下,我想出了这个解决方案。希望它可以帮助其他人:

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.PorterDuff.Mode;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    
    
    public class CircleImage extends Activity {
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.circle_layout);
            ImageView img1 = (ImageView) findViewById(R.id.imageView1);
            Bitmap bm = BitmapFactory.decodeResource(getResources(),
                    R.drawable.hair_four);
            Bitmap resized = Bitmap.createScaledBitmap(bm, 100, 100, true);
            Bitmap conv_bm = getRoundedRectBitmap(resized, 100);
            img1.setImageBitmap(conv_bm);
            // TODO Auto-generated method stub
        }
    
        public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
            Bitmap result = null;
            try {
                result = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(result);
    
                int color = 0xff424242;
                Paint paint = new Paint();
                Rect rect = new Rect(0, 0, 200, 200);
    
                paint.setAntiAlias(true);
                canvas.drawARGB(0, 0, 0, 0);
                paint.setColor(color);
                canvas.drawCircle(50, 50, 50, paint);
                paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
                canvas.drawBitmap(bitmap, rect, rect, paint);
    
            } catch (NullPointerException e) {
            } catch (OutOfMemoryError o) {
            }
            return result;
        }
    
    }    
    

    【讨论】:

      【解决方案3】:

      您可以编写自己的图像视图并在 xml 中使用该视图。这是代码

      public class RoundedImageView extends ImageView {
      
      public RoundedImageView(Context context) {
          super(context);
          // TODO Auto-generated constructor stub
      }
      
      public RoundedImageView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
      
      public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
      }
      
      @Override
      protected void onDraw(Canvas canvas) {
      
          Drawable drawable = getDrawable();
      
          if (drawable == null) {
              return;
          }
      
          if (getWidth() == 0 || getHeight() == 0) {
              return; 
          }
          Bitmap b =  ((BitmapDrawable)drawable).getBitmap() ;
          Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
      
          int w = getWidth(), h = getHeight();
      
          Bitmap roundBitmap =  getCroppedBitmap(bitmap, w);
          canvas.drawBitmap(roundBitmap, 0,0, null);
      
      }
      
      public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
          Bitmap sbmp;
          if(bmp.getWidth() != radius || bmp.getHeight() != radius)
              sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
          else
              sbmp = bmp;
          Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),
                  sbmp.getHeight(), Config.ARGB_8888);
          Canvas canvas = new Canvas(output);
      
          final int color = 0xffa19774;
          final Paint paint = new Paint();
          final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
      
          paint.setAntiAlias(true);
          paint.setFilterBitmap(true);
          paint.setDither(true);
          canvas.drawARGB(0, 0, 0, 0);
          paint.setColor(Color.parseColor("#BAB399"));
          canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
                  sbmp.getWidth() / 2+0.1f, paint);
          paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
          canvas.drawBitmap(sbmp, rect, rect, paint);
      
      
                  return output;
      }
      
      }
      

      然后在 xml 中使用那个 imagview

      <com.mypackage.RoundedImageView
              android:id="@+id/imageview"
              ....
      .../>
      

      【讨论】:

        【解决方案4】:
        URL img_value = null;
        try {
              img_value = new URL("paste your picture url here");
        
                                        } catch (MalformedURLException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                        Bitmap mIcon1 = null;
                                        Bitmap dstBmp;
                                        try {
                                            mIcon1 = BitmapFactory
                                                    .decodeStream(img_value
                                                            .openConnection()
                                                            .getInputStream());
                                            if (mIcon1.getWidth() >= mIcon1
                                                    .getHeight()) {
        
                                                dstBmp = Bitmap.createBitmap(
                                                        mIcon1,
                                                        mIcon1.getWidth()
                                                                / 2
                                                                - mIcon1.getHeight()
                                                                / 2, 0, mIcon1
                                                                .getHeight(),
                                                        mIcon1.getHeight());
        
                                            } else {
        
                                                dstBmp = Bitmap.createBitmap(
                                                        mIcon1, 0,
                                                        mIcon1.getHeight() / 2
                                                                - mIcon1.getWidth()
                                                                / 2,
                                                        mIcon1.getWidth(),
                                                        mIcon1.getWidth());
                                            }
        
                                            circleBitmap = Bitmap.createBitmap(
                                                    dstBmp.getWidth(),
                                                    dstBmp.getHeight(),
                                                    Bitmap.Config.ARGB_8888);
                                            BitmapShader shader = new BitmapShader(
                                                    dstBmp, TileMode.CLAMP,
                                                    TileMode.CLAMP);
                                            Paint paint = new Paint();
                                            paint.setShader(shader);
        
                                            Canvas c = new Canvas(circleBitmap);
                                            c.drawCircle(dstBmp.getWidth() / 2,
                                                    dstBmp.getHeight() / 2,
                                                    dstBmp.getWidth() / 2, paint);
        
                                        } catch (IOException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                        profileImage.setAdjustViewBounds(true);
                                        profileImage.setImageBitmap(circleBitmap);
        

        【讨论】:

          【解决方案5】:

          您可以通过编程方式使用循环 ImageView 的代码。如果需要,也可以在图像周围放置圆形边框。

          Bitmap icon = BitmapFactory.decodeResource(
                      MainActivity.this.getResources(), R.drawable.yourimage);
          
               iv2.setImageBitmap(getCircularBitmap(icon));
          

          \\

          public Bitmap getCircularBitmap(Bitmap bitmap) {
          
              int width = bitmap.getWidth();
              int height = bitmap.getHeight();
          
              if (width <= height) {
                  height = width;
              } else {
                  width = height;
              }
          
              Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
          
              Canvas canvas = new Canvas(output);
          
              int pixels = 850;
              final int color = 0xff424242;
              final Paint paint = new Paint();
              final Rect rect = new Rect(0, 0, width, height);
              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);
              // to Add Boorder
              float borderSizePx = 10.0f;
              float cornerSizePx = 850f;
          
              paint.setColor(0x33ffffff);
              paint.setStyle(Paint.Style.STROKE);
              paint.setStrokeWidth((float) borderSizePx);
              canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);
          
              return output;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多