1 package org.coolreader.crengine; 2 3 import org.coolreader.CoolReader; 4 5 import android.graphics.Canvas; 6 import android.graphics.Paint; 7 import android.graphics.Rect; 8 import android.view.View; 9 import android.view.ViewGroup.LayoutParams; 10 11 class PositionIndicator extends View { 12 13 private final int INDICATOR_HEIGHT = 8; 14 15 private int color = 0; 16 private int percent = 0; 17 PositionIndicator(CoolReader context)18 public PositionIndicator(CoolReader context) { 19 super(context); 20 setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 21 this.color = context.settings().getColor(Settings.PROP_STATUS_FONT_COLOR, 0); 22 //setBackgroundColor(0xC0404040); 23 } 24 setColor(int color)25 public void setColor(int color) { 26 this.color = color & 0xFFFFFF; 27 if (isShown()) 28 invalidate(); 29 } 30 31 @Override onDraw(Canvas canvas)32 protected void onDraw(Canvas canvas) { 33 Paint readPaint = Utils.createSolidPaint(0xC0000000 | color); 34 Paint unreadPaint = Utils.createSolidPaint(0x40000000 | color); 35 int w = getWidth(); 36 int h = getHeight(); 37 int pos = percent; 38 int x = w * pos / 10000; 39 canvas.drawRect(new Rect(getLeft(), h/2 - 3, getLeft() + x, h/2 + 0), readPaint); 40 canvas.drawRect(new Rect(getLeft() + x, h/2 - 3, getLeft() + w, h/2 + 0), unreadPaint); 41 } 42 43 @Override onLayout(boolean changed, int left, int top, int right, int bottom)44 protected void onLayout(boolean changed, int left, int top, int right, 45 int bottom) { 46 super.onLayout(changed, left, top, right, bottom); 47 } 48 49 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)50 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 51 int w = MeasureSpec.getSize(widthMeasureSpec); 52 int h = INDICATOR_HEIGHT; 53 setMeasuredDimension(w, h); 54 } 55 setPosition(int percent)56 public void setPosition(int percent) { 57 if (this.percent == percent) 58 return; 59 this.percent = percent; 60 if (isShown()) 61 invalidate(); 62 } 63 }