Back to project page HexNanoController_Android.
The source code is released under:
Code license GNU GPL v2 http://www.gnu.org/licenses/gpl.html Content license CC BY-NC-SA 4.0 http://creativecommons.org/licenses/by-nc-sa/4.0/
If you think the Android project HexNanoController_Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.hexairbot.hexmini.ui.control; //from ww w. j a v a2 s. c o m import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.Gravity; import android.widget.SeekBar; import android.widget.TableLayout.LayoutParams; import android.widget.TextView; import com.hexairbot.hexmini.R; public class CustomSeekBar extends SeekBar { private TextView lowerBoundView; private TextView upperBoundView; private int textColor = Color.WHITE; public CustomSeekBar(Context context, AttributeSet attrs) { super (context, attrs); setThumbOffset(2); lowerBoundView = new TextView(context, attrs); lowerBoundView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); lowerBoundView.setGravity(Gravity.LEFT); upperBoundView = new TextView(context, attrs); upperBoundView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); upperBoundView.setGravity(Gravity.RIGHT); initControl(context, attrs); updateTextColor(); } public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setThumbOffset(2); lowerBoundView = new TextView(context, attrs, defStyle); lowerBoundView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); lowerBoundView.setGravity(Gravity.LEFT); upperBoundView = new TextView(context, attrs, defStyle); upperBoundView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); upperBoundView.setGravity(Gravity.RIGHT); initControl(context, attrs); updateTextColor(); } private void initControl(Context context, AttributeSet attrs) { Resources res = context.getResources(); float topPadding = res.getDimension(R.dimen.settings_seek_text_padding_top); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomSeekBar); float lowerBoundLeft = a.getDimension(R.styleable.CustomSeekBar_lowerValuePaddingLeft, 0); float upperBoundRight = a.getDimension(R.styleable.CustomSeekBar_upperValuePaddingRight, 0); topPadding = a.getDimension(R.styleable.CustomSeekBar_textPaddingTop, topPadding); String upperBoundText = a.getString(R.styleable.CustomSeekBar_upperBoundText); String lowerBoundText = a.getString(R.styleable.CustomSeekBar_lowerBoundText); lowerBoundView.setText(lowerBoundText); upperBoundView.setText(upperBoundText); lowerBoundView.setPadding((int)lowerBoundLeft, (int)topPadding, 0, 0); upperBoundView.setPadding(0, (int)topPadding, (int)upperBoundRight, 0); a.recycle(); } @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); lowerBoundView.draw(canvas); upperBoundView.draw(canvas); } @Override public void invalidate() { updateTextColor(); super.invalidate(); } private void updateTextColor() { float progr = (float)getProgress() / (float)getMax(); if (lowerBoundView != null) { if (progr < 0.05) { lowerBoundView.setTextColor(Color.BLACK); } else { lowerBoundView.setTextColor(textColor); } } if (upperBoundView != null) { if (progr > 0.93) { upperBoundView.setTextColor(Color.BLACK); } else { upperBoundView.setTextColor(textColor); } } } @Override protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { lowerBoundView.measure(widthMeasureSpec, heightMeasureSpec); upperBoundView.measure(widthMeasureSpec, heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); lowerBoundView.layout(left, top, right, bottom); upperBoundView.layout(left, top, right, bottom); } public void setTypeface(Typeface tf) { lowerBoundView.setTypeface(tf); upperBoundView.setTypeface(tf); } }