Android - RoundedImageView不起作用

时间:2015-02-22 06:56:48

标签: java android android-layout image-processing android-activity

我有一个用户定义的类,它扩展了ImageView类。我想在ImageView上找到圆角。

当我应用该类时,它会在图形布局中显示图像的圆角。但是当我运行项目时,它没有显示图像的圆角。

我没有更改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>

1 个答案:

答案 0 :(得分:0)

使用以上链接。    它对我有用。希望。它会很有用..

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