Skip to content

Instantly share code, notes, and snippets.

@imandaliya
Created July 14, 2020 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imandaliya/4ed4807eacf958cb0cdd135895bd6fd2 to your computer and use it in GitHub Desktop.
Save imandaliya/4ed4807eacf958cb0cdd135895bd6fd2 to your computer and use it in GitHub Desktop.
package com.rahul.android.material.support.views;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
import com.rahul.android.material.support.R;
import com.rahul.android.material.support.utils.Utils;
/*
* TODO Attribute set
*
<declare-styleable name="RoundedSquareImageView">
<attr name="radius_RoundedSquareImageView" format="dimension" />
<attr name="type_RoundedSquareImageView" format="enum">
<enum name="width_square" value="1" />
<enum name="height_square" value="2" />
</attr>
<attr name="color_RoundedSquareImageView" format="color" />
</declare-styleable>
* */
public class RoundedSquareImageView extends AppCompatImageView {
private static final int WIDTH_SQUARE = 1;
private static final int HEIGHT_SQUARE = 2;
private static final int DEFAULT_RADIUS = 8;
private int type;
private GradientDrawable gradientDrawable;
public RoundedSquareImageView(@NonNull Context context) {
this(context, null);
}
public RoundedSquareImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public RoundedSquareImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
float radius;
int backgroundColor;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedSquareImageView, 0, 0);
try {
type = a.getInt(R.styleable.RoundedSquareImageView_type_RoundedSquareImageView, WIDTH_SQUARE);
radius = a.getFloat(R.styleable.RoundedSquareImageView_radius_RoundedSquareImageView, Utils.dpToPx(context, DEFAULT_RADIUS));
backgroundColor = a.getColor(R.styleable.RoundedSquareImageView_color_RoundedSquareImageView, ContextCompat.getColor(context, R.color.uil_light));
} catch (Exception e) {
radius = Utils.dpToPx(context, 8);
backgroundColor = ContextCompat.getColor(context, R.color.uil_light);
} finally {
a.recycle();
}
gradientDrawable = new GradientDrawable();
setShapeColor(backgroundColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setShapeRadius(radius);
setClipToOutline(true);
}
}
private void setShapeRadius(float radius) {
gradientDrawable.setCornerRadius(radius);
setBackground(gradientDrawable);
}
private void setShapeColor(int backgroundColor) {
gradientDrawable.setColor(backgroundColor);
setBackground(gradientDrawable);
}
@SuppressWarnings("SuspiciousNameCombination")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (type == WIDTH_SQUARE)
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
else if (type == HEIGHT_SQUARE)
super.onMeasure(heightMeasureSpec, heightMeasureSpec);
else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment