web-dev-qa-db-ja.com

ImageViewの角丸

画像の角を丸くしたかった。このxmlコードを実装し、これを画像ビューで使用します。しかし、画像は形状と重なります。非同期タスクを介して画像をダウンロードしています。

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:Android="http://schemas.Android.com/apk/res/Android"
 Android:shape="rectangle" >
 <corners Android:radius="20dip" />
</shape>


<ImageView
    Android:id="@+id/trVouchersImage"
    Android:layout_width="55dp"
    Android:layout_height="55dp"
    Android:layout_marginLeft="8dp"
    Android:layout_centerVertical="true"
    Android:layout_centerHorizontal="true"
    Android:layout_alignParentLeft="true"
    Android:background="@drawable/ash_arrow"
 />
29
FIXI

ユニバーサルイメージローダーライブラリを使用して、イメージの隅をダウンロードして丸めてみました。

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(thisContext)
            // You can pass your own memory cache implementation
           .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
           .build();

DisplayImageOptions options = new DisplayImageOptions.Builder()
            .displayer(new RoundedBitmapDisplayer(10)) //rounded corner bitmap
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .build();

ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
imageLoader.displayImage(image_url,image_view, options );
23
FIXI

シンプルなアプローチ:

Xmlファイルを作成rounded_fg.xmlres/drawable /アプリのフォルダー。 rounded_fg.xmlの内容は次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:innerRadiusRatio="2"
    Android:shape="ring"
    Android:thicknessRatio="1"
    Android:useLevel="false">
    <gradient
        Android:type="radial"
        Android:gradientRadius="8dp"
        Android:endColor="@color/white"
       />
</shape>

endColorとImageViewコンテナレイアウトの背景を一致させることができますgradientRadiusは要件ごとの任意の値(<= 36dp)。

次に、このドロアブルをforegroundとして次のように使用します。

 <ImageView
     Android:layout_width="55dp"
     Android:layout_height="55dp" 
     Android:foreground="@drawable/rounded_fg" />

square imagesおよび/またはimageviewに最適です。

Square Image/ImageView:

Square Image/ImageView

長方形の画像/ ImageView:

Rectangular Image/ImageView

ボタン上に適用される前景:

Foreground applied over a button

22
Nihal

このようにXMLで行うことができます

<stroke Android:width="3dp"
        Android:color="#ff000000"/>

<padding Android:left="1dp"
         Android:top="1dp"
         Android:right="1dp"
         Android:bottom="1dp"/> 

<corners Android:radius="30px"/> 

実用的には、丸みを帯びたビットマップを作成し、ImageViewで設定できます。

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
    bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12;

Paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
Paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, Paint);

Paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, Paint);

return output;
}

niversal lazy loaderの場合、このワットも使用できます。

DisplayImageOptions options = new DisplayImageOptions.Builder()
        .displayer(new RoundedBitmapDisplayer(25)) // default
        .build();
15
Sanket Kachhela

新しい回答このためにグライドライブラリを使用します。このライブラリはGoogleでも推奨されています。 Glideライブラリで画像を丸める方法 を参照してください

OLD ANSWERその画像をcardViewに追加し、cardViewの標高を0dpに設定するだけで、トリックが実行されます(私の場合はviewPagerの画像-viewPagerImageViewに置き換えるだけです:

<Android.support.v7.widget.CardView
        Android:layout_width="match_parent"
        Android:layout_height="250dp"
        app:cardElevation="0dp">

        <Android.support.v4.view.ViewPager
            xmlns:Android="http://schemas.Android.com/apk/res/Android"
            Android:id="@+id/viewPager"
            Android:layout_width="match_parent"
            Android:layout_height="match_parent" />

    </Android.support.v7.widget.CardView>
11

このライブラリRoundedCornersTransformationを使用し、循環ImageViewを作成する必要があります。

import Android.graphics.Bitmap;
import Android.graphics.BitmapShader;
import Android.graphics.Canvas;
import Android.graphics.Paint;
import Android.graphics.RectF;
import Android.graphics.Shader;
import com.squareup.picasso.Transformation;

public class RoundedCornersTransformation implements Transformation {

  public enum CornerType {
    ALL,
    TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
    TOP, BOTTOM, LEFT, RIGHT,
    OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
    DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
  }

  private int mRadius;
  private int mDiameter;
  private int mMargin;
  private CornerType mCornerType;

  public RoundedCornersTransformation(int radius, int margin) {
    this(radius, margin, CornerType.ALL);
  }

  public RoundedCornersTransformation(int radius, int margin, CornerType cornerType) {
    mRadius = radius;
    mDiameter = radius * 2;
    mMargin = margin;
    mCornerType = cornerType;
  }

  @Override public Bitmap transform(Bitmap source) {

    int width = source.getWidth();
    int height = source.getHeight();

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    Paint.setAntiAlias(true);
    Paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    drawRoundRect(canvas, Paint, width, height);
    source.recycle();

    return bitmap;
  }

  private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {
    float right = width - mMargin;
    float bottom = height - mMargin;

    switch (mCornerType) {
      case ALL:
        canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, Paint);
        break;
      case TOP_LEFT:
        drawTopLeftRoundRect(canvas, Paint, right, bottom);
        break;
      case TOP_RIGHT:
        drawTopRightRoundRect(canvas, Paint, right, bottom);
        break;
      case BOTTOM_LEFT:
        drawBottomLeftRoundRect(canvas, Paint, right, bottom);
        break;
      case BOTTOM_RIGHT:
        drawBottomRightRoundRect(canvas, Paint, right, bottom);
        break;
      case TOP:
        drawTopRoundRect(canvas, Paint, right, bottom);
        break;
      case BOTTOM:
        drawBottomRoundRect(canvas, Paint, right, bottom);
        break;
      case LEFT:
        drawLeftRoundRect(canvas, Paint, right, bottom);
        break;
      case RIGHT:
        drawRightRoundRect(canvas, Paint, right, bottom);
        break;
      case OTHER_TOP_LEFT:
        drawOtherTopLeftRoundRect(canvas, Paint, right, bottom);
        break;
      case OTHER_TOP_RIGHT:
        drawOtherTopRightRoundRect(canvas, Paint, right, bottom);
        break;
      case OTHER_BOTTOM_LEFT:
        drawOtherBottomLeftRoundRect(canvas, Paint, right, bottom);
        break;
      case OTHER_BOTTOM_RIGHT:
        drawOtherBottomRightRoundRect(canvas, Paint, right, bottom);
        break;
      case DIAGONAL_FROM_TOP_LEFT:
        drawDiagonalFromTopLeftRoundRect(canvas, Paint, right, bottom);
        break;
      case DIAGONAL_FROM_TOP_RIGHT:
        drawDiagonalFromTopRightRoundRect(canvas, Paint, right, bottom);
        break;
      default:
        canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, Paint);
        break;
    }
  }

  private void drawTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter),
        mRadius, mRadius, Paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, mMargin + mRadius, bottom), Paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), Paint);
  }

  private void drawTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius,
        mRadius, Paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), Paint);
    canvas.drawRect(new RectF(right - mRadius, mMargin + mRadius, right, bottom), Paint);
  }

  private void drawBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom),
        mRadius, mRadius, Paint);
    canvas.drawRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom - mRadius), Paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), Paint);
  }

  private void drawBottomRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius,
        mRadius, Paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), Paint);
    canvas.drawRect(new RectF(right - mRadius, mMargin, right, bottom - mRadius), Paint);
  }

  private void drawTopRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
        Paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right, bottom), Paint);
  }

  private void drawBottomRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
        Paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right, bottom - mRadius), Paint);
  }

  private void drawLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
        Paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), Paint);
  }

  private void drawRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
        Paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), Paint);
  }

  private void drawOtherTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
        Paint);
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
        Paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), Paint);
  }

  private void drawOtherTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
        Paint);
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
        Paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom - mRadius), Paint);
  }

  private void drawOtherBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
        Paint);
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
        Paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mRadius, bottom), Paint);
  }

  private void drawOtherBottomRightRoundRect(Canvas canvas, Paint paint, float right,
      float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
        Paint);
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
        Paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), Paint);
  }

  private void drawDiagonalFromTopLeftRoundRect(Canvas canvas, Paint paint, float right,
      float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter),
        mRadius, mRadius, Paint);
    canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius,
        mRadius, Paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mDiameter, bottom), Paint);
    canvas.drawRect(new RectF(mMargin + mDiameter, mMargin, right, bottom - mRadius), Paint);
  }

  private void drawDiagonalFromTopRightRoundRect(Canvas canvas, Paint paint, float right,
      float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius,
        mRadius, Paint);
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom),
        mRadius, mRadius, Paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), Paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), Paint);
  }

  @Override public String key() {
    return "RoundedTransformation(radius=" + mRadius + ", margin=" + mMargin + ", diameter="
        + mDiameter + ", cornerType=" + mCornerType.name() + ")";
  }
}
9
Kaushik
  • あなたはあなたの解決策を見つけたかもしれませんが、最近、Image VIewに設定したい多くの形状を作成できる新しいライブラリを見つけました。

    • ライブラリを here からダウンロードします
    • Xmlを次のように定義します:-
    • <com.makeramen.roundedimageview.RoundedImageView......... app:riv_corner_radius="Yourradiusdip"/>
    • そして、コーディングをお楽しみください!私が見つけたこのライブラリは最高です!ありがとう[後でこの回答を投稿しましたが、最近使用したので本当に役に立ちました]
4
Hardy

誰かがまだそれを行う必要があるかどうか疑問に思うだけです。

それらの場合:目的にRoundedBitmapDrawableを使用できます。

サンプルコード:

ImageView profilePic = (ImageView) findViewById(R.idimageView);

RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(this.getResources(),bitmap);
roundedBitmapDrawable.setCircular(true);
profilePic.setImageDrawable(roundedBitmapDrawable);

ここで、bitmapは、imageViewにロードする画像です。

3
Swr7der

ここに私がここから見つけたものがあります: github

少し即興をしました。非常にシンプルできれい。外部ファイルまたはメソッドはありません:

public class RoundedImageView extends ImageView {


private float mCornerRadius = 10.0f;

public RoundedImageView(Context context) {
    super(context);
}

public RoundedImageView(Context context, AttributeSet attributes) {
    super(context, attributes);
}



@Override
protected void onDraw(Canvas canvas) {
    // Round some corners betch!
    Drawable myDrawable = getDrawable();

    if (myDrawable!=null && myDrawable instanceof BitmapDrawable && mCornerRadius > 0) {
        Paint paint = ((BitmapDrawable) myDrawable).getPaint();
        final int color = 0xff000000;
        Rect bitmapBounds = myDrawable.getBounds();
        final RectF rectF = new RectF(bitmapBounds);
        // Create an off-screen bitmap to the PorterDuff alpha blending to work right
        int saveCount = canvas.saveLayer(rectF, null,
                Canvas.MATRIX_SAVE_FLAG |
                Canvas.CLIP_SAVE_FLAG |
                Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
                Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
                Canvas.CLIP_TO_LAYER_SAVE_FLAG);
        // Resize the rounded rect we'll clip by this view's current bounds
        // (super.onDraw() will do something similar with the drawable to draw)
        getImageMatrix().mapRect(rectF);

        Paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        Paint.setColor(color);
        canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, Paint);

        Xfermode oldMode = Paint.getXfermode();
        // This is the Paint already associated with the BitmapDrawable that super draws
        Paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        super.onDraw(canvas);
        Paint.setXfermode(oldMode);
        canvas.restoreToCount(saveCount);
    } else {
        super.onDraw(canvas);
    }
}


}
2
Michael Kern

このutilメソッドを使用して可能な限りシンプルに

/*
 * param@ imageView is your image you want to bordered it
 */
public static Bitmap generateBorders(ImageView imageView){
    Bitmap mbitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    Bitmap imageRounded = Bitmap.createBitmap(mbitmap.getWidth(), mbitmap.getHeight(), mbitmap.getConfig());
    Canvas canvas = new Canvas(imageRounded);
    Paint mpaint = new Paint();
    mpaint.setAntiAlias(true);
    mpaint.setShader(new BitmapShader(mbitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    canvas.drawRoundRect((new RectF(0, 0, mbitmap.getWidth(), mbitmap.getHeight())), 100, 100, mpaint);// Round Image Corner 100 100 100 100
    return imageRounded;
}

その後、戻り値を使用して画像ビューのビットマップを設定します

1
Mostafa Anter

Nihalの答え( https://stackoverflow.com/a/42234152/2832027 )に基づいて、API 24以上で角が丸い長方形を提供する純粋なXMLバージョンがあります。 API 24の下では、丸い角は表示されません。

使用法:

<ImageView
    Android:id="@+id/thumbnail"
    Android:layout_width="150dp"
    Android:layout_height="200dp"
    Android:foreground="@drawable/rounded_corner_mask"/>

rounded_corner_mask.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:Android="http://schemas.Android.com/apk/res/Android">

    <item
        Android:gravity="bottom|right">
        <vector xmlns:Android="http://schemas.Android.com/apk/res/Android"
                Android:width="@dimen/rounding_radius"
                Android:height="@dimen/rounding_radius"
                Android:viewportWidth="10.0"
                Android:viewportHeight="10.0">
            <path
                Android:pathData="M0,10 A10,10 0 0,0 10,0 L10,10 Z"
                Android:fillColor="@color/white"/>
        </vector>
    </item>

    <item
        Android:gravity="bottom|left">
        <vector xmlns:Android="http://schemas.Android.com/apk/res/Android"
                Android:width="@dimen/rounding_radius"
                Android:height="@dimen/rounding_radius"
                Android:viewportWidth="10.0"
                Android:viewportHeight="10.0">
            <path
                Android:pathData="M0,0 A10,10 0 0,0 10,10 L0,10 Z"
                Android:fillColor="@color/white"/>
        </vector>
    </item>

    <item
        Android:gravity="top|left">
        <vector xmlns:Android="http://schemas.Android.com/apk/res/Android"
                Android:width="@dimen/rounding_radius"
                Android:height="@dimen/rounding_radius"
                Android:viewportWidth="10.0"
                Android:viewportHeight="10.0">
            <path
                Android:pathData="M10,0 A10,10 0 0,0 0,10 L0,0 Z"
                Android:fillColor="@color/white"/>
        </vector>
    </item>

    <item
        Android:gravity="top|right">
        <vector xmlns:Android="http://schemas.Android.com/apk/res/Android"
                Android:width="@dimen/rounding_radius"
                Android:height="@dimen/rounding_radius"
                Android:viewportWidth="10.0"
                Android:viewportHeight="10.0">
            <path
                Android:pathData="M10,10 A10,10 0 0,0 0,0 L10,0 Z"
                Android:fillColor="@color/white"/>
        </vector>
    </item>

</layer-list>
0
TTransmit

Drawable XMLファイルを作成する

    <shape xmlns:Android="http://schemas.Android.com/apk/res/Android"
        Android:shape="rectangle">
        <corners Android:radius="8dp" />
       <solid Android:color="@Android:color/white" />

<stroke
    Android:width="1dp"
    Android:color="@color/md_white_1000" />

レイアウトで、imageViewの背景としてドロアブルを追加します。

    <ImageView
Android:layout_width="200dp"
Android:layout_height="200dp"
Android:src="@drawable/photo"
Android:background="@drawable/roundcorners"/>

次に、この行をJavaコードに追加します。

    ImageView.setClipToOutline(true);

注:これはレベル21+でのみ機能します

0
Mutale Chanda

丸みのある画像を取得する必要がある場合は、このようにしてください。

MainActivity.Javaクラス

public class MainActivity extends Activity {
    private static final int REQUEST_CODE = 1;
    private Bitmap bitmap;
    private ImageView imageView;
    Bitmap roundedBitmapImage;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.result);

    }

    public void pickImage(View View) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)


            try {
                // We need to recycle unused bitmaps
                if (bitmap != null) {
                    bitmap.recycle();
                }
                InputStream stream = getContentResolver().openInputStream(
                        data.getData());
                bitmap = BitmapFactory.decodeStream(stream);
                stream.close();

               /* Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                Bitmap bitmap2=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

                Bitmap resultingImage=Bitmap.createBitmap(320, 480, bitmap1.getConfig());

                Canvas canvas = new Canvas(resultingImage);

                Paint paint = new Paint();
                Paint.setAntiAlias(true);
                Path path=new Path();
                path.lineTo(150, 0);
                path.lineTo(230, 120);
                path.lineTo(70, 120);
                path.lineTo(150, 0);

                canvas.drawPath(path, Paint);

                Paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
                canvas.drawBitmap(bitmap2, 0, 0, Paint);*/

               //-------> compositeImageView.setImageBitmap(resultingImage);

                // Use this when to provide any shape to image i.e Fit image to any shape.
                // under mentioned taking reference from Rounder class. Finally changing image in round shape.
                // Here we are passing reference  bitmap.
                roundedBitmapImage = new  Rounder().getRoundedShape(bitmap);

                imageView.setImageBitmap(roundedBitmapImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

そして、あなたの丸いクラス..

/ **このクラスは画像を丸い形にトリミングします* /

public class Rounder {
    public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
        int targetWidth = 125;
        int targetHeight = 125;
        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
                Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(targetBitmap);

        Path path = new Path();

        path.addCircle(((float) targetWidth - 1) / 2,
                ((float) targetHeight - 1) / 2,
                (Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
                Path.Direction.CCW);

        canvas.clipPath(path);

        Bitmap sourceBitmap = scaleBitmapImage;

        canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
                sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
                targetHeight), null);

        return targetBitmap;
    }
}
0
AndroidHacker

あなたのMainActivity.Javaは次のようなものです:

LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
ImageView iv = (ImageView) findViewById(R.id.iv);

最初にリソースからBitmapまたはDrawableとして画像を取得する必要があります。

Bitmapとして取得する場合:

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ash_arrow);
bm = new Newreza().setEffect(bm, 0.2f, ((ColorDrawable) ll.getBackground).getColor);
iv.setImageBitmap(bm);

