Reputation: 2288
i have created CircleImageView to display the image at Circle Shape
CircularImageView
public class CircularImageView extends ImageView {
private final static String TAG = "CircularImageView";
private final static float DEFAULT_RADIUS = 90;
private float mRadius;
public CircularImageView(Context context) {
super(context);
}
public CircularImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircularImageView,defStyle,0);
mRadius = a.getFloat(R.styleable.CircularImageView_Radius,DEFAULT_RADIUS);
//Log.i(TAG, String.format("%f", mRadius));
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
Path path = new Path();
path.addCircle(getWidth() / 2 ,getHeight() / 2,mRadius,Path.Direction.CW);
canvas.clipPath(path);
super.onDraw(canvas);
}
}
now i am using it inside XML File
XML File
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.itzik.samara.apps.pokerbuddies.main.back.views.CircularImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/empty_user_image"
app:Radius="50"
android:id="@+id/poker_row_user_image" />
attrs.xml
<declare-styleable name="CircularImageView">
<attr name="Radius" format="float" />
</declare-styleable>
first Android Studio says Constructor not used?? second it doesn't display the image. if i change in the function onDraw(Canvas canvas) mRadius to a number the image works meaning onDraw() is called
anyone ever encountered with that kind of problem why the Constructor not called?
Upvotes: 1
Views: 1237
Reputation: 9655
Modify this constructor:
public CircularImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
To:
public CircularImageView(Context context, AttributeSet attrs) {
//super(context, attrs);
this(context, attrs, 0);
}
Upvotes: 5