中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android設置圖片圓角的方法

發布時間:2020-09-22 16:37:02 來源:腳本之家 閱讀:229 作者:lza qq_895860866 欄目:移動開發

Android中經常會遇到對圖片進行二次處理,例如加圓角,或者顯示圓形圖片

實現的效果圖:

Android設置圖片圓角的方法

方法一:

通過第三方框架Glide實現圖片顯示有圓角,有三種寫法如下:

1.1、第一種實現:

RequestOptions options = new RequestOptions().error(R.drawable.img_load_failure).bitmapTransform(new RoundedCorners(30));//圖片圓角為30
Glide.with(this).load(URL) //圖片地址
    .apply(options)
    .into(ImagView);

1.2、第二種實現:

RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_launcher_background);
requestOptions.circleCropTransform();
requestOptions.transforms( new RoundedCorners(30));
Glide.with(this).load(URL) //圖片地址
    .apply(options)
    .into(ImagView);

1.3、第三種實現:

RequestOptions options = new RequestOptions().centerCrop() .transform(new RoundTransform(this,30)); 
Glide.with(this).load(URL) //圖片地址
    .apply(options)
    .into(ImagView);
public class RoundTransform extends BitmapTransformation { 
 private static float radius = 0f; 
 public RoundTransform(Context context) { 
  this(context, 4); 
 } 
 
 public RoundTransform(Context context, int dp) { 
  super(context); 
  this.radius = Resources.getSystem().getDisplayMetrics().density * dp; 
 } 
 
 @Override 
 protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 
  Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight); 
  return roundCrop(pool, bitmap); 
 } 
 
 private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { 
  if (source == null) return null; 
  Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); 
  if (result == null) { 
   result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); 
  } 
 
  Canvas canvas = new Canvas(result); 
  Paint paint = new Paint(); 
  paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); 
  paint.setAntiAlias(true); 
  RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); 
  canvas.drawRoundRect(rectF, radius, radius, paint); 
  return result; 
 } 
 
 public String getId() { 
  return getClass().getName() + Math.round(radius); 
 } 
 
 @Override 
 public void updateDiskCacheKey(MessageDigest messageDigest) { 
 
 }
}

方法二:

自定義ImageView:

<ImageView
  android:id="@+id/iv"
  android:layout_width="300dp"
  android:layout_height="300dp"
  android:layout_centerHorizontal="true"
  />
ImageView iv = findViewById(R.id.iv); 
Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.fengjing);
  Bitmap outBitmap =getRoundBitmapByShader(bitmap, 500,300,20, 3);
  iv.setImageBitmap(outBitmap);
public class RoundRectImageView extends ImageView{
 
 private Paint paint;
 
 public RoundRectImageView(Context context) {
  this(context,null);
 }
 
 public RoundRectImageView(Context context, AttributeSet attrs) {
  this(context, attrs,0);
 }
 
 public RoundRectImageView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  paint = new Paint();
 }
 
 /**
  * 繪制圓角矩形圖片
  * @author caizhiming
  */
 @Override
 protected void onDraw(Canvas canvas) {
  Drawable drawable = getDrawable();
  if (null != drawable) {
   Bitmap bitmap = getBitmapFromDrawable(drawable);
//   Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
   Bitmap b = getRoundBitmapByShader(bitmap,getWidth(),getHeight(), 50,0);
   final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
   final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
   paint.reset();
   canvas.drawBitmap(b, rectSrc, rectDest, paint);
 
  } else {
   super.onDraw(canvas);
  }
 }
 
 /**
  * 把資源圖片轉換成Bitmap
  * @param drawable
  * 資源圖片
  * @return 位圖
  */
 public static Bitmap getBitmapFromDrawable(Drawable drawable) {
  int width = drawable.getIntrinsicWidth();
  int height = drawable.getIntrinsicHeight();
  Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
    .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
    : Bitmap.Config.RGB_565);
  Canvas canvas = new Canvas(bitmap);
  //drawable.setBounds(-4, -4, width + 4, height + 4);
  drawable.draw(canvas);
  return bitmap;
 }
 
 public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
  if (bitmap == null) {
   return null;
  }
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  float widthScale = outWidth * 1f / width;
  float heightScale = outHeight * 1f / height;
 
  Matrix matrix = new Matrix();
  matrix.setScale(widthScale, heightScale);
  //創建輸出的bitmap
  Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
  //創建canvas并傳入desBitmap,這樣繪制的內容都會在desBitmap上
  Canvas canvas = new Canvas(desBitmap);
  Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  //創建著色器
  BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  //給著色器配置matrix
  bitmapShader.setLocalMatrix(matrix);
  paint.setShader(bitmapShader);
  //創建矩形區域并且預留出border
  RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
  //把傳入的bitmap繪制到圓角矩形區域內
  canvas.drawRoundRect(rect, radius, radius, paint);
 
  if (boarder > 0) {
   //繪制boarder
   Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
   boarderPaint.setColor(Color.GREEN);
   boarderPaint.setStyle(Paint.Style.STROKE);
   boarderPaint.setStrokeWidth(boarder);
   canvas.drawRoundRect(rect, radius, radius, boarderPaint);
  }
  return desBitmap;
 }
 
}

