├── md ├── ic_launcher-web.png ├── libs └── android-support-v4.jar ├── res ├── drawable-xhdpi │ ├── Thumbs.db │ ├── ic_launcher.png │ ├── android_image.jpg │ ├── android_image_three.png │ └── android_image_two.jpg ├── drawable-hdpi │ └── ic_launcher.png ├── drawable-mdpi │ └── ic_launcher.png ├── drawable-xxhdpi │ └── ic_launcher.png ├── values-sw600dp │ └── dimens.xml ├── values │ ├── dimens.xml │ ├── strings.xml │ ├── styles.xml │ └── attrs.xml ├── menu │ └── main.xml ├── values-sw720dp-land │ └── dimens.xml ├── values-v11 │ └── styles.xml ├── values-v14 │ └── styles.xml └── layout │ └── activity_main.xml ├── assets └── Screenshot_2014-03-02-17-48-35.png ├── src └── com │ └── fuzzydev │ ├── MainActivity.java │ └── LabeledImageView.java ├── proguard-project.txt ├── AndroidManifest.xml ├── .gitignore └── README.md /md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/ic_launcher-web.png -------------------------------------------------------------------------------- /libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/libs/android-support-v4.jar -------------------------------------------------------------------------------- /res/drawable-xhdpi/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/res/drawable-xhdpi/Thumbs.db -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/android_image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/res/drawable-xhdpi/android_image.jpg -------------------------------------------------------------------------------- /res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /assets/Screenshot_2014-03-02-17-48-35.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/assets/Screenshot_2014-03-02-17-48-35.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/android_image_three.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/res/drawable-xhdpi/android_image_three.png -------------------------------------------------------------------------------- /res/drawable-xhdpi/android_image_two.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DejanRistic/LabeledImageView/HEAD/res/drawable-xhdpi/android_image_two.jpg -------------------------------------------------------------------------------- /res/values-sw600dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 16dp 6 | 7 | 8 | -------------------------------------------------------------------------------- /res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | LabeledImageView 5 | Settings 6 | Hello world! 7 | description 8 | 9 | 10 | -------------------------------------------------------------------------------- /res/values-sw720dp-land/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 128dp 8 | 9 | 10 | -------------------------------------------------------------------------------- /res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/com/fuzzydev/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.fuzzydev; 2 | 3 | import com.fuzzydev.labeledimageview.R; 4 | 5 | import android.os.Bundle; 6 | import android.app.Activity; 7 | import android.view.Menu; 8 | 9 | public class MainActivity extends Activity { 10 | 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_main); 15 | } 16 | 17 | @Override 18 | public boolean onCreateOptionsMenu(Menu menu) { 19 | getMenuInflater().inflate(R.menu.main, menu); 20 | return true; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /res/values/attrs.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 21 | 22 | 34 | 35 | 48 | 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | 18 | # Eclipse project files 19 | .classpath 20 | .project 21 | project.properties 22 | 23 | .idea/ 24 | GitFit-Android.iml 25 | build.gradle 26 | build.gradle~ 27 | build.xml 28 | 29 | #custom-MBW 30 | _mbw_diagrams/ 31 | _mbw_ignore/ 32 | .metadata/.lock 33 | .metadata/.mylyn/.taskListIndex/segments.gen 34 | .metadata/.mylyn/.taskListIndex/segments_1 35 | .metadata/.mylyn/repositories.xml.zip 36 | .metadata/.mylyn/tasklist.xml 37 | .metadata/.mylyn/tasks.xml.zip 38 | .metadata/.plugins/com.objectaid.uml/uml.log 39 | .metadata/.plugins/org.eclipse.cdt.core/.log 40 | .metadata/.plugins/org.eclipse.cdt.make.core/specs.c 41 | .metadata/.plugins/org.eclipse.cdt.make.core/specs.cpp 42 | .metadata/.plugins/org.eclipse.core.resources/.root/.indexes/history.version 43 | .metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index 44 | .metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.version 45 | .metadata/.plugins/org.eclipse.core.resources/.root/1.tree 46 | .metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources 47 | .metadata/.plugins/org.eclipse.core.runtime/.settings/com.android.ide.eclipse.adt.prefs 48 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.cdt.ui.prefs 49 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs 50 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.debug.ui.prefs 51 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs 52 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.m2e.discovery.prefs 53 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.context.core.prefs 54 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.monitor.ui.prefs 55 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.team.cvs.ui.prefs 56 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.team.ui.prefs 57 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.ide.prefs 58 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.prefs 59 | .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs 60 | .metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi 61 | .metadata/.plugins/org.eclipse.jdt.core/invalidArchivesCache 62 | .metadata/.plugins/org.eclipse.jdt.core/nonChainingJarsCache 63 | .metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat 64 | .metadata/.plugins/org.eclipse.jdt.ui/OpenTypeHistory.xml 65 | .metadata/.plugins/org.eclipse.jdt.ui/QualifiedTypeNameHistory.xml 66 | .metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml 67 | .metadata/.plugins/org.eclipse.m2e.logback.configuration/0.log 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple ImageView subclass that allows for overlayed text/label. 2 | 3 | Custom properites include: 4 | 5 | * Text - The text for the label. 6 | * Text Size - The size of the font in pixels. (note do not add sp or px, just put the int) 7 | * Text Color - The color of the text. 8 | * Text Style - The style of the tex. (Bold, Italic, BoldItalic) 9 | * Custom Font - The string of the custom font file in assets folder. ("myfont.ttf") 10 | * Label Postion - The position of the text. (Top Left, Top Right, Top Center, Bottom Left, Bottom Right, Bottom Center, Center) 11 | * X Offset - X offset for any of the positions above, to further customize location on screen. 12 | * Y Offset - Y offset for any of the positions above, to further customize location on screen. 13 | 14 | ![alt tag](https://raw.github.com/DejanRistic/LabeledImageView/master/assets/Screenshot_2014-03-02-17-48-35.png) 15 | 16 | 17 | Integration Instructions: 18 | 19 | Download or clone this project and copy the LabeledImageView.java into your project somewhere. 20 | 21 | Make sure you setup the custom attributes, you can copy the attrs.xml from this project or copy this into your attrs.xml under the resources tag. 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 | Then in any layout where you will use the widget make sure to add this line to your root element: 49 | 50 | xmlns:custom="http://schemas.android.com/apk/res-auto" 51 | 52 | In Eclipse, if the previous line gives you trouble you can try the following: 53 | 54 | xmlns:app="http://schemas.android.com/apk/res/com.yourpackage.LabeledImageView" 55 | 56 | Then you are ready to use the LabeledImageView in your layout, here is an example (if using the second line form above replace custom with app): 57 | 58 | 69 | 70 | 71 | You can also add the ImageView and set all the custom attributes programmatically just like any normal ImageView. 72 | -------------------------------------------------------------------------------- /src/com/fuzzydev/LabeledImageView.java: -------------------------------------------------------------------------------- 1 | package com.fuzzydev; 2 | 3 | import com.fuzzydev.labeledimageview.R; 4 | 5 | import android.content.Context; 6 | import android.content.res.TypedArray; 7 | import android.graphics.Canvas; 8 | import android.graphics.Color; 9 | import android.graphics.Paint; 10 | import android.graphics.Typeface; 11 | import android.util.AttributeSet; 12 | import android.util.Log; 13 | import android.widget.ImageView; 14 | 15 | /* 16 | * ---------------------------------------------------------------------------- 17 | * "THE BEER-WARE LICENSE" (Revision 42): 18 | * As long as you retain this notice you 19 | * can do whatever you want with this stuff. If we meet some day, and you think 20 | * this stuff is worth it, you can buy me a beer in return Dejan Ristic 21 | * ---------------------------------------------------------------------------- 22 | */ 23 | 24 | public class LabeledImageView extends ImageView { 25 | 26 | private static final String TAG = "LabeledImageView"; 27 | 28 | private static final int DEFAULT_TEXT_STYLE = Typeface.NORMAL; 29 | private static final int DEFAULT_TEXT_COLOR = Color.WHITE; 30 | private static final int DEFAULT_SCREEN_LOCATION = 0; 31 | private static final int DEFAULT_X_OFFSET = 30; 32 | private static final int DEFAULT_Y_OFFSET = 30; 33 | 34 | private static final float DEFAULT_TEXT_SIZE = 40f; 35 | 36 | private float textSize; 37 | private float xPos, yPos; 38 | private float xOffset, yOffset; 39 | 40 | private int labelLocation; 41 | private int textStyle; 42 | private int textColor; 43 | 44 | private String text; 45 | private String customFont; 46 | 47 | private Paint mTextPaint; 48 | 49 | public LabeledImageView(Context context) { 50 | super(context); 51 | initWithDefautls(); 52 | } 53 | 54 | public LabeledImageView(Context context, AttributeSet attrs) { 55 | super(context, attrs); 56 | initWithAttrs(attrs, context); 57 | } 58 | 59 | private void initWithDefautls() { 60 | textSize = DEFAULT_TEXT_SIZE; 61 | textStyle = DEFAULT_TEXT_STYLE; 62 | textColor = DEFAULT_TEXT_COLOR; 63 | labelLocation = DEFAULT_SCREEN_LOCATION; 64 | xOffset = DEFAULT_X_OFFSET; 65 | yOffset = DEFAULT_Y_OFFSET; 66 | setTextPaint(); 67 | } 68 | 69 | private void initWithAttrs(AttributeSet attrs, Context context) { 70 | initAttrs(context, attrs); 71 | setTextPaint(); 72 | if (customFont != null) { 73 | setCustomFont(context); 74 | } 75 | } 76 | 77 | private void initAttrs(Context context, AttributeSet attrs) { 78 | TypedArray a = context.getTheme().obtainStyledAttributes(attrs, 79 | R.styleable.LabeledImageView, 0, 0); 80 | 81 | try { 82 | text = a.getString(R.styleable.LabeledImageView_text); 83 | textSize = a.getFloat(R.styleable.LabeledImageView_textSizePx, 84 | DEFAULT_TEXT_SIZE); 85 | textStyle = a.getInt(R.styleable.LabeledImageView_textStyle, 86 | DEFAULT_TEXT_STYLE); 87 | textColor = a.getInt(R.styleable.LabeledImageView_textColor, 88 | DEFAULT_TEXT_COLOR); 89 | labelLocation = a.getInt( 90 | R.styleable.LabeledImageView_labelPosition, 91 | DEFAULT_SCREEN_LOCATION); 92 | customFont = a.getString(R.styleable.LabeledImageView_customFont); 93 | xOffset = a.getFloat(R.styleable.LabeledImageView_xOffset, 94 | DEFAULT_X_OFFSET); 95 | yOffset = a.getFloat(R.styleable.LabeledImageView_yOffset, 96 | DEFAULT_Y_OFFSET); 97 | ; 98 | } finally { 99 | a.recycle(); 100 | } 101 | } 102 | 103 | private void setCustomFont(Context ctx) { 104 | Typeface tf = null; 105 | try { 106 | tf = Typeface.createFromAsset(ctx.getAssets(), customFont); 107 | } catch (Exception e) { 108 | Log.e(TAG, "Could not get typeface: " + e.getMessage()); 109 | } 110 | mTextPaint.setTypeface(tf); 111 | } 112 | 113 | private void setLabelLocation() { 114 | 115 | switch (labelLocation) { 116 | case 0: // Top Left 117 | xPos = xOffset; 118 | yPos = yOffset; 119 | break; 120 | case 1: // Top Right 121 | xPos = getWidth() - mTextPaint.measureText(text) - xOffset; 122 | yPos = yOffset; 123 | break; 124 | case 2: // Bottom Left 125 | xPos = xOffset; 126 | yPos = getHeight() - yOffset; 127 | break; 128 | case 3: // Bottom Right 129 | xPos = getWidth() - mTextPaint.measureText(text) - xOffset; 130 | yPos = getHeight() - yOffset; 131 | break; 132 | case 4: // Top Center 133 | xPos = (getWidth() / 2) - (mTextPaint.measureText(text) / 2); 134 | yPos = yOffset; 135 | break; 136 | case 5: // Bottom Center 137 | xPos = (getWidth() / 2) - (mTextPaint.measureText(text) / 2); 138 | yPos = getHeight() - yOffset; 139 | break; 140 | case 6: // Center 141 | xPos = (getWidth() / 2) - (mTextPaint.measureText(text) / 2); 142 | yPos = (getHeight() / 2); 143 | default: 144 | break; 145 | } 146 | } 147 | 148 | @Override 149 | public void onSizeChanged(int w, int h, int oldw, int oldh) { 150 | super.onSizeChanged(w, h, oldw, oldh); 151 | setLabelLocation(); 152 | } 153 | 154 | private void setTextPaint() { 155 | mTextPaint = new Paint(); 156 | mTextPaint.setTextSize(textSize); 157 | mTextPaint.setColor(textColor); 158 | mTextPaint.setTypeface(Typeface.defaultFromStyle(textStyle)); 159 | } 160 | 161 | @Override 162 | protected void onDraw(Canvas canvas) { 163 | super.onDraw(canvas); 164 | if (text != null) { 165 | canvas.drawText(text, xPos, yPos, mTextPaint); 166 | } 167 | } 168 | 169 | public void setTextSize(float textSize) { 170 | this.textSize = textSize; 171 | } 172 | 173 | public void setCustomFont(String customFont) { 174 | this.customFont = customFont; 175 | } 176 | 177 | public void setLabelLocation(int labelLocation) { 178 | this.labelLocation = labelLocation; 179 | } 180 | 181 | public void setyOffset(float yOffset) { 182 | this.yOffset = yOffset; 183 | } 184 | 185 | public void setxOffset(float xOffset) { 186 | this.xOffset = xOffset; 187 | } 188 | 189 | public String getText() { 190 | return text; 191 | } 192 | 193 | public void setText(String text) { 194 | this.text = text; 195 | } 196 | 197 | public void setTextStyle(int textStyle) { 198 | this.textStyle = textStyle; 199 | } 200 | 201 | public void setTextColor(int textColor) { 202 | this.textColor = textColor; 203 | } 204 | } 205 | --------------------------------------------------------------------------------