如何在android中使图像适合圆形框架

问题描述 投票:0回答:8

我有一个

ListView
,其中有一个
ImageView
ImageView
中的图像在从服务器获取后动态加载。 现在,我想要将这些任意大小的图像放入圆形框架中,该怎么做? 这是我想要的示例图片

enter image description here

android bitmap imageview
8个回答
24
投票

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

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

 }

9
投票

试试这个代码:

public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
try {
result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
canvas = new Canvas(result);

color = 0xff424242;
paint = new Paint();
rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
rectF = new RectF(rect);
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);
} catch (NullPointerException e) {
// return bitmap;
} catch (OutOfMemoryError o){}
return result;
}

如果你想要一个实际的圆,那么你可以传递

100px
作为参数。


8
投票

更新

Github 上有

CircleImageView
可用。

您可以从 Maven 存储库 获取最新版本作为 gradle 依赖项添加。



1
投票

我们可以通过 xml 代码管理图像的高度和宽度,并通过 java 代码绘制圆形/椭圆形,例如

    <ImageView
            android:id="@+id/imageView1"
            android:layout_width="@dimen/width"
            android:layout_height="@dimen/height"
            />

椭圆形视图

ImageView img1 = (ImageView) findViewById(R.id.imageView1);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
        R.drawable.user_image);
Bitmap conv_bm = getRoundedBitmap(bm);
img1.setImageBitmap(conv_bm);


public static Bitmap getRoundedBitmap(Bitmap bitmap)
{
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final 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);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    bitmap.recycle();

    return output;
  }

 }

0
投票

添加以下依赖项

 implementation 'jp.wasabeef:picasso-transformations:2.2.1'
 implementation 'de.hdodenhof:circleimageview:3.0.0'

CircularImageView 可用于在图像看起来不正确的情况下使图像适合圆形。resize 用于在圆形图像视图中调整图像大小。

    CircleImageView img;
    String Imageid; 

    Imageid="ImageName"; //String is not compulsary it may be drawable  

    Picasso.with(mContext)
            .load(Imageid.get(position)) //Load the image
            .error(R.drawable.ic_launcher_background) //Image resource for error
            .resize(20, 20)  // Post processing - Resizing the image
            .into(img); // View where image is loaded.

0
投票
如果您使用 Glide 加载图像,

circleCrop() 将会有所帮助。

  Glide.with(context)
    .load(imageUrl)
    .circleCrop()
    .into(imageView)

-1
投票
 public static Bitmap getCircleBitmap(Bitmap bitmap) {
        final Bitmap circuleBitmap = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getWidth(), Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(circuleBitmap);

        final int color = Color.RED;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getWidth());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawOval(rectF, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        bitmap.recycle();

        return circuleBitmap;
    }
© www.soinside.com 2019 - 2024. All rights reserved.