gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:13:51 25 4
gpt4 key购买 nike

我有一个 ListView,其中有一个 ImageViewImageView 中的图像在从服务器获取后动态加载。现在,我希望这些任何尺寸的图像都适合圆形框架,该怎么做?这是我想要的示例图片

enter image description here

最佳答案

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

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

}

关于android - 如何使图像适合android中的圆形框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14050813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com