本篇文章給大家分享的是有關(guān)如何給Android應(yīng)用設(shè)置圓角圖片,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

主要原理是使用系統(tǒng)自帶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);
  //創(chuàng)建RoundedBitmapDrawable對象
  RoundedBitmapDrawable roundImg =RoundedBitmapDrawableFactory.create(getResources(),image);
  //抗鋸齒
  roundImg.setAntiAlias(true);
  //設(shè)置圓角半徑
  roundImg.setCornerRadius(30);
  //設(shè)置顯示圖片
  mImgRectRound.setImageDrawable(roundImg);
 }
 private void roundBitmap(){
  //如果是圓的時候,我們應(yīng)該把bitmap圖片進(jìn)行剪切成正方形, 然后再設(shè)置圓角半徑為正方形邊長的一半即可
  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應(yīng)用設(shè)置圓角圖片-創(chuàng)新互聯(lián)
                
                文章路徑:http://www.yijiale78.com/article42/jgjec.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、建站公司、品牌網(wǎng)站設(shè)計、小程序開發(fā)、自適應(yīng)網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
