zoukankan      html  css  js  c++  java
  • 自定义ImageSpan

        /**
         * span图片垂直居中
         */
        public class VerticalImageSpan extends ImageSpan {
    
            @SuppressWarnings("unused")
            public VerticalImageSpan(Context arg0, int arg1) {
                super(arg0, arg1);
            }
    
            public VerticalImageSpan(Context context, Bitmap b) {
                super(context, b, ALIGN_BASELINE);
            }
    
            public int getSize(Paint paint, CharSequence text, int start, int end,
                               Paint.FontMetricsInt fm) {
                Drawable d = getDrawable();
                Rect rect = d.getBounds();
                if (fm != null) {
                    Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
                    int fontHeight = fmPaint.bottom - fmPaint.top;
                    int drHeight = rect.bottom - rect.top;
    
                    int top = drHeight / 2 - fontHeight / 4;
                    int bottom = drHeight / 2 + fontHeight / 4;
    
                    fm.ascent = -bottom;
                    fm.top = -bottom;
                    fm.bottom = top;
                    fm.descent = top;
                }
                return rect.right;
            }
    
            @Override
            public void draw(Canvas canvas, CharSequence text, int start, int end,
                             float x, int top, int y, int bottom, Paint paint) {
                Drawable b = getDrawable();
                canvas.save();
                int transY;
                transY = ((bottom - top) - b.getBounds().bottom) / 2 + top;
                canvas.translate(x, transY);
                b.draw(canvas);
                canvas.restore();
            }
        }
  • 相关阅读:
    第9天 图片整合
    第六天 元素类型
    第五天 文本溢出
    第四天 盒子模型
    第三天 css核心属性
    第二天 css基础 ,部分选择符
    第一天 HTML基础
    *Move Zeroes
    Word Pattern
    ReentrantLock
  • 原文地址:https://www.cnblogs.com/huyang011/p/7476931.html
Copyright © 2011-2022 走看看