Android Open Source - HexNanoController_Android Custom Seek Bar






From Project

Back to project page HexNanoController_Android.

License

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.

Java Source Code

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);
  }
}




Java Source Code List

.FileHelper.java
.Input.java
.Output.java
.Serializable.java
com.hexairbot.hexmini.HelpActivity.java
com.hexairbot.hexmini.HexMiniApplication.java
com.hexairbot.hexmini.HudActivity.java
com.hexairbot.hexmini.HudViewControllerDelegate.java
com.hexairbot.hexmini.HudViewController.java
com.hexairbot.hexmini.SettingsDialogDelegate.java
com.hexairbot.hexmini.SettingsDialog.java
com.hexairbot.hexmini.SettingsViewControllerDelegate.java
com.hexairbot.hexmini.SettingsViewController.java
com.hexairbot.hexmini.ViewController.java
com.hexairbot.hexmini.adapter.SettingsViewAdapter.java
com.hexairbot.hexmini.ble.BleConnectinManagerDelegate.java
com.hexairbot.hexmini.ble.BleConnectinManager.java
com.hexairbot.hexmini.ble.BluetoothLeService.java
com.hexairbot.hexmini.gestures.EnhancedGestureDetector.java
com.hexairbot.hexmini.modal.ApplicationSettings.java
com.hexairbot.hexmini.modal.Channel.java
com.hexairbot.hexmini.modal.OSDCommon.java
com.hexairbot.hexmini.modal.Transmitter.java
com.hexairbot.hexmini.sensors.DeviceOrientationChangeDelegate.java
com.hexairbot.hexmini.sensors.DeviceOrientationManager.java
com.hexairbot.hexmini.sensors.DeviceSensorManagerWrapper.java
com.hexairbot.hexmini.sensors.SensorManagerWrapper.java
com.hexairbot.hexmini.services.ConnectStateManager.java
com.hexairbot.hexmini.services.IpcControlService.java
com.hexairbot.hexmini.services.IpcProxy.java
com.hexairbot.hexmini.services.NavData.java
com.hexairbot.hexmini.services.OnIpcConnectChangedListener.java
com.hexairbot.hexmini.services.VIConfig.java
com.hexairbot.hexmini.ui.Button.java
com.hexairbot.hexmini.ui.Image.java
com.hexairbot.hexmini.ui.Indicator.java
com.hexairbot.hexmini.ui.Sprite.java
com.hexairbot.hexmini.ui.Text.java
com.hexairbot.hexmini.ui.ToggleButton.java
com.hexairbot.hexmini.ui.UIRenderer.java
com.hexairbot.hexmini.ui.control.CustomSeekBar.java
com.hexairbot.hexmini.ui.control.ViewPagerIndicator.java
com.hexairbot.hexmini.ui.gl.GLSprite.java
com.hexairbot.hexmini.ui.joystick.AcceleratorJoystick.java
com.hexairbot.hexmini.ui.joystick.AnalogueJoystick.java
com.hexairbot.hexmini.ui.joystick.JoystickBase.java
com.hexairbot.hexmini.ui.joystick.JoystickFactory.java
com.hexairbot.hexmini.ui.joystick.JoystickListener.java
com.hexairbot.hexmini.util.DebugHandler.java
com.hexairbot.hexmini.util.FontUtils.java
com.hexairbot.hexmini.util.SystemUiHiderBase.java
com.hexairbot.hexmini.util.SystemUiHiderHoneycomb.java
com.hexairbot.hexmini.util.SystemUiHider.java
com.hexairbot.hexmini.util.SystemUtil.java
com.hexairbot.hexmini.util.TextureUtils.java
fix.android.opengl.GLES20.java