自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name = "text" format="string"/>
<attr name="textSize" format="dimension"/>
<attr name="textColor" format="color"/>
<declare-styleable name="MyView">
<attr name = "text" />
<attr name="textSize"/>
<attr name="textColor"/>
</declare-styleable>
</resources>
自定义view部分
public class MyView extends View {
private String mText;
private int mColor;
private int mTextSize;
private Rect mBound;
private Paint mPaint;
public MyView(Context context) {
this(context, null);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, 0);
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, defStyleAttr, 0);
int n = array.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = array.getIndex(i);
switch (attr) {
case R.styleable.MyView_text:
mText = array.getString(attr);
break;
case R.styleable.MyView_textColor:
mColor = array.getColor(attr, Color.BLACK);
break;
case R.styleable.MyView_textSize:
mTextSize = array.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
break;
}
}
array.recycle();
mPaint = new Paint();
mPaint.setTextSize(mTextSize);
mBound = new Rect();
mPaint.getTextBounds(mText, 0, mText.length(), mBound);
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mText = reandomText();
postInvalidate();
}
});
}
private String reandomText() {
Random random = new Random();
Set<Integer> set = new HashSet<>();
while (set.size() < 4) {
int randomInt = random.nextInt(10);
set.add(randomInt);
}
StringBuilder stringBuilder = new StringBuilder();
for (Integer i : set) {
stringBuilder.append(i);
}
return stringBuilder.toString();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
} else {
mPaint.setTextSize(mTextSize);
mPaint.getTextBounds(mText, 0, mText.length(), mBound);
float textWidth = mBound.width() + 100;
width = (int) (getPaddingLeft() + textWidth + getPaddingRight());
}
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else {
mPaint.setTextSize(mTextSize);
mPaint.getTextBounds(mText, 0, mText.length(), mBound);
float textheight = mBound.height() + 100;
height = (int) (getPaddingTop() + textheight + getPaddingBottom());
}
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
mPaint.setColor(Color.YELLOW);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
mPaint.setColor(mColor);
canvas.drawText(mText, (float) (getWidth() / 2 - mBound.width() / 2), (float) (getHeight() / 2 + mBound.height() / 2), mPaint);
for (int a = 0; a < 5; a++) {
drawLine(canvas, mPaint);
}
for (int a = 0; a < 100; a++) {
drawPoint(canvas, mPaint);
}
}
private void drawLine(Canvas canvas, Paint paint) {
Random mRandom = new Random();
int color = randomColor();
int startX = mRandom.nextInt(getMeasuredWidth());
int startY = mRandom.nextInt(getMeasuredHeight());
int stopX = mRandom.nextInt(getMeasuredWidth());
int stopY = mRandom.nextInt(getMeasuredHeight());
paint.setStrokeWidth(4);
paint.setColor(colo
r);
canvas.drawLine(startX, startY, stopX, stopY, paint);
}
private void drawPoint(Canvas canvas, Paint paint){
Random random =new Random();
int color =randomColor();
float x =random.nextInt(getMeasuredWidth());
float y =random.nextInt(getMeasuredHeight());
paint.setColor(color);
paint.setStrokeWidth(4);
canvas.drawPoint(x,y,paint);
}
private int randomColor() {
Random mRandom = new Random();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 8; i++) {
int a = mRandom.nextInt(0xf);
stringBuilder.append(Integer.toHexString(a));
}
Log.e(TAG, "randomColor: "+stringBuilder );
return Color.parseColor("#" + stringBuilder);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.myapplication.MyView
android:id="@+id/myview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:text="1234"
app:textColor="@color/colorAccent"
app:textSize="40sp" />
</android.support.constraint.ConstraintLayout>
在MainActivity中调用
findViewById(R.id.myview);
最后的效果

网友评论