网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何给Android应用设置圆角图片

本篇文章给大家分享的是有关如何给Android应用设置圆角图片,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

沙市ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

主要原理是使用系统自带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);
 }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>


 

 

以上就是如何给Android应用设置圆角图片,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


当前标题:如何给Android应用设置圆角图片
文章转载:http://bjjierui.cn/article/ispsjo.html

其他资讯