您的位置:首页 > 其它

自定义控件 shadeView 做阴影的控件

2016-05-06 23:22 387 查看
一, ShadeView 继承View

public class ShadeView extends View {
String text;
int text_color, shade_color;//文本颜色 与阴影颜色
float text_size, shade_size;// 文本大小与 阴影大小
int viewWidth, viewHeight;  // 父控件的宽 高

public ShadeView(Context context, AttributeSet attrs) {
super(context, attrs);
// TypedArray 封装自定义控件的属性   属性就引用资源文件
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Shade);
text = typedArray.getString(R.styleable.Shade_text);
text_color = typedArray.getColor(R.styleable.Shade_text_color,Color.BLACK);
shade_color = typedArray.getColor(R.styleable.Shade_shade_color,Color.GRAY);
text_size = typedArray.getFloat(R.styleable.Shade_text_size, 10);
shade_size = text_size / 10;
// 回收
typedArray.recycle();
}

@Override //测量方法
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 得文字的大小  文字的大小用像素来设置,这样解决不同分辨率屏幕的适配问题
Paint paint = new Paint();
int px = DisplayUtil.dp2px(getContext(), (int) text_size);
// 代码中字号单位是像素
paint.setTextSize(px);
Rect rect = new Rect();
paint.getTextBounds(text, 0, text.length(), rect);
// 设置控件的大小 Dimension 尺寸
// 392,45
setMeasuredDimension((int) (rect.width() + shade_size),
(int) (rect.height() + shade_size));

}

@Override //绘制
protected void onDraw(Canvas canvas) {
Log.i("绘制顺序", this.toString() + "onDraw ");

Paint paint = new Paint();
// 画背景
// Rect background = new Rect(0, 0, viewWidth, viewHeight);
// paint.setColor(Color.RED);
// canvas.drawRect(background, paint);
int px = DisplayUtil.dp2px(getContext(), (int) text_size);
// 代码中字号单位是像素
paint.setTextSize(px);

// 画阴影
paint.setColor(shade_color);
canvas.drawText(text, shade_size, viewHeight - shade_size / 5, paint);
// 画文字
paint.setColor(text_color);
canvas.drawText(text, 0, viewHeight - shade_size, paint);
}

@Override  //测量之后 就可以拿父控件的宽 高
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewHeight = h;
viewWidth = w;
}

}


二 ,用来把dp或sp转换成px (像素) 的工具类

public class DisplayUtil {
/**
* 把dp或sp转成px
* @param context
* @param dp
* @return
*/
public static int dp2px(Context context, int dp) {
// mdpi 1dp=1px
// hdpi 1dp=1.5px
// xhdpi 720*1280 1dp=2px
// xxhdpi 1080*1920 1dp=3px
// xxxhdpi 1440*2560 1dp=4px
Resources resources = context.getResources();
// 1个dp或sp等于多少个像素点
float density = resources.getDisplayMetrics().scaledDensity;
// 6.5-->6
// 6.5+0.5=7    +0.5F 是为四舍五入的时候 大一位
float px = density * dp + 0.5F;
return (int) px;
}

}


三,布局layout 文件

com.teeeeee.all.widget.ShadeView:包名+自定义的类名

<com.teeeeee.all.widget.ShadeView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Shade:shade_color="#FF888888"
Shade:text="中华大地 万里河山"
Shade:text_color="#FF0000FF"
Shade:text_size="48" />


四 ,引用的资源 在res–>valus 文件中 新建attrs.xml文件

format 表示属性的格式类型

<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="Shade">
<attr name="text" format="string"></attr>
<attr name="text_color" format="color"></attr>
<attr name="shade_color" format="color"></attr>
<attr name="text_size" format="float"></attr>
</declare-styleable>
</resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: