private void initPaintShadow() {
   if (highContrast && needsShadow(color)) {
     paint.setShadowLayer(
         applyDimension(
             COMPLEX_UNIT_DIP, SHADOW_RADIUS_DPS, view.getResources().getDisplayMetrics()),
         0,
         0,
         BLACK);
   } else {
     paint.setShadowLayer(0, 0, 0, BLACK);
   }
 }
예제 #2
0
  /** Initializes resources */
  private void initResourcesIfNecessary() {
    if (itemsPaint == null) {
      itemsPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
      // itemsPaint.density = getResources().getDisplayMetrics().density;
      itemsPaint.setTextSize(TEXT_SIZE);
    }

    if (valuePaint == null) {
      valuePaint =
          new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG | Paint.DITHER_FLAG);
      // valuePaint.density = getResources().getDisplayMetrics().density;
      valuePaint.setTextSize(TEXT_SIZE);
      valuePaint.setShadowLayer(0.1f, 0, 0.1f, 0xFFC0C0C0);
    }

    if (centerDrawable == null) {
      centerDrawable = getContext().getResources().getDrawable(R.drawable.wheel_val);
    }

    if (topShadow == null) {
      topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
    }

    if (bottomShadow == null) {
      bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
    }

    setBackgroundResource(R.drawable.wheel_bg);
  }
예제 #3
0
  private void init() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      setLayerType(LAYER_TYPE_SOFTWARE, null);
    } else {
      setDrawingCacheEnabled(true);
    }

    boolean hasShot =
        getContext()
            .getSharedPreferences(PREFS_SHOWCASE_INTERNAL, Context.MODE_PRIVATE)
            .getBoolean("hasShot" + getConfigOptions().showcaseId, false);
    if (hasShot && mOptions.shotType == TYPE_ONE_SHOT) {
      // The showcase has already been shot once, so we don't need to do anything
      setVisibility(View.GONE);
      isRedundant = true;
      return;
    }
    showcase = getContext().getResources().getDrawable(R.drawable.cling);

    showcaseRadius = metricScale * INNER_CIRCLE_RADIUS;
    PorterDuffXfermode mBlender = new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);
    setOnTouchListener(this);

    mPaintTitle = new Paint();
    mPaintTitle.setColor(titleTextColor);
    mPaintTitle.setShadowLayer(2.0f, 0f, 2.0f, Color.DKGRAY);
    mPaintTitle.setTextSize(24 * metricScale);
    mPaintTitle.setAntiAlias(true);

    mPaintDetail = new TextPaint();
    mPaintDetail.setColor(detailTextColor);
    mPaintDetail.setShadowLayer(2.0f, 0f, 2.0f, Color.DKGRAY);
    mPaintDetail.setTextSize(16 * metricScale);
    mPaintDetail.setAntiAlias(true);

    mEraser = new Paint();
    mEraser.setColor(0xFFFFFF);
    mEraser.setAlpha(0);
    mEraser.setXfermode(mBlender);
    mEraser.setAntiAlias(true);

    if (!mOptions.noButton && mEndButton.getParent() == null) {
      RelativeLayout.LayoutParams lps = (LayoutParams) generateDefaultLayoutParams();
      lps.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
      lps.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
      int margin = ((Number) (metricScale * 12)).intValue();
      lps.setMargins(margin, margin, margin, margin);
      lps.height = LayoutParams.WRAP_CONTENT;
      lps.width = LayoutParams.WRAP_CONTENT;
      mEndButton.setLayoutParams(lps);
      mEndButton.setText(buttonText != null ? buttonText : getResources().getString(R.string.ok));
      if (!hasCustomClickListener) mEndButton.setOnClickListener(this);
      addView(mEndButton);
    }
  }
