Added Android code
[wl-app.git] / Android / folioreader / src / main / java / com / folioreader / view / UnderlinedTextView.java
1 package com.folioreader.view;
2
3 import android.content.Context;
4 import android.content.res.TypedArray;
5 import android.graphics.Canvas;
6 import android.graphics.Paint;
7 import android.graphics.Rect;
8 import android.support.v7.widget.AppCompatTextView;
9 import android.text.Layout;
10 import android.util.AttributeSet;
11
12 import com.folioreader.R;
13
14 /**
15  * Created by mobisys on 7/4/2016.
16  */
17 public class UnderlinedTextView extends AppCompatTextView {
18
19     private Rect mRect;
20     private Paint mPaint;
21     private int mColor;
22     private float mDensity;
23     private float mStrokeWidth;
24
25     public UnderlinedTextView(Context context) {
26         this(context, null, 0);
27     }
28
29     public UnderlinedTextView(Context context, AttributeSet attrs) {
30         this(context, attrs, 0);
31     }
32
33     public UnderlinedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
34         super(context, attrs, defStyleAttr);
35         init(context, attrs, defStyleAttr);
36     }
37
38     private void init(Context context, AttributeSet attributeSet, int defStyle) {
39
40         mDensity = context.getResources().getDisplayMetrics().density;
41
42         TypedArray typedArray =
43                 context.obtainStyledAttributes(attributeSet, R.styleable.UnderlinedTextView,
44                         defStyle, 0);
45         mStrokeWidth =
46                 typedArray.getDimension(
47                         R.styleable.UnderlinedTextView_underlineWidth,
48                         mDensity * 2);
49         typedArray.recycle();
50
51         mRect = new Rect();
52         mPaint = new Paint();
53         mPaint.setStyle(Paint.Style.STROKE);
54         mPaint.setColor(mColor); //line mColor
55         mPaint.setStrokeWidth(mStrokeWidth);
56     }
57
58     public int getUnderLineColor() {
59         return mColor;
60     }
61
62     public void setUnderLineColor(int mColor) {
63         this.mColor = mColor;
64         mRect = new Rect();
65         mPaint = new Paint();
66         mPaint.setStyle(Paint.Style.STROKE);
67         mPaint.setColor(mColor); //line mColor
68         mPaint.setStrokeWidth(mStrokeWidth);
69         postInvalidate();
70     }
71
72     public float getUnderlineWidth() {
73         return mStrokeWidth;
74     }
75
76     public void setUnderlineWidth(float mStrokeWidth) {
77         this.mStrokeWidth = mStrokeWidth;
78         postInvalidate();
79     }
80
81     @Override
82     protected void onDraw(Canvas canvas) {
83         int count = getLineCount();
84
85         final Layout layout = getLayout();
86         float xStart, xStop, xDiff;
87         int firstCharInLine, lastCharInLine;
88
89         for (int i = 0; i < count; i++) {
90             int baseline = getLineBounds(i, mRect);
91             firstCharInLine = layout.getLineStart(i);
92             lastCharInLine = layout.getLineEnd(i);
93
94             xStart = layout.getPrimaryHorizontal(firstCharInLine);
95             xDiff = layout.getPrimaryHorizontal(firstCharInLine + 1) - xStart;
96             xStop = layout.getPrimaryHorizontal(lastCharInLine - 1) + xDiff;
97
98             canvas.drawLine(xStart,
99                     baseline + mStrokeWidth,
100                     xStop,
101                     baseline + mStrokeWidth,
102                     mPaint);
103         }
104
105         super.onDraw(canvas);
106     }
107 }