简单记录Android设置圆角的步骤
来源:爱站网时间:2020-06-29编辑:网友分享
在开发过程中,我们可能需要把图片设置成圆角的,这个设置方法有几种,今天爱站技术频道小编就为大家介绍一种操作方法,别着急,下面是简单记录Android设置圆角的步骤的介绍,一起参考下文看看吧!
在开发过程中,我们可能需要把图片设置成圆角的,这个设置方法有几种,今天爱站技术频道小编就为大家介绍一种操作方法,别着急,下面是简单记录Android设置圆角的步骤的介绍,一起参考下文看看吧!
主要原理是使用系统自带api:
RoundedBitmapDrawableFactory
先上效果图:

由于比较简单,直接给出实现方式:
public class MainActivity extends AppCompatActivity {
private ImageView mImgRectRound;
private ImageView mImgRound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImgRectRound = (ImageView) findViewById(R.id.img_rect_rounded);
mImgRound = (ImageView) findViewById(R.id.img_rounded);
rectRoundBitmap();
roundBitmap();
}
private void rectRoundBitmap(){
//得到资源文件的BitMap
Bitmap image= BitmapFactory.decodeResource(getResources(),R.drawable.dog);
//创建RoundedBitmapDrawable对象
RoundedBitmapDrawable roundImg =RoundedBitmapDrawableFactory.create(getResources(),image);
//抗锯齿
roundImg.setAntiAlias(true);
//设置圆角半径
roundImg.setCornerRadius(30);
//设置显示图片
mImgRectRound.setImageDrawable(roundImg);
}
private void roundBitmap(){
//如果是圆的时候,我们应该把bitmap图片进行剪切成正方形, 然后再设置圆角半径为正方形边长的一半即可
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
Bitmap bitmap = null;
//将长方形图片裁剪成正方形图片
if (image.getWidth() == image.getHeight()) {
bitmap = Bitmap.createBitmap(image, image.getWidth() / 2 - image.getHeight() / 2, 0, image.getHeight(), image.getHeight());
} else {
bitmap = Bitmap.createBitmap(image, 0, image.getHeight() / 2 - image.getWidth() / 2, image.getWidth(), image.getWidth());
}
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
//圆角半径为正方形边长的一半
roundedBitmapDrawable.setCornerRadius(bitmap.getWidth() / 2);
//抗锯齿
roundedBitmapDrawable.setAntiAlias(true);
mImgRound.setImageDrawable(roundedBitmapDrawable);
}
}
布局文件:
如有问题,欢迎指正,谢谢。
关于简单记录Android设置圆角的步骤内容,爱站技术频道小编今天就和大家聊到这里,我们在无法判断的使用哪种操作的情况下,一定要静待时机,才能开始敲打键盘哦。