예제 #4
0
  /** Initializes resources */
  private void initResourcesIfNecessary() {
    if (itemsPaint == null) {
      itemsPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
      itemsPaint.setTextSize(TEXT_SIZE);
    }

    if (valuePaint == null) {
      valuePaint =
          new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG | Paint.DITHER_FLAG);
      valuePaint.setTextSize(TEXT_SIZE);
      valuePaint.setShadowLayer(0.1f, 0, 0.1f, 0xFFC0C0C0);
    }

    // if (centerDrawable == null) {
    // centerDrawable =
    // getContext().getResources().getDrawable(R.drawable.wheel_val);
    // }
  }
  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();
  }
  private void drawText(Canvas canvas, Bitmap bitmap, int percent, Boolean isShort) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    Boolean isGradient = prefs.getBoolean("gradientKey", true);

    int size = 0;
    int smallDummyHeight = 0;
    while (smallDummyHeight < ((bitmap.getHeight() / 100) * percent)) {
      TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG);
      size = size + 1;
      paintText.setTextSize(size);
      paintText.setStyle(Paint.Style.FILL_AND_STROKE);
      paintText.setShadowLayer(3f, 3f, 3f, Color.BLACK);

      Rect smallDummyRect = new Rect();
      paintText.getTextBounds(gagTitle, 0, gagTitle.length(), smallDummyRect);

      smallDummyHeight = smallDummyRect.height();
    }

    int pL = bitmap.getWidth() / 100;
    int pT = bitmap.getHeight() / 100;

    if (isShort) {

      TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG);
      paintText.setColor(Color.WHITE);
      paintText.setTextSize(size);
      paintText.setStyle(Paint.Style.FILL_AND_STROKE);
      paintText.setShadowLayer(3f, 3f, 3f, Color.BLACK);

      Rect rectText = new Rect();
      paintText.getTextBounds(gagTitle, 0, gagTitle.length(), rectText);

      if (isGradient) {
        int GRADIENT_HEIGHT = rectText.height();

        Paint paint = new Paint();
        LinearGradient shader =
            new LinearGradient(
                0, 0, 0, GRADIENT_HEIGHT, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        canvas.drawRect(0, 0, newBitmap.getWidth(), GRADIENT_HEIGHT, paint);
      }

      canvas.drawText(gagTitle, pL, rectText.height() + (3 * pT) / 2, paintText);
    } else {

      TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG);
      paintText.setColor(Color.WHITE);
      paintText.setTextSize(size);
      paintText.setStyle(Paint.Style.FILL_AND_STROKE);
      paintText.setShadowLayer(3f, 3f, 3f, Color.BLACK);

      StaticLayout mTextLayout =
          new StaticLayout(
              gagTitle,
              paintText,
              canvas.getWidth(),
              Layout.Alignment.ALIGN_NORMAL,
              1.0f,
              0.0f,
              false);

      if (isGradient) {
        int GRADIENT_HEIGHT = mTextLayout.getHeight();

        Paint paint = new Paint();
        LinearGradient shader =
            new LinearGradient(
                0, 0, 0, GRADIENT_HEIGHT, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        canvas.drawRect(0, 0, newBitmap.getWidth(), GRADIENT_HEIGHT, paint);
      }

      canvas.save();
      canvas.translate(pL, pT);
      mTextLayout.draw(canvas);
      canvas.restore();
    }
  }
예제 #7
0
  /** Initializes resources. */
  private void initResourcesIfNecessary() {
    if (itemsPaint == null) {
      itemsPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG);
      // itemsPaint.density = getResources().getDisplayMetrics().density;
      itemsPaint.setTextSize(valueTextSize);
    }

    if (valuePaint == null) {
      valuePaint =
          new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG | Paint.DITHER_FLAG);
      // valuePaint.density = getResources().getDisplayMetrics().density;
      valuePaint.setTextSize(valueTextSize);
    }

    if (labelPaint == null) {
      labelPaint =
          new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FAKE_BOLD_TEXT_FLAG | Paint.DITHER_FLAG);
      labelPaint.setTextSize(labelTextSize);
      // 设置阴影
      labelPaint.setShadowLayer(0.5f, 0, 1, 0xFFFFFFFF);
    }

    // 如果没设置中间的选中条用默认的颜色
    if (centerSelectDrawable == null) {
      GradientDrawable mGradientDrawable =
          new GradientDrawable(Orientation.BOTTOM_TOP, centerSelectGradientColors);
      mGradientDrawable.setStroke(centerSelectStrokeWidth, centerSelectStrokeColor);
      centerSelectDrawable = mGradientDrawable;
    }

    /* Android中提供了Shader类专门用来渲染图像以及一些几何图形,
    Shader下面包括几个直接子类,分别是BitmapShader、 ComposeShader、LinearGradient、
    RadialGradient、SweepGradient。 BitmapShader主要用来渲染图像,
    LinearGradient 用来进行梯度渲染,RadialGradient 用来进行环形渲染,
    SweepGradient
    用来进行梯度渲染,ComposeShader则是一个 混合渲染,可以和其它几个子类组合起来使用。 */

    // 上边界渐变层
    if (topShadow == null) {
      topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
    }
    // 下边界渐变层
    if (bottomShadow == null) {
      bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
    }

    if (this.getBackground() == null) {
      // 原来用颜色渐变实现setBackgroundDrawable(layerDrawable);
      // 底部的颜色
      GradientDrawable mGradientDrawable1 =
          new GradientDrawable(Orientation.TOP_BOTTOM, topGradientColors);
      GradientDrawable mGradientDrawable2 =
          new GradientDrawable(Orientation.BOTTOM_TOP, bottomGradientColors);

      mGradientDrawable1.setStroke(topStrokeWidth, topStrokeColor);
      mGradientDrawable1.setShape(GradientDrawable.RECTANGLE);
      mGradientDrawable2.setShape(GradientDrawable.RECTANGLE);
      mGradientDrawable1.setGradientType(GradientDrawable.LINEAR_GRADIENT);
      mGradientDrawable2.setGradientType(GradientDrawable.LINEAR_GRADIENT);

      GradientDrawable[] mDrawables = new GradientDrawable[2];
      mDrawables[0] = mGradientDrawable1;
      mDrawables[1] = mGradientDrawable2;

      LayerDrawable layerDrawable = new LayerDrawable(mDrawables);
      layerDrawable.setLayerInset(0, 0, 0, 0, 0); // 第一个参数0代表数组的第1个元素
      layerDrawable.setLayerInset(1, 4, 1, 4, 1); // 第一个参数1代表数组的第2个元素
      setBackgroundDrawable(layerDrawable);
    }
  }
  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);
    }
  }