How to make RelativeSizeSpan align to top

后端 未结 9 2005
情深已故
情深已故 2021-01-31 03:14

I have the following String RM123.456. I would like to

  • Make RM relatively smaller
  • Make RM aligned to to
9条回答
  •  遥遥无期
    2021-01-31 03:29

    I had a look at the RelativeSizeSpan and found a rather simple implementation. So you could just implement your own RelativeSizeSpan for your purpose. The only difference here is that it doesn't implement ParcelableSpan, since this is only intended for framework code. AntiRelativeSizeSpan is just a fast hack without much testing of course, but it seems to work fine. It completely relies on Paint.getTextBounds() to find the best value for the baselineShift, but maybe there'd be a better approach.

    public class AntiRelativeSizeSpan extends MetricAffectingSpan {
        private final float mProportion;
    
        public AntiRelativeSizeSpan(float proportion) {
            mProportion = proportion;
        }
    
        public float getSizeChange() {
            return mProportion;
        }
    
        @Override
        public void updateDrawState(TextPaint ds) {
            updateAnyState(ds);
        }
    
        @Override
        public void updateMeasureState(TextPaint ds) {
            updateAnyState(ds);
        }
    
        private void updateAnyState(TextPaint ds) {
            Rect bounds = new Rect();
            ds.getTextBounds("1A", 0, 2, bounds);
            int shift = bounds.top - bounds.bottom;
            ds.setTextSize(ds.getTextSize() * mProportion);
            ds.getTextBounds("1A", 0, 2, bounds);
            shift += bounds.bottom - bounds.top;
            ds.baselineShift += shift;
        }
    }
    

提交回复
热议问题