自定义控件 shadeView 做阴影的控件
2016-05-06 23:22
387 查看
一, ShadeView 继承View
二 ,用来把dp或sp转换成px (像素) 的工具类
三,布局layout 文件
com.teeeeee.all.widget.ShadeView:包名+自定义的类名
四 ,引用的资源 在res–>valus 文件中 新建attrs.xml文件
format 表示属性的格式类型
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>
相关文章推荐
- Java文件写入时是否覆盖
- jdk源码分析之HashTable
- Codeforces Round #350 (Div. 2) E (跳转链表)
- Python的学习笔记(四)(MySQL数据库的操作)
- 手机安全卫士03
- linux常用命令加实例大全
- 使用telnet发送附件邮件
- ELF文件格式
- Android中pendingIntent的深入理解
- C++中new的用法
- python爬虫 之 wooyun公开漏洞
- PAT-B 1040. 有几个PAT
- Java千百问_06数据结构(007)_String属于基本数据类型吗
- wireshark使用心得
- 【在线】Actionbar Style Generator:ActionBar风格生成器
- html中各种hr样式
- LinkedHashMap原理及LRU应用
- 【Android】Bitmap优化
- android 插件化 模块化开发
- 20145305 《Java程序设计》第10周学习总结