Created
April 11, 2018 14:26
-
-
Save hasankucuk/23ebcc7c1a3b246543a059bbb9483386 to your computer and use it in GitHub Desktop.
Android - TextProgressBar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end | |
#parse("File Header.java") | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.graphics.Typeface; | |
import android.util.AttributeSet; | |
import android.widget.ProgressBar; | |
import android.widget.TextView; | |
public class TextProgressBar extends ProgressBar { | |
private String text; | |
private Paint textPaint; | |
public TextProgressBar(Context context) { | |
super(context); | |
text = "HP"; | |
textPaint = new Paint(); | |
textPaint.setColor(Color.WHITE); | |
setTextSize(); | |
} | |
public TextProgressBar(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
text = "HP"; | |
textPaint = new Paint(); | |
textPaint.setColor(Color.WHITE); | |
setTextSize(); | |
} | |
public TextProgressBar(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
text = "HP"; | |
textPaint = new Paint(); | |
textPaint.setColor(Color.WHITE); | |
setTextSize(); | |
} | |
@Override | |
protected synchronized void onDraw(Canvas canvas) { | |
// First draw the regular progress bar, then custom draw our text | |
super.onDraw(canvas); | |
Rect bounds = new Rect(); | |
textPaint.getTextBounds(text, 0, text.length(), bounds); | |
int x = getWidth() / 2 - bounds.centerX(); | |
int y = getHeight() / 2 - bounds.centerY(); | |
canvas.drawText(text, x, y, textPaint); | |
} | |
public synchronized void setText(String text) { | |
this.text = text; | |
drawableStateChanged(); | |
} | |
public void setTextColor(int color) { | |
textPaint.setColor(color); | |
drawableStateChanged(); | |
} | |
public void setTextSize() { | |
Typeface type = Typeface.createFromAsset(getContext().getAssets(),"font/Raleway-ExtraBold.ttf"); | |
textPaint.setTypeface(type); | |
TextView defaultTextView = new TextView(getContext()); | |
float sourceTextSize =defaultTextView.getTextSize(); | |
textPaint.setTextSize(sourceTextSize); | |
// textPaint.setTextSize(size); | |
drawableStateChanged(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment