LoginSignup
10
10

More than 5 years have passed since last update.

ライブラリを使用せずに角丸画像を表示する

Posted at

Android Support Library v4にあるRoundedBitmapDrawableを使えば、
RoundedImageViewUniversal Image Loaderなど多数あるライブラリを使用せずに角丸画像の表示ができます。

image.png

RoundedBitmapDrawableのインスタンス生成

RoundedBitmapDrawableFactoryの以下のいずれかのメソッドを使用して、インスタンスを生成して使用します。

RoundedBitmapDrawableFactory#create
public static RoundedBitmapDrawable create(Resources res, Bitmap bitmap)

public static RoundedBitmapDrawable create(Resources res, String filepath)

public static RoundedBitmapDrawable create(Resources res, InputStream is) {

引数はBitmap、ファイル名、InputStreamのいずれかなので、
ネットワーク上の画像、端末内のファイル、Assets、Rawリソースなどに使えます。

(あまり用途はないかもしれないけど)Drawableリソースの場合は以下の通り、少し面倒。
URLではなく、ファイル名なのでこの方法ではできませんでした。

BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.xxx);
Bitmap bitmap = bitmapDrawable.getBitmap();
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
drawable.setCornerRadius(100);

imageView.setImageDrawable(drawable);

結論

XMLでも書けないし、RoundedBitmapDrawableから変換後のBitmapが取れるわけでもないので、PicassoTransformationにも使えない。
個人的には使うことなさそうかな。。。

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10