aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/BootstrapThumbnail.java
blob: a4e1ba1bc8b38edff0ca09d5d6741acc2d461d82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.beardedhen.androidbootstrap;

import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BootstrapThumbnail extends FrameLayout
{
	private static final int DEFAULT_WIDTH = 150; //width of thumbnail when no width is given
	private static final int DEFAULT_HEIGHT = 150;//height of thumbnail when no height is given
	private static final int DEFAULT_MAX_PADDING = 8; //8dp is max padding size when padding isn't specified by user
	private static final int DEFAULT_MIN_PADDING = 4; //4dp
	private static final String DEFAULT_TYPE = "rounded";
	
	private static Map<String, ThumbnailTypes> bThumbnailTypeMap;
	private static Typeface font;
	private ViewGroup container;
	private LinearLayout placeholder;
	private TextView dimensionsLabel;
	private boolean roundedCorners = true;
	
	static{	
		bThumbnailTypeMap = new HashMap<String, ThumbnailTypes>();
		
		bThumbnailTypeMap.put("rounded", ThumbnailTypes.ROUNDED);//default is rounded if user doesn't specify to use square
		bThumbnailTypeMap.put("square", ThumbnailTypes.SQUARE);
	}
	
	public BootstrapThumbnail(Context context, AttributeSet attrs, int defStyle) 
	{
		super(context, attrs, defStyle);
		initialise(attrs);
	}
	
	public BootstrapThumbnail(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		initialise(attrs);
	}
	
	public BootstrapThumbnail(Context context) 
	{
		super(context);
		initialise(null);
	}
	
	public void setImage(int drawable)
	{
		this.placeholder.setBackgroundResource(drawable);
        invalidate();
        requestLayout();
	}
	
	//set up the bootstrap types
	private enum ThumbnailTypes
	{		
		ROUNDED(R.drawable.bthumbnail_container_rounded, R.drawable.bthumbnail_placeholder_default),
		SQUARE(R.drawable.bthumbnail_container_square, R.drawable.bthumbnail_placeholder_default);
		
		private int containerDrawable;
		private int placeholderDrawable;

		ThumbnailTypes(int containerDrawable, int placeholderDrawable)
		{
			this.containerDrawable = containerDrawable;
			this.placeholderDrawable = placeholderDrawable;
		}
	}
	
	private void initialise( AttributeSet attrs )
	{
		LayoutInflater inflator = (LayoutInflater)getContext().getSystemService(
			    Context.LAYOUT_INFLATER_SERVICE);
		
		readFont(getContext());

		TypedArray a = getContext().obtainStyledAttributes(attrs,
			    R.styleable.BootstrapThumbnail);
		
		//defaults
		ThumbnailTypes type = null;
		String thumbnailType = DEFAULT_TYPE;
		String text = "";
		int imageDrawable = 0;
		float scale = getResources().getDisplayMetrics().density; //for padding
		int width = DEFAULT_WIDTH;
		int height = DEFAULT_HEIGHT;	
		int padding = 0;
		int paddingDP = 0;

		//attribute values	
		if(a.getString(R.styleable.BootstrapThumbnail_bt_width) != null) {
			width = (int) a.getDimension(R.styleable.BootstrapThumbnail_bt_width, 0);
			Log.v("width", Integer.toString(width));
		}
		
		if(a.getString(R.styleable.BootstrapThumbnail_bt_height) != null) {
			height = (int) a.getDimension(R.styleable.BootstrapThumbnail_bt_height, 0);
		}
		
		if(a.getString(R.styleable.BootstrapThumbnail_bt_inside_padding) != null) {
			paddingDP = (int) a.getDimension(R.styleable.BootstrapThumbnail_bt_inside_padding, 0);
		}
		else{
			padding = (int) (((Math.sqrt(width * height)) / 100) * 2);
			if(padding > DEFAULT_MAX_PADDING)
				padding = DEFAULT_MAX_PADDING;
			if(padding < DEFAULT_MIN_PADDING)
				padding = DEFAULT_MIN_PADDING;
			
			paddingDP = (int) (padding * scale + 0.5f);//container padding in DP
		}
		
		if(a.getString(R.styleable.BootstrapThumbnail_bt_roundedCorners) != null){
			roundedCorners = a.getBoolean(R.styleable.BootstrapThumbnail_bt_roundedCorners, false) ;
		}
		
		if(a.getString(R.styleable.BootstrapThumbnail_bt_image) != null){
			imageDrawable = a.getResourceId(R.styleable.BootstrapThumbnail_bt_image, 0);
		}
	
		a.recycle();
		
		text = (int)(width/scale) + "x" + (int)(height/scale);
		View v = inflator.inflate(R.layout.bootstrap_thumbnail, null, false);
	
		//get layout items
		container = (ViewGroup) v.findViewById(R.id.container);
		placeholder = (LinearLayout) v.findViewById(R.id.placeholder);
		dimensionsLabel = (TextView) v.findViewById(R.id.dimensionsLabel);
		
		Log.v("size", "width:" + width + " height:" + height);
		
		
		type = bThumbnailTypeMap.get(thumbnailType);

		//get the correct background type
		if(roundedCorners == true)
		{
			type = bThumbnailTypeMap.get("rounded");
		} else {
			type = bThumbnailTypeMap.get("square");
		}
		
		//apply the background type
		container.setBackgroundResource(type.containerDrawable);
		
		//if no image is provided by user
		if(imageDrawable == 0){
			//set default grey placeholder background
			placeholder.setBackgroundResource(type.placeholderDrawable);
			
			//set the text 
	        if(text.length() > 0){        	
	        	dimensionsLabel.setText(text);
	        	dimensionsLabel.setVisibility(View.VISIBLE);        	
	        }
		}
		else{		
			//set background to user's provided image
			placeholder.setBackgroundResource(imageDrawable);
			
			//remove textview dimensions
			dimensionsLabel.setVisibility(View.GONE);
		}
			
		//placeholder padding
		int paddingP = (int) (((Math.sqrt(width * height)) / 100) * 4);

		//convert to DP	    
	    int paddingDPP = (int) (paddingP * scale + 0.5f);//placeholder padding in DP

		container.setPadding(paddingDP, paddingDP, paddingDP, paddingDP);
		placeholder.setPadding(paddingDPP, paddingDPP, paddingDPP, paddingDPP);
		
		placeholder.setLayoutParams(new LinearLayout.LayoutParams(width,height));
		
		//set the font awesome icon typeface
		dimensionsLabel.setTypeface(font);

        this.setClickable(true);
                
		addView(v);
	}
	
	//static class to read in font
	private static void readFont(Context context)
	{		
		if(font == null){	
			try {
			font = Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf");
			} catch (Exception e) {
                Log.e("BootstrapButton", "Could not get typeface because " + e.getMessage());
                font = Typeface.DEFAULT;
            }
		}

	}
}