Created
February 9, 2021 11:24
-
-
Save dmide/e1f38c3460116fb9f88318432b6b9726 to your computer and use it in GitHub Desktop.
Simple CustomTextView
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
class CustomTextView extends View { | |
private static final String SUPERSTATE = "superState", TEXT = "text", | |
TEXTSIZE = "textsize"; | |
private String mText = ""; | |
private Paint mTextPaint; | |
private int mTextSize, mTextX, mTextY, mCenter; | |
public CustomTextView(Context context) { | |
this(context, null); | |
} | |
public CustomTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
if (attrs != null) { | |
TypedArray a = context.obtainStyledAttributes(attrs, | |
R.styleable.CustomTextView, 0, 0); | |
try { | |
mText = a.getString(R.styleable.CustomTextView_android_text); | |
mTextSize = a.getDimensionPixelSize( | |
R.styleable.CustomTextView_android_textSize, 15); | |
} finally { | |
a.recycle(); | |
} | |
} | |
init(); | |
} | |
public void setText(CharSequence text) { | |
if (text == null) { | |
text = ""; | |
} | |
mText = text.toString(); | |
invalidate(); | |
} | |
private void setTextSize(int textSize) { | |
mTextSize = textSize; | |
} | |
public String getText() { | |
return mText; | |
} | |
public int getTextSize() { | |
return mTextSize; | |
} | |
@Override | |
public Parcelable onSaveInstanceState() { | |
Bundle state = new Bundle(); | |
state.putParcelable(SUPERSTATE, super.onSaveInstanceState()); | |
state.putString(TEXT, getText()); | |
state.putFloat(TEXTSIZE, getTextSize()); | |
return (state); | |
} | |
@Override | |
public void onRestoreInstanceState(Parcelable ss) { | |
Bundle state = (Bundle) ss; | |
super.onRestoreInstanceState(state.getParcelable(SUPERSTATE)); | |
setText(state.getString(TEXT)); | |
setTextSize(state.getInt(TEXTSIZE)); | |
} | |
private void init() { | |
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
mTextPaint.setColor(0xFF000000); | |
mTextPaint.setTextSize(mTextSize); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
mTextX = mCenter - (int)(mTextPaint.measureText(mText)) / 2; | |
canvas.drawText(mText, mTextX, mTextY, mTextPaint); | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
mCenter = (w - getPaddingLeft() - getPaddingRight()) / 2; | |
mTextX = mCenter - (int)(mTextPaint.measureText(mText)) / 2; | |
mTextY = (int) mTextPaint.getTextSize(); | |
} | |
} |
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
public class CustomTextViewActivity extends Activity implements | |
android.view.View.OnClickListener { | |
private Button defButton, customButton; | |
private TextView defTextView; | |
private CustomTextView customTextView; | |
private final int CYCLES = 10000; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.custom_text_view); | |
defButton = (Button) findViewById(R.id.defButton); | |
defButton.setOnClickListener(this); | |
customButton = (Button) findViewById(R.id.customButton); | |
customButton.setOnClickListener(this); | |
defTextView = (TextView) findViewById(R.id.defTextView); | |
customTextView = (CustomTextView) findViewById(R.id.customTextView); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.custom_text_view, menu); | |
return true; | |
} | |
@Override | |
public void onClick(View v) { | |
Date before = new Date(); | |
if (v.getId() == R.id.defButton) { | |
for (int i = 0; i < CYCLES; i++) { | |
defTextView.setText(String.valueOf(i)); | |
} | |
long delta = new Date().getTime() - before.getTime(); | |
defTextView.setText("Time taken for " + String.valueOf(CYCLES) | |
+ " text changes: " + String.valueOf(delta) + "ms"); | |
} else { | |
for (int i = 0; i < CYCLES; i++) { | |
customTextView.setText(String.valueOf(i)); | |
} | |
long delta = new Date().getTime() - before.getTime(); | |
customTextView.setText("Time taken for " + String.valueOf(CYCLES) | |
+ " text changes: " + String.valueOf(delta) + "ms"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment