Skip to content

Instantly share code, notes, and snippets.

@searover
Last active August 29, 2015 14:18
Show Gist options
  • Save searover/bf93ba819b2d63ba2766 to your computer and use it in GitHub Desktop.
Save searover/bf93ba819b2d63ba2766 to your computer and use it in GitHub Desktop.
android-snippets
/**
* Created by searover on 3/29/15.
*/
public class CustomTitleView extends View {
/**
* 文本
*/
private String mTitleText;
/**
* 文本颜色
*/
private int mTitleTextColor;
/**
* 文本大小
*/
private int mTitleTextSize;
/**
* 绘制时控制文本绘制的范围
*/
private Rect mBound;
private Paint mPaint;
private static final String TAG = "CustomTitleView";
public CustomTitleView(Context context) {
this(context, null);
}
public CustomTitleView(Context context, AttributeSet attrs) {
this(context,attrs,0);
}
/**
* 获取我们自定义的样式属性
* @param context
* @param attrs
* @param defStyleAttr
*/
public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context,attrs,defStyleAttr);
TypedArray array = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyleAttr, 0);
int count = array.getIndexCount();
for (int i = 0; i < count; i++) {
int attr = array.getIndex(i);
switch (attr) {
case R.styleable.CustomTitleView_titleText:
mTitleText = array.getString(attr);
break;
case R.styleable.CustomTitleView_titleTextColor:
mTitleTextColor = array.getColor(attr, Color.BLACK);
break;
case R.styleable.CustomTitleView_titleTextSize:
mTitleTextSize = array.getDimensionPixelSize(attr,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
16, getResources().getDisplayMetrics()));
break;
}
}
array.recycle();
// 获得绘制文本的宽和高
mPaint = new Paint();
mPaint.setTextSize(mTitleTextSize);
mBound = new Rect();
mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTitleText = randomText();
// postInvalidate();
// invalidate();
new Thread(){
public void run(){
postInvalidate();
}
}.start();
}
private String randomText() {
Random random = new Random();
Set<Integer> set = new HashSet<Integer>();
while (set.size() < 4){
int randomInt = random.nextInt(10);
set.add(randomInt);
}
StringBuilder builder = new StringBuilder();
for (Integer i : set){
builder.append("" + i);
}
return builder.toString();
}
});
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
Log.d(TAG,"onMeasure method called");
int width = 0;
int height = 0;
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
switch (specMode){
case MeasureSpec.EXACTLY:
width = getPaddingLeft() + getPaddingRight() + specSize;
break;
case MeasureSpec.AT_MOST:
width = getPaddingLeft() + getPaddingRight() + mBound.width();
break;
}
specMode = MeasureSpec.getMode(heightMeasureSpec);
specSize = MeasureSpec.getSize(heightMeasureSpec);
switch (specMode){
case MeasureSpec.EXACTLY:
height = getPaddingTop() + getPaddingBottom() + specSize;
break;
case MeasureSpec.AT_MOST:
height = getPaddingTop() + getPaddingBottom() + mBound.height();
break;
}
setMeasuredDimension(width,height);
}
@Override
protected void onDraw(Canvas canvas){
Log.w(TAG,"onDraw method has been called");
mPaint.setColor(Color.YELLOW);
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);
mPaint.setColor(mTitleTextColor);
canvas.drawText(mTitleText,getWidth() / 2 - mBound.width() / 2,
getHeight() / 2 + mBound.height() / 2, mPaint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment