Android - RoundedImageView 不工作

Android - RoundedImageView is not working

我有一个用户定义的 class,它扩展了 ImageView class。我想要 ImageView.

上的圆角

当我应用 class 时,它在图形布局中显示圆角图像。但是当我 运行 项目时,它不显示图像的圆角。

我没有更改MainActivity.java

RoundedImageView.java

package com.round.image;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundedImageView extends ImageView {

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

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

  public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
  }

  @Override
  protected void onDraw(Canvas canvas) {
       float radius = 90.0f; // angle of round corners
       Path clipPath = new Path();
       RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
       clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
       canvas.clipPath(clipPath);

       super.onDraw(canvas);
   }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.round.image.MainActivity" >

<com.round.image.RoundedImageView
    android:id="@+id/imageView1"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="110dp"
    android:layout_marginRight="40dp"
    android:src="@drawable/bg_image" />

</RelativeLayout>

使用上面的link。 它适用于 me.Hope。它将很有用..

http://www.androidhub4you.com/2014/10/android-custom-shape-imageview-rounded.html