Skip to content

Commit

Permalink
Fixed Javadocs & don't extract null bitmaps
Browse files Browse the repository at this point in the history
- Fixed Javadocs because of reasons.
- Null drawable check before extracting a Bitmap
  • Loading branch information
Pkmmte committed Aug 14, 2014
1 parent f7a688e commit 12d2773
Showing 1 changed file with 41 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import android.view.MotionEvent;
import android.widget.ImageView;

/**
* Custom ImageView for circular images in Android while maintaining the
* best draw performance and supporting custom borders & selectors.
*/
public class CircularImageView extends ImageView {
// Border & Selector configuration variables
private boolean hasBorder;
Expand All @@ -39,8 +43,7 @@ public CircularImageView(Context context) {
this(context, null);
}

public CircularImageView(Context context, AttributeSet attrs)
{
public CircularImageView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.circularImageViewStyle);
}

Expand All @@ -51,10 +54,9 @@ public CircularImageView(Context context, AttributeSet attrs, int defStyle) {

/**
* Initializes paint objects and sets desired attributes.
*
* @param context
* @param attrs
* @param defStyle
* @param context Context
* @param attrs Attributes
* @param defStyle Default Style
*/
private void init(Context context, AttributeSet attrs, int defStyle) {
// Initialize paint objects
Expand Down Expand Up @@ -98,8 +100,7 @@ private void init(Context context, AttributeSet attrs, int defStyle) {

/**
* Sets the CircularImageView's border width in pixels.
*
* @param borderWidth
* @param borderWidth Width in pixels for the border.
*/
public void setBorderWidth(int borderWidth) {
this.borderWidth = borderWidth;
Expand All @@ -109,8 +110,7 @@ public void setBorderWidth(int borderWidth) {

/**
* Sets the CircularImageView's basic border color.
*
* @param borderColor
* @param borderColor The new color (including alpha) to set the border.
*/
public void setBorderColor(int borderColor) {
if (paintBorder != null)
Expand All @@ -121,8 +121,7 @@ public void setBorderColor(int borderColor) {
/**
* Sets the color of the selector to be draw over the
* CircularImageView. Be sure to provide some opacity.
*
* @param selectorColor
* @param selectorColor The color (including alpha) to set for the selector overlay.
*/
public void setSelectorColor(int selectorColor) {
this.selectorFilter = new PorterDuffColorFilter(selectorColor, PorterDuff.Mode.SRC_ATOP);
Expand All @@ -132,8 +131,7 @@ public void setSelectorColor(int selectorColor) {
/**
* Sets the stroke width to be drawn around the CircularImageView
* during click events when the selector is enabled.
*
* @param selectorStrokeWidth
* @param selectorStrokeWidth Width in pixels for the selector stroke.
*/
public void setSelectorStrokeWidth(int selectorStrokeWidth) {
this.selectorStrokeWidth = selectorStrokeWidth;
Expand All @@ -144,8 +142,7 @@ public void setSelectorStrokeWidth(int selectorStrokeWidth) {
/**
* Sets the stroke color to be drawn around the CircularImageView
* during click events when the selector is enabled.
*
* @param selectorStrokeColor
* @param selectorStrokeColor The color (including alpha) to set for the selector stroke.
*/
public void setSelectorStrokeColor(int selectorStrokeColor) {
if (paintSelectorBorder != null)
Expand All @@ -154,7 +151,7 @@ public void setSelectorStrokeColor(int selectorStrokeColor) {
}

/**
* Adds a dark shadow to this CircularImageView.
* Enables a dark shadow for this CircularImageView.
*/
public void addShadow() {
setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);
Expand Down Expand Up @@ -242,13 +239,25 @@ public boolean dispatchTouchEvent(MotionEvent event) {

public void invalidate(Rect dirty) {
super.invalidate(dirty);

// Don't do anything without a valid drawable
if(getDrawable() == null)
return;

// Extract a Bitmap out of the drawable & set it as the main shader
image = drawableToBitmap(getDrawable());
if(shader != null || canvasSize > 0)
refreshBitmapShader();
}

public void invalidate(int l, int t, int r, int b) {
super.invalidate(l, t, r, b);

// Don't do anything without a valid drawable
if(getDrawable() == null)
return;

// Extract a Bitmap out of the drawable & set it as the main shader
image = drawableToBitmap(getDrawable());
if(shader != null || canvasSize > 0)
refreshBitmapShader();
Expand All @@ -257,6 +266,12 @@ public void invalidate(int l, int t, int r, int b) {
@Override
public void invalidate() {
super.invalidate();

// Don't do anything without a valid drawable
if(getDrawable() == null)
return;

// Extract a Bitmap out of the drawable & set it as the main shader
image = drawableToBitmap(getDrawable());
if(shader != null || canvasSize > 0)
refreshBitmapShader();
Expand Down Expand Up @@ -291,7 +306,7 @@ else if (specMode == MeasureSpec.AT_MOST) {
}

private int measureHeight(int measureSpecHeight) {
int result = 0;
int result;
int specMode = MeasureSpec.getMode(measureSpecHeight);
int specSize = MeasureSpec.getSize(measureSpecHeight);

Expand All @@ -310,26 +325,23 @@ private int measureHeight(int measureSpecHeight) {
}

/**
* Convert a drawable object into a Bitmap
*
* @param drawable
* @return
* Convert a drawable object into a Bitmap.
* @param drawable Drawable to extract a Bitmap from.
* @return A Bitmap created from the drawable parameter.
*/
public Bitmap drawableToBitmap(Drawable drawable) {
if (drawable == null) { // Don't do anything without a proper drawable
if (drawable == null) // Don't do anything without a proper drawable
return null;
}
else if (drawable instanceof BitmapDrawable) { // Use the getBitmap() method instead if BitmapDrawable
else if (drawable instanceof BitmapDrawable) // Use the getBitmap() method instead if BitmapDrawable
return ((BitmapDrawable) drawable).getBitmap();
}

// Create Bitmap object out of the drawable
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

// Return the created Bitmap
return bitmap;
}

Expand All @@ -342,7 +354,7 @@ public void refreshBitmapShader() {
}

/**
* Returns whether or not this view is currently
* @return Whether or not this view is currently
* in its selected state.
*/
public boolean isSelected() {
Expand Down

0 comments on commit 12d2773

Please sign in to comment.