方法三:

對圖片進行處理,此方法還可以加邊框

/**
 * 通過BitmapShader實現圓形邊框
 * @param bitmap 
 * @param outWidth 輸出的圖片寬度
 * @param outHeight 輸出的圖片高度
 * @param radius 圓角大小
 * @param boarder 邊框寬度
 */
public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
 if (bitmap == null) {
  return null;
 }
 int height = bitmap.getHeight();
 int width = bitmap.getWidth();
 
 float widthScale = outWidth * 1f / width;
 float heightScale = outHeight * 1f / height;
 
 Matrix matrix = new Matrix();
 matrix.setScale(widthScale, heightScale);
 //創建輸出的bitmap
 Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
 //創建canvas并傳入desBitmap,這樣繪制的內容都會在desBitmap上
 Canvas canvas = new Canvas(desBitmap);
 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
 //創建著色器
 BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
 //給著色器配置matrix
 bitmapShader.setLocalMatrix(matrix);
 paint.setShader(bitmapShader);
 //創建矩形區域并且預留出border
 RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
 //把傳入的bitmap繪制到圓角矩形區域內
 canvas.drawRoundRect(rect, radius, radius, paint);
 
 if (boarder > 0) {
  //繪制boarder
  Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  boarderPaint.setColor(Color.GREEN);
  boarderPaint.setStyle(Paint.Style.STROKE);
  boarderPaint.setStrokeWidth(boarder);
  canvas.drawRoundRect(rect, radius, radius, boarderPaint);
 }
 return desBitmap;
}

實現圓形和邊框:

/**
 * 通過BitmapShader實現圓形邊框
 * @param bitmap 
 * @param outWidth 輸出的圖片寬度
 * @param outHeight 輸出的圖片高度
 * @param boarder 邊框大小
 */
public static Bitmap getCircleBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int boarder) {
int radius;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float widthScale = outWidth * 1f / width;
float heightScale = outHeight * 1f / height;
 
Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
if (outHeight > outWidth) {
 radius = outWidth / 2;
} else {
 radius = outHeight / 2;
}
//創建canvas
Canvas canvas = new Canvas(desBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Matrix matrix = new Matrix();
matrix.setScale(widthScale, heightScale);
bitmapShader.setLocalMatrix(matrix);
paint.setShader(bitmapShader);
canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, paint);
if (boarder > 0) {
 //繪制boarder
 Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 boarderPaint.setColor(Color.GREEN);
 boarderPaint.setStyle(Paint.Style.STROKE);
 boarderPaint.setStrokeWidth(boarder);
 canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, boarderPaint);
}
return desBitmap;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

北辰区| 闻喜县| 五原县| 宿松县| 肃北| 沁阳市| 耒阳市| 金湖县| 皮山县| 彭州市| 安阳市| 聂拉木县| 上蔡县| 特克斯县| 资兴市| 田林县| 太原市| 云和县| 泰顺县| 沭阳县| 浠水县| 连州市| 山丹县| 措美县| 松潘县| 通山县| 信丰县| 于都县| 乌兰察布市| 当阳市| 宜兴市| 佛学| 敦煌市| 原阳县| 曲阳县| 方山县| 嘉义县| 宜昌市| 青铜峡市| 高要市| 双辽市|