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

一, 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值