コード例 #1
0
  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);
  }
コード例 #2
0
  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();
  }
コード例 #3
0
  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);
    }
  }