【问题标题】:Trying to convert image from rectangle shape to circular [duplicate]试图将图像从矩形转换为圆形[重复]
【发布时间】:2014-05-20 11:33:59
【问题描述】:

我正在尝试将 imageview 转换为 60px 圆形图像,但它没有发生...

我正在尝试的方式是......

File imgFile = new  File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    "MyCameraApp" + File.separator + "profile_" + userId + ".jpg");

        if(imgFile.exists()){
            Bitmap correctBmp=null;

              ExifInterface exif;
            try {
                exif = new ExifInterface(imgFile.getPath());
                  int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

                    int angle = 0;

                    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                           angle = 90;
                    }
                    else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                           angle = 180;
                    }
                    else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                           angle = 270;
                    }

                    Matrix mat = new Matrix();
                    mat.postRotate(90);



                  Bitmap bmp1 = BitmapFactory.decodeStream(new FileInputStream(imgFile), null, null);
                  /* correctBmp = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(), bmp1.getHeight(), mat, true);*/
                  correctBmp = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(),  bmp1.getConfig());
                  Canvas canvas = new Canvas(correctBmp);
                   Paint paint = new Paint();
                   paint.setAntiAlias(true);
                   paint.setShader(new BitmapShader(bmp1, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
                   canvas.drawRoundRect((new RectF(0.0f, 0.0f, bmp1.getWidth(), bmp1.getHeight())), 10, 10, paint);
                   profileImage.setImageBitmap(correctBmp);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                  }

它正在加载矩形形状的图像,我该怎么办?

请帮忙...

【问题讨论】:

  • 为什么不用xml android:background给图片添加圆形背景
  • 这不是一个好主意,实际上我这样做了,但没有变得更好看...@IllegalArgument
  • 如果您知道图像的高度,我请求使用椭圆形并提供半径,然后您可以指定图像高度的一半的角半径这样做不需要任何 java 代码,所以我认为它值得一枪
  • 好的,我明白了,谢谢@Illegal Argument

标签: android bitmap android-canvas


【解决方案1】:

这是我的代码,它适用于我:

public class ImageHelper {

public Bitmap getCroppedBitmap(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());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //return _bmp;
    return output;
}   

}

只需创建一个类并将这段代码放入其中,然后您就可以像这样使用它:

// create round image
public void setCircularImage() {

    ImageView imv = (ImageView) findViewById(R.id.imageView1);
    Bitmap bitImg = BitmapFactory.decodeResource(getResources(), R.drawable.icon_person);
    ImageHelper imgHlp = new ImageHelper();
    imv.setImageBitmap(imgHlp.getCroppedBitmap(bitImg));   
}

【讨论】:

    【解决方案2】:

    为此,您可以使用通用图像加载器。 您可以通过创建图像加载器选项轻松使图像四舍五入

    options = new DisplayImageOptions.Builder()
            .displayer(new RoundedBitmapDisplayer(50))
            .showStubImage(R.drawable.ic_app)
            .showImageForEmptyUri(R.drawable.camera)
            .showImageOnFail(R.drawable.ic_error)
            .cacheOnDisc()
            .build();
    

    您可以多参考here

    【讨论】:

      【解决方案3】:

      见下面的链接:-

      Add border to getRoundedCornerBitmap android

      或使用通用加载器

      DisplayImageOptions options = new DisplayImageOptions.Builder()
      .cacheInMemory(true)
      .cacheOnDisc(true)
      .displayer(new RoundedBitmapDisplayer(60))
      .build();//value which you want to round
      
      ImageLoader.getInstance().displayImage(Uri.parse(imgByURL).toString(), imgThumb, options);
      

      【讨论】:

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