Created
August 25, 2015 14:03
-
-
Save dominicbartl/f2d367edaf0e92cf8bc7 to your computer and use it in GitHub Desktop.
This view is not finished
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.contentfleet.corporatecampus.ui.widget; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.ColorFilter; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import android.os.Build; | |
import android.util.AttributeSet; | |
import android.view.Gravity; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.widget.FrameLayout; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
public class SeekBar extends LinearLayout { | |
private FrameLayout linearTextContainer; | |
private Bar barView; | |
public SeekBar(Context context) { | |
super(context); | |
init(); | |
} | |
public SeekBar(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public SeekBar(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public SeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(); | |
} | |
private void init() { | |
setOrientation(VERTICAL); | |
linearTextContainer = new FrameLayout(getContext()); | |
linearTextContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); | |
barView = new Bar(getContext()); | |
addView(barView); | |
addView(linearTextContainer); | |
//TEST | |
String[] labels = new String[]{ | |
"1h", | |
"24h", | |
"7 days", | |
"14 days", | |
"30 days" | |
}; | |
setLabels(labels); | |
barView.setSelectedCircle(3); | |
} | |
public void setLabels(String[] labels) { | |
barView.setSteps(labels.length); | |
linearTextContainer.removeAllViews(); | |
for (String l : labels) { | |
TextView tv = new TextView(getContext()); | |
tv.setText(l); | |
tv.setGravity(Gravity.CENTER); | |
FrameLayout.LayoutParams layoutParams = | |
new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); | |
tv.setLayoutParams(layoutParams); | |
linearTextContainer.addView(tv); | |
} | |
positionTexts(); | |
} | |
private void positionTexts() { | |
for (int i = 0; i < linearTextContainer.getChildCount(); i++) { | |
View v = linearTextContainer.getChildAt(i); | |
v.measure(0, 0); | |
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams(); | |
layoutParams.leftMargin = barView.getCenterOfCircle(i) - v.getMeasuredWidth() / 2; | |
} | |
} | |
private class Bar extends View implements View.OnTouchListener{ | |
private Paint barPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
private Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
private int padding = 50; | |
private int barSize = 15; | |
private int circleCount = 5; | |
private int circleRadius = 20; | |
private int inactiveColor = Color.GRAY; | |
private int activeColor = Color.CYAN; | |
private int circleStep; | |
private int selected = 0; | |
private Drawable thumbDrawable; | |
public Bar(Context context) { | |
super(context); | |
barPaint.setStyle(Paint.Style.STROKE); | |
barPaint.setStrokeWidth(barSize); | |
barPaint.setColor(inactiveColor); | |
circlePaint.setColor(inactiveColor); | |
setPadding(padding, 0, padding, 0); | |
thumbDrawable = new ThumbDrawable(60); | |
setOnTouchListener(this); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
int vCenter = getHeight() / 2; | |
barPaint.setColor(inactiveColor); | |
canvas.drawLine(getStart(), vCenter, getEnd(), vCenter, barPaint); | |
barPaint.setColor(activeColor); | |
canvas.drawLine(getStart(), vCenter, getCenterOfCircle(selected), vCenter, barPaint); | |
for (int i = 0; i < circleCount; i++) { | |
if (i < selected) { | |
circlePaint.setColor(activeColor); | |
} else { | |
circlePaint.setColor(inactiveColor); | |
} | |
int x = getCenterOfCircle(i); | |
canvas.drawCircle(x, vCenter, circleRadius, circlePaint); | |
} | |
thumbDrawable.draw(canvas); | |
} | |
private int getStart() { | |
return getPaddingLeft() + circleRadius; | |
} | |
private int getEnd() { | |
return getWidth() - getPaddingRight() - circleRadius; | |
} | |
public int getCenterOfCircle(int i) { | |
return (i * circleStep) + getStart(); | |
} | |
public void setSelectedCircle(int index) { | |
selected = index; | |
invalidate(); | |
} | |
public void setSteps(int steps) { | |
this.circleCount = steps; | |
circleStep = (getWidth() - (2 * getStart())) / (circleCount - 1); | |
invalidate(); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int desiredWidth = 200; | |
int desiredHeight = 100; | |
int widthMode = MeasureSpec.getMode(widthMeasureSpec); | |
int widthSize = MeasureSpec.getSize(widthMeasureSpec); | |
int heightMode = MeasureSpec.getMode(heightMeasureSpec); | |
int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
int width; | |
int height; | |
//Measure Width | |
if (widthMode == MeasureSpec.EXACTLY) { | |
//Must be this size | |
width = widthSize; | |
} else if (widthMode == MeasureSpec.AT_MOST) { | |
//Can't be bigger than... | |
width = Math.min(desiredWidth, widthSize); | |
} else { | |
//Be whatever you want | |
width = desiredWidth; | |
} | |
//Measure Height | |
if (heightMode == MeasureSpec.EXACTLY) { | |
//Must be this size | |
height = heightSize; | |
} else if (heightMode == MeasureSpec.AT_MOST) { | |
//Can't be bigger than... | |
height = Math.min(desiredHeight, heightSize); | |
} else { | |
//Be whatever you want | |
height = desiredHeight; | |
} | |
//MUST CALL THIS | |
setMeasuredDimension(width, height); | |
} | |
@Override | |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { | |
super.onLayout(changed, left, top, right, bottom); | |
setSteps(circleCount); | |
SeekBar parent = (SeekBar) getParent(); | |
parent.positionTexts(); | |
Rect rect = thumbDrawable.copyBounds(); | |
rect.top = getHeight()/2; | |
thumbDrawable.setBounds(rect); | |
} | |
@Override | |
public boolean onTouch(View view, MotionEvent motionEvent) { | |
int x = (int) motionEvent.getX(); | |
Rect rect = thumbDrawable.copyBounds(); | |
int s = rect.right; | |
if (motionEvent.getAction() != MotionEvent.ACTION_UP) { | |
rect.left = x; | |
rect.right = s + x; | |
} else { | |
// TODO snap drawable to points | |
} | |
thumbDrawable.setBounds(rect); | |
invalidate(); | |
return true; | |
} | |
} | |
private class ThumbDrawable extends Drawable { | |
private Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
private Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
private int radius; | |
public ThumbDrawable(int size) { | |
outerPaint.setColor(Color.CYAN); | |
innerPaint.setColor(Color.rgb(150,150,150)); | |
setBounds(0, 0, size, size); | |
radius = size / 2; | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
Rect b = getBounds(); | |
canvas.drawCircle(b.left, b.top, radius, outerPaint); | |
} | |
@Override | |
public void setAlpha(int i) { | |
} | |
@Override | |
public void setColorFilter(ColorFilter colorFilter) { | |
} | |
@Override | |
public int getOpacity() { | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment