您的位置:首页 > 其它

自定义圆角TextView 背景色

2017-03-18 15:36 246 查看
在开发工程中可能会写很多不同颜色 倒角的shape 作为TextView 或Button的北京实现倒角的效果

管理先来张图



代码如下:

package com.yighao.secondcar.secondHandCar.customView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.TextView;

import com.yighao.secondcar.R;
import com.yighao.secondcar.secondHandCar.utils.DensityUtils;

/**
* Created by Administrator on 2017/2/16 0016.
*/

public class CirButton extends TextView {

private Paint mPaint;

private int radiu;

public CirButton(Context context) {
super(context);
}

public CirButton(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.FILL);

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.cir_button);

int resourceId = typedArray.getResourceId(R.styleable.cir_button_cirColor, R.color.blue);

int color = context.getResources().getColor(resourceId);

mPaint.setColor(color);
int integer = typedArray.getInteger(R.styleable.cir_button_cirRadiu, 10);
radiu = DensityUtils.dp2px(context, integer);
typedArray.recycle();
}

@Override
protected void onDraw(Canvas canvas) {
RectF rectF = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
canvas.drawRoundRect(rectF, radiu, radiu, mPaint);
super.onDraw(canvas);
}
}

只是在OnDraw方法中画了一个圆角矩形  切记在super之前画

自定义两个属性 :

<resources>

<declare-styleable name="cir_button">
<attr name="cirColor" format="reference" />
<attr name="cirRadiu" format="reference|integer" />
</declare-styleable>
</resources>


用法如下:

<com.yighao.secondcar.secondHandCar.customView.CirButton
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="adgdgg"
app:cirColor="@color/orange"  背景颜色
app:cirRadiu="10" />            倒角大小  单位为dp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: