Android 代码片段

来源:互联网 发布:unity3d 水波纹特效 编辑:程序博客网 时间:2024/06/05 15:28

staticlayout使用讲解

处理换行的工具类StaticLayout已经实现了文本绘制换行处理

 TextPaint tp = new TextPaint();            tp.setColor(Color.BLUE);            tp.setStyle(Style.FILL);            tp.setTextSize(50);            String message = "paint,draw paint指用颜色画,如油画颜料、水彩或者水墨画,而draw 通常指用铅笔、钢笔或者粉笔画,后者一般并不涂上颜料。两动词的相应名词分别为p";            StaticLayout myStaticLayout = new StaticLayout(message, tp, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);            myStaticLayout.draw(canvas);            canvas.restore();

参数解释
StaticLayout的构造函数有三个:

public StaticLayout(CharSequence source,                    TextPaint paint,                    int width,                    Layout.Alignment align,                    float spacingmult,                    float spacingadd,                    boolean includepad)public StaticLayout(CharSequence source,                    int bufstart,                    int bufend,                    TextPaint paint,                    int outerwidth,                    Layout.Alignment align,                    float spacingmult,                    float spacingadd,                    boolean includepad)public StaticLayout(CharSequence source,                    int bufstart,                    int bufend,                    TextPaint paint,                    int outerwidth,                    Layout.Alignment align,                    float spacingmult,                    float spacingadd,                    boolean includepad,                    TextUtils.TruncateAt ellipsize,                    int ellipsizedWidth)1.需要分行的字符串2.需要分行的字符串从第几的位置开始3.需要分行的字符串到哪里结束4.画笔对象5.layout的宽度,字符串超出宽度时自动换行。6.layout的对其方式,有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE 三种。7.相对行间距,相对字体大小,1.5f表示行间距为1.5倍的字体高度。8.在基础行距上添加多少实际行间距等于这两者的和。9.参数未知10.从什么位置开始省略11.超过多少开始省略需要指出的是这个layout是默认画在Canvas的(0,0)点的,如果需要调整位置只能在draw之前移Canvas的起始坐标canvas.translate(x,y);

自定义属性xml

在res/value文件夹下边新建attr文件

<declare-styleable name="CountDownView">        <attr name="background_color" format="color" />        <attr name="border_width" format="dimension" />        <attr name="border_color" format="color" />        <attr name="text" format="string" />        <attr name="text_size" format="dimension" />        <attr name="text_color" format="color" />    </declare-styleable>

获取自定义属性

 //获取自定义属性值             TypedArray ta = context.obtainStyledAttributes(attrs,       R.styleable.CountDownView);//        获取进度的背景颜色        backgroundColor = ta.getColor(R.styleable.CountDownView_background_color, BACKGROUND_COLOR);        borderColor = ta.getColor(R.styleable.CountDownView_border_color, BORDER_COLOR);        borderWidth = ta.getDimension(R.styleable.CountDownView_border_width, BORDER_WIDTH);        text = ta.getString(R.styleable.CountDownView_text);        if (TextUtils.isEmpty(text)) {            text = TEXT;        }        textColor = ta.getColor(R.styleable.CountDownView_text_color, TEXT_COLOR);        textSize = ta.getDimension(R.styleable.CountDownView_text_size, TEXT_SIZE);        ta.recycle();        init();

Paint常用的方法

参考:http://blog.sina.com.cn/s/blog_5da93c8f0101d4mx.html
http://blog.csdn.net/lovexieyuan520/article/details/50732023

Paint mp = new Paint();circlePaint = new Paint();circlePaint.setAntiAlias(true); //设置抗锯齿circlePaint.setDither(true);//设置防抖动circlePaint.setColor(backgroundColor); //设置画笔颜色circlePaint.setStyle(Paint.Style.FILL); //设置画笔为实心mp.setFakeBoldText(true); //true为粗体,false为非粗体mp.setTextSkewX(-0.5f); //float类型参数,负数表示右斜,整数左斜mp.setUnderlineText(true); //true为下划线,false为非下划线mp.setStrikeThruText(true); //true为删除线,false为非删除线   setAntiAlias: 设置画笔的锯齿效果。   setColor: 设置画笔颜色   setARGB:  设置画笔的a,r,p,g值。   setAlpha:  设置Alpha值   setTextSize: 设置字体尺寸。   setStyle:  设置画笔风格,空心或者实心。   setStrokeWidth: 设置空心的边框宽度。   getColor:  得到画笔的颜色   getAlpha:  得到画笔的Alpha值。
0 0