|
| 1 | +package com.beardedhen.androidbootstrap; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.graphics.Bitmap; |
| 6 | +import android.graphics.BitmapFactory; |
| 7 | +import android.util.AttributeSet; |
| 8 | +import android.util.DisplayMetrics; |
| 9 | +import android.view.LayoutInflater; |
| 10 | +import android.view.View; |
| 11 | +import android.widget.FrameLayout; |
| 12 | +import android.widget.ImageView; |
| 13 | +import android.widget.LinearLayout; |
| 14 | +import android.widget.TextView; |
| 15 | + |
| 16 | +import com.beardedhen.androidbootstrap.utils.ImageUtils; |
| 17 | + |
| 18 | +public class BootstrapCircleThumbnail extends FrameLayout |
| 19 | +{ |
| 20 | + private static final int PADDING_SMALL = 4; |
| 21 | + private static final int PADDING_MEDIUM = 4; |
| 22 | + private static final int PADDING_LARGE = 6; |
| 23 | + private static final int PADDING_XLARGE = 8; |
| 24 | + |
| 25 | + private static final int SIZE_SMALL = 48; //dp total size (outer circle) |
| 26 | + private static final int SIZE_MEDIUM = 80;//dp |
| 27 | + private static final int SIZE_LARGE = 112;//dp |
| 28 | + private static final int SIZE_XLARGE = 176;//dp |
| 29 | + private static final int SIZE_DEFAULT = SIZE_MEDIUM; |
| 30 | + |
| 31 | + private static final String SMALL = "small"; |
| 32 | + private static final String MEDIUM = "medium"; |
| 33 | + private static final String LARGE = "large"; |
| 34 | + private static final String XLARGE = "xlarge"; |
| 35 | + |
| 36 | + |
| 37 | + private static final int DEFAULT_IMAGE_WIDTH = 400;//px, size when no image is given |
| 38 | + private static final int DEFAULT_IMAGE_HEIGHT = 400;//px, size when no image is given |
| 39 | + |
| 40 | + private LinearLayout container; |
| 41 | + private LinearLayout placeholder; |
| 42 | + private ImageView image; |
| 43 | + private TextView dimensionsLabel; |
| 44 | + private String size = MEDIUM; |
| 45 | + private boolean minimal = false;//minimal means display just the image, no padding |
| 46 | + private String text = ""; |
| 47 | + private int imageWidth = SIZE_DEFAULT; |
| 48 | + private int imageHeight = SIZE_DEFAULT; |
| 49 | + private int padding = 0; |
| 50 | + |
| 51 | + public BootstrapCircleThumbnail(Context context, AttributeSet attrs, int defStyle) |
| 52 | + { |
| 53 | + super(context, attrs, defStyle); |
| 54 | + initialise(attrs); |
| 55 | + } |
| 56 | + |
| 57 | + public BootstrapCircleThumbnail(Context context, AttributeSet attrs) |
| 58 | + { |
| 59 | + super(context, attrs); |
| 60 | + initialise(attrs); |
| 61 | + } |
| 62 | + |
| 63 | + public BootstrapCircleThumbnail(Context context) |
| 64 | + { |
| 65 | + super(context); |
| 66 | + initialise(null); |
| 67 | + } |
| 68 | + |
| 69 | + private void initialise( AttributeSet attrs ) |
| 70 | + { |
| 71 | + LayoutInflater inflator = (LayoutInflater)getContext().getSystemService( |
| 72 | + Context.LAYOUT_INFLATER_SERVICE); |
| 73 | + |
| 74 | + |
| 75 | + TypedArray a = getContext().obtainStyledAttributes(attrs, |
| 76 | + R.styleable.BootstrapCircleThumbnail); |
| 77 | + |
| 78 | + |
| 79 | + int imageDrawable = 0; |
| 80 | + |
| 81 | + if(a.getString(R.styleable.BootstrapCircleThumbnail_bct_image) != null) |
| 82 | + { |
| 83 | + imageDrawable = a.getResourceId(R.styleable.BootstrapCircleThumbnail_bct_image, 0); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + if(a.getString(R.styleable.BootstrapCircleThumbnail_android_text) != null) |
| 88 | + { |
| 89 | + text = a.getString(R.styleable.BootstrapCircleThumbnail_android_text); |
| 90 | + } |
| 91 | + |
| 92 | + if(a.getString(R.styleable.BootstrapCircleThumbnail_bct_size) != null) |
| 93 | + { |
| 94 | + this.size = a.getString(R.styleable.BootstrapCircleThumbnail_bct_size); |
| 95 | + } |
| 96 | + |
| 97 | + if(a.getString(R.styleable.BootstrapCircleThumbnail_bct_minimal) != null) |
| 98 | + { |
| 99 | + this.minimal = a.getBoolean(R.styleable.BootstrapCircleThumbnail_bct_minimal, false); |
| 100 | + } |
| 101 | + |
| 102 | + a.recycle(); |
| 103 | + |
| 104 | + View v = inflator.inflate(R.layout.bootstrap_thumbnail_circle, null, false); |
| 105 | + dimensionsLabel = (TextView) v.findViewById(R.id.dimensionsLabel); |
| 106 | + container = (LinearLayout) v.findViewById(R.id.container); |
| 107 | + placeholder = (LinearLayout) v.findViewById(R.id.placeholder); |
| 108 | + image = (ImageView) v.findViewById(R.id.image); |
| 109 | + float scale = getResources().getDisplayMetrics().density; |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + //small image |
| 114 | + if(this.size.equals(SMALL)) |
| 115 | + { |
| 116 | + padding = PADDING_SMALL; |
| 117 | + imageWidth = SIZE_SMALL; |
| 118 | + imageHeight = SIZE_SMALL; |
| 119 | + |
| 120 | + } |
| 121 | + else if(this.size.equals(MEDIUM)) |
| 122 | + { |
| 123 | + padding = PADDING_MEDIUM; |
| 124 | + imageWidth = SIZE_MEDIUM; |
| 125 | + imageHeight = SIZE_MEDIUM; |
| 126 | + } |
| 127 | + else if(this.size.equals(LARGE)) |
| 128 | + { |
| 129 | + padding = PADDING_LARGE; |
| 130 | + imageWidth = SIZE_LARGE; |
| 131 | + imageHeight = SIZE_LARGE; |
| 132 | + } |
| 133 | + else if(this.size.equals(XLARGE)) |
| 134 | + { |
| 135 | + padding = PADDING_XLARGE; |
| 136 | + imageWidth = SIZE_XLARGE; |
| 137 | + imageHeight = SIZE_XLARGE; |
| 138 | + } |
| 139 | + //no valid size is given, set image to default size |
| 140 | + else |
| 141 | + { |
| 142 | + padding = PADDING_MEDIUM; |
| 143 | + imageWidth = SIZE_DEFAULT; |
| 144 | + imageHeight = SIZE_DEFAULT; |
| 145 | + } |
| 146 | + |
| 147 | + //convert padding to pixels |
| 148 | + DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics(); |
| 149 | + int paddingPX = (int)((padding * scale) + 0.5); |
| 150 | + |
| 151 | + //convert image size to pixels |
| 152 | + int imageSizeWidthPX = (int)((imageWidth * scale) + 0.5); |
| 153 | + int imageSizeHeightPX = (int)((imageHeight * scale) + 0.5); |
| 154 | + |
| 155 | + //make inner image smaller to compensate for the padding so that entire circle including padding equals the size |
| 156 | + //ex. small image = 48dp, small padding = 4dp, inner image = 48 - (4 * 2) = 40 |
| 157 | + if(this.minimal == false) |
| 158 | + { |
| 159 | + imageSizeWidthPX = imageSizeWidthPX - (paddingPX * 2); |
| 160 | + imageSizeHeightPX = imageSizeHeightPX - (paddingPX * 2); |
| 161 | + |
| 162 | + this.container.setPadding(paddingPX, paddingPX, paddingPX, paddingPX); |
| 163 | + container.setBackgroundResource(R.drawable.thumbnail_circle_container); |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + container.setBackgroundResource(R.drawable.thumbnail_circle_minimal); |
| 168 | + } |
| 169 | + |
| 170 | + //if no image is given |
| 171 | + if(imageDrawable == 0) |
| 172 | + { |
| 173 | + this.image.setVisibility(View.GONE); |
| 174 | + placeholder.setLayoutParams(new LinearLayout.LayoutParams(imageSizeWidthPX, imageSizeHeightPX)); |
| 175 | + placeholder.setPadding(paddingPX, paddingPX, paddingPX, paddingPX); |
| 176 | + |
| 177 | + //set placeholder image |
| 178 | + placeholder.setBackgroundResource(R.drawable.thumbnail_circle); |
| 179 | + |
| 180 | + this.dimensionsLabel.setText(text); |
| 181 | + } |
| 182 | + else |
| 183 | + { |
| 184 | + placeholder.setPadding(0, 0, 0, 0); |
| 185 | + this.dimensionsLabel.setVisibility(View.GONE); |
| 186 | + Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), imageDrawable); |
| 187 | + |
| 188 | + Bitmap roundBitmap = ImageUtils.getCircleBitmap(bitmap, imageSizeWidthPX, imageSizeHeightPX); |
| 189 | + image.setImageBitmap(roundBitmap); |
| 190 | + } |
| 191 | + |
| 192 | + this.addView(v); |
| 193 | + } |
| 194 | + |
| 195 | + public void setImage(int drawable) |
| 196 | + { |
| 197 | + Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), drawable); |
| 198 | + |
| 199 | + float scale = getResources().getDisplayMetrics().density; |
| 200 | + |
| 201 | + //convert image size to pixels |
| 202 | + int widthPX = (int)((this.imageWidth * scale) + 0.5); |
| 203 | + int heightPX = (int)((this.imageHeight * scale) + 0.5); |
| 204 | + |
| 205 | + int paddingPX = (int)((this.padding * scale) + 0.5); |
| 206 | + |
| 207 | + if(this.minimal == false) |
| 208 | + { |
| 209 | + widthPX = widthPX - (paddingPX * 2); |
| 210 | + heightPX = heightPX - (paddingPX * 2); |
| 211 | + } |
| 212 | + |
| 213 | + Bitmap roundBitmap = ImageUtils.getCircleBitmap(bitmap, widthPX, heightPX); |
| 214 | + image.setImageBitmap(roundBitmap); |
| 215 | + |
| 216 | + invalidate(); |
| 217 | + requestLayout(); |
| 218 | + } |
| 219 | +} |
0 commit comments