private Layout makeLabel(CharSequence text) {
    final Resources res = getResources();
    final TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    paint.density = res.getDisplayMetrics().density;
    paint.setCompatibilityScaling(res.getCompatibilityInfo().applicationScale);
    paint.setColor(mLabelColor);
    paint.setTextSize(mLabelSize);

    return new StaticLayout(
        text,
        paint,
        (int) Math.ceil(Layout.getDesiredWidth(text, paint)),
        Layout.Alignment.ALIGN_NORMAL,
        1.f,
        0,
        true);
  }
Exemplo n.º 2
0
  /**
   * Construct a new Switch with a default style determined by the given theme attribute, overriding
   * specific style attributes as requested.
   *
   * @param context The Context that will determine this widget's theming.
   * @param attrs Specification of attributes that should deviate from the default styling.
   * @param defStyle An attribute ID within the active theme containing a reference to the default
   *     style for this widget. e.g. android.R.attr.switchStyle.
   */
  public Switch(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    Resources res = getResources();
    mTextPaint.density = res.getDisplayMetrics().density;

    // TODO resolve error
    // mTextPaint.setCompatibilityScaling(res.getCompatibilityInfo().applicationScale);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Switch, defStyle, 0);

    mThumbDrawable = a.getDrawable(R.styleable.Switch_thumb);
    mTrackDrawable = a.getDrawable(R.styleable.Switch_track);
    mTextOn = a.getText(R.styleable.Switch_textOn);
    mTextOff = a.getText(R.styleable.Switch_textOff);
    mThumbTextPadding = a.getDimensionPixelSize(R.styleable.Switch_thumbTextPadding, 0);
    mSwitchMinWidth = a.getDimensionPixelSize(R.styleable.Switch_switchMinWidth, 0);
    mSwitchPadding = a.getDimensionPixelSize(R.styleable.Switch_switchPadding, 0);

    int appearance = a.getResourceId(R.styleable.Switch_switchTextAppearance, 0);
    if (appearance != 0) {
      setSwitchTextAppearance(context, appearance);
    }
    a.recycle();

    ViewConfiguration config = ViewConfiguration.get(context);
    mTouchSlop = config.getScaledTouchSlop();
    mMinFlingVelocity = config.getScaledMinimumFlingVelocity();

    // Refresh display with current params
    refreshDrawableState();
    setChecked(isChecked());

    // TODO I don't know why, but in order for the OnCheckedChangeListener to work this switch has
    // to have an OnClickListener
    this.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            // do nothing
          }
        });
  }
  private void invalidateLabelTemplate() {
    if (mLabelTemplateRes != 0) {
      final CharSequence template = getResources().getText(mLabelTemplateRes);

      final TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
      paint.density = getResources().getDisplayMetrics().density;
      paint.setCompatibilityScaling(getResources().getCompatibilityInfo().applicationScale);
      paint.setColor(mLabelColor);
      paint.setShadowLayer(4 * paint.density, 0, 0, Color.BLACK);

      mLabelTemplate = new SpannableStringBuilder(template);
      mLabelLayout =
          new DynamicLayout(
              mLabelTemplate, paint, LARGE_WIDTH, Alignment.ALIGN_RIGHT, 1f, 0f, false);
      invalidateLabel();

    } else {
      mLabelTemplate = null;
      mLabelLayout = null;
    }

    invalidate();
    requestLayout();
  }
  public BatteryHistoryChart(Context context, AttributeSet attrs) {
    super(context, attrs);

    mBatteryBackgroundPaint.setARGB(255, 128, 128, 128);
    mBatteryBackgroundPaint.setStyle(Paint.Style.FILL);
    mBatteryGoodPaint.setARGB(128, 0, 255, 0);
    mBatteryGoodPaint.setStyle(Paint.Style.STROKE);
    mBatteryWarnPaint.setARGB(128, 255, 255, 0);
    mBatteryWarnPaint.setStyle(Paint.Style.STROKE);
    mBatteryCriticalPaint.setARGB(192, 255, 0, 0);
    mBatteryCriticalPaint.setStyle(Paint.Style.STROKE);
    mChargingPaint.setARGB(255, 0, 128, 0);
    mChargingPaint.setStyle(Paint.Style.STROKE);
    mScreenOnPaint.setStyle(Paint.Style.STROKE);
    mGpsOnPaint.setStyle(Paint.Style.STROKE);
    mWifiRunningPaint.setStyle(Paint.Style.STROKE);
    mWakeLockPaint.setStyle(Paint.Style.STROKE);
    mPhoneSignalChart.setColors(
        new int[] {
          0x00000000, 0xffa00000, 0xffa0a000, 0xff808020, 0xff808040, 0xff808060, 0xff008000
        });

    mTextPaint.density = getResources().getDisplayMetrics().density;
    mTextPaint.setCompatibilityScaling(getResources().getCompatibilityInfo().applicationScale);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BatteryHistoryChart, 0, 0);

    ColorStateList textColor = null;
    int textSize = 15;
    int typefaceIndex = -1;
    int styleIndex = -1;

    TypedArray appearance = null;
    int ap = a.getResourceId(R.styleable.BatteryHistoryChart_android_textAppearance, -1);
    if (ap != -1) {
      appearance =
          context.obtainStyledAttributes(ap, com.android.internal.R.styleable.TextAppearance);
    }
    if (appearance != null) {
      int n = appearance.getIndexCount();
      for (int i = 0; i < n; i++) {
        int attr = appearance.getIndex(i);

        switch (attr) {
          case com.android.internal.R.styleable.TextAppearance_textColor:
            textColor = appearance.getColorStateList(attr);
            break;

          case com.android.internal.R.styleable.TextAppearance_textSize:
            textSize = appearance.getDimensionPixelSize(attr, textSize);
            break;

          case com.android.internal.R.styleable.TextAppearance_typeface:
            typefaceIndex = appearance.getInt(attr, -1);
            break;

          case com.android.internal.R.styleable.TextAppearance_textStyle:
            styleIndex = appearance.getInt(attr, -1);
            break;
        }
      }

      appearance.recycle();
    }

    int shadowcolor = 0;
    float dx = 0, dy = 0, r = 0;

    int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {
      int attr = a.getIndex(i);

      switch (attr) {
        case R.styleable.BatteryHistoryChart_android_shadowColor:
          shadowcolor = a.getInt(attr, 0);
          break;

        case R.styleable.BatteryHistoryChart_android_shadowDx:
          dx = a.getFloat(attr, 0);
          break;

        case R.styleable.BatteryHistoryChart_android_shadowDy:
          dy = a.getFloat(attr, 0);
          break;

        case R.styleable.BatteryHistoryChart_android_shadowRadius:
          r = a.getFloat(attr, 0);
          break;

        case R.styleable.BatteryHistoryChart_android_textColor:
          textColor = a.getColorStateList(attr);
          break;

        case R.styleable.BatteryHistoryChart_android_textSize:
          textSize = a.getDimensionPixelSize(attr, textSize);
          break;

        case R.styleable.BatteryHistoryChart_android_typeface:
          typefaceIndex = a.getInt(attr, typefaceIndex);
          break;

        case R.styleable.BatteryHistoryChart_android_textStyle:
          styleIndex = a.getInt(attr, styleIndex);
          break;
      }
    }

    a.recycle();

    mTextPaint.setColor(textColor.getDefaultColor());
    mTextPaint.setTextSize(textSize);

    Typeface tf = null;
    switch (typefaceIndex) {
      case SANS:
        tf = Typeface.SANS_SERIF;
        break;

      case SERIF:
        tf = Typeface.SERIF;
        break;

      case MONOSPACE:
        tf = Typeface.MONOSPACE;
        break;
    }

    setTypeface(tf, styleIndex);

    if (shadowcolor != 0) {
      mTextPaint.setShadowLayer(r, dx, dy, shadowcolor);
    }
  }