または、Drawableとして取得する場合:

Drawable d = getResources().getDrawable(R.drawable.ash_arrow);
d = new Newreza().setEffect(d, 0.2f, ((ColorDrawable) ll.getBackground).getColor);
iv.setImageDrawable(d);

次に、Newreza.Javaの近くにMainActivity.Javaとして新しいファイルを作成し、Newreza.Javaの下のコードをコピーします。

package your.package.name;
import Android.content.res.Resources;
import Android.graphics.Bitmap;
import Android.graphics.drawable.BitmapDrawable;
import Android.graphics.drawable.Drawable;
//Telegram:@newreza
//mail:[email protected]
public class Newreza{
    int a,x,y;
    float bmr;
    public Bitmap setEffect(Bitmap bm,float radius,int color){
        bm=bm.copy(Bitmap.Config.ARGB_8888,true);
        bmr=radius*bm.getWidth();
        for(y=0;y<bmr;y++){
            a=(int)(bmr-Math.sqrt(y*(2*bmr-y)));
            for(x=0;x<a;x++){
                bm.setPixel(x,y,color);
            }
        }
        for(y=0;y<bmr;y++){
            a=(int)(bm.getWidth()-bmr+Math.sqrt(y*(2*bmr-y)));
            for(x=a;x<bm.getWidth();x++){
                bm.setPixel(x,y,color);
            }
        }
        for(y=(int)(bm.getHeight()-bmr);y<bm.getHeight();y++){
            a=(int)(bm.getWidth()-bmr+Math.sqrt(Math.pow(bmr,2)-Math.pow(bmr+y-bm.getHeight(),2)));
            for(x=a;x<bm.getWidth();x++){
                bm.setPixel(x,y,color);
            }
        }
        for(y=(int)(bm.getHeight()-bmr);y<bm.getHeight();y++){
            a=(int)(bmr-Math.sqrt(Math.pow(bmr,2)-Math.pow(bmr+y-bm.getHeight(),2)));
            for(x=0;x<a;x++){
                bm.setPixel(x,y,color);
            }
        }
        return bm;
    }
    public Drawable setEffect(Drawable d,float radius,int color){
        return new BitmapDrawable(Resources.getSystem(),setEffect(((BitmapDrawable)d).getBitmap(),radius,color));
    }
}

パッケージ名をコードの最初の行に置き換えてください。

詳細に記述されているため、%100が機能します。

0
newreza

簡単な方法が見つかりました。ユーザー定義の半径を持つ丸い画像ビュー:

http://shortcutsandroid.blogspot.in/2015/02/round-image-view-in-Android.html

imageView.setRadius();を追加するだけです

//丸いimageViewの半径を設定します。

0
user4583119

XmlでこのカスタムImageViewを使用します

public class RoundedCornerImageView extends ImageView {

    public RoundedCornerImageView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
        int w = getWidth(), h = getHeight();
        Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w);
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    }

    public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) {
        Bitmap finalBitmap;
        if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
            finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
                    false);
        else
            finalBitmap = bitmap;
        Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
                finalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
                finalBitmap.getHeight());

        final RectF rectf = new RectF(0, 0, finalBitmap.getWidth(),
                finalBitmap.getHeight());

        Paint.setAntiAlias(true);
        Paint.setFilterBitmap(true);
        Paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        Paint.setColor(Color.parseColor("#BAB399"));
        //Set Required Radius Here
        int yourRadius = 7;
        canvas.drawRoundRect(rectf, yourRadius, yourRadius, Paint);
        Paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(finalBitmap, rect, rect, Paint);

        return output;
    }

}
0
Shreesha P R

私はExtend ImageViewを使用しています:

public class RadiusCornerImageView extends Android.support.v7.widget.AppCompatImageView {
    private int cornerRadiusDP = 0; // dp
    private int corner_radius_position;

    public RadiusCornerImageView(Context context) {
        super(context);
    }

    public RadiusCornerImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RadiusCornerImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RadiusCornerImageView, 0, 0);
        try {
            cornerRadiusDP = typeArray.getInt(R.styleable.RadiusCornerImageView_corner_radius_dp, 0);
            corner_radius_position = typeArray.getInteger(R.styleable.RadiusCornerImageView_corner_radius_position, 0);
        } finally {
            typeArray.recycle();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float radiusPx = AndroidUtil.dpToPx(getContext(), cornerRadiusDP);
        Path clipPath = new Path();
        RectF rect = null;
        if (corner_radius_position == 0) { // all
            // round corners on all 4 angles
            rect = new RectF(0, 0, this.getWidth(), this.getHeight());
        } else if (corner_radius_position == 1) {
            // round corners only on top left and top right
            rect = new RectF(0, 0, this.getWidth(), this.getHeight() + radiusPx);
         } else {
            throw new IllegalArgumentException("Unknown corner_radius_position = " + corner_radius_position);
        }
        clipPath.addRoundRect(rect, radiusPx, radiusPx, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }
}
0
Alex