/**
  * Create a layered drawable
  *
  * @param back - Background image to use when target is active
  * @param front - Front image to use for target
  * @param inset - Target inset padding
  * @param frontBlank - Whether the front image for active target should be blank
  * @return StateListDrawable
  */
 private StateListDrawable getLayeredDrawable(
     Drawable back, Drawable front, int inset, boolean frontBlank) {
   front.mutate();
   back.mutate();
   InsetDrawable[] inactivelayer = new InsetDrawable[2];
   InsetDrawable[] activelayer = new InsetDrawable[2];
   Drawable activeFront = frontBlank ? mResources.getDrawable(android.R.color.transparent) : front;
   Drawable inactiveBack =
       mResources.getDrawable(com.android.internal.R.drawable.ic_lockscreen_lock_pressed);
   inactivelayer[0] = new InsetDrawable(inactiveBack, 0, 0, 0, 0);
   inactivelayer[1] = new InsetDrawable(front, inset, inset, inset, inset);
   activelayer[0] = new InsetDrawable(back, 0, 0, 0, 0);
   activelayer[1] = new InsetDrawable(activeFront, inset, inset, inset, inset);
   StateListDrawable states = new StateListDrawable();
   LayerDrawable inactiveLayerDrawable = new LayerDrawable(inactivelayer);
   inactiveLayerDrawable.setId(0, 0);
   inactiveLayerDrawable.setId(1, 1);
   LayerDrawable activeLayerDrawable = new LayerDrawable(activelayer);
   activeLayerDrawable.setId(0, 0);
   activeLayerDrawable.setId(1, 1);
   states.addState(TargetDrawable.STATE_INACTIVE, inactiveLayerDrawable);
   states.addState(TargetDrawable.STATE_ACTIVE, activeLayerDrawable);
   states.addState(TargetDrawable.STATE_FOCUSED, activeLayerDrawable);
   return states;
 }
  /**
   * Converts a drawable to a tiled version of itself. It will recursively traverse layer and state
   * list drawables.
   */
  private Drawable tileify(Drawable drawable, boolean clip) {

    if (drawable instanceof LayerDrawable) {
      LayerDrawable background = (LayerDrawable) drawable;
      final int N = background.getNumberOfLayers();
      Drawable[] outDrawables = new Drawable[N];

      for (int i = 0; i < N; i++) {
        int id = background.getId(i);
        outDrawables[i] =
            tileify(
                background.getDrawable(i), (id == R.id.progress || id == R.id.secondaryProgress));
      }

      LayerDrawable newBg = new LayerDrawable(outDrawables);

      for (int i = 0; i < N; i++) {
        newBg.setId(i, background.getId(i));
      }

      return newBg;

    } else if (drawable instanceof StateListDrawable) {
      StateListDrawable in = (StateListDrawable) drawable;
      StateListDrawable out = new StateListDrawable();
      int numStates = in.getStateCount();
      for (int i = 0; i < numStates; i++) {
        out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
      }
      return out;

    } else if (drawable instanceof BitmapDrawable) {
      final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
      if (mSampleTile == null) {
        mSampleTile = tileBitmap;
      }

      final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());

      final BitmapShader bitmapShader =
          new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
      shapeDrawable.getPaint().setShader(bitmapShader);

      return (clip)
          ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL)
          : shapeDrawable;
    }

    return drawable;
  }
示例#3
0
 /**
  * ÆÀ¼ÛÐÇÌõ״̬ͼƬ¸Ä±ä£¬µ±Á½¿ÅÐÇÒÔÉÏÊÇÏÔʾ»ÆÉ«ÐÇÐÇ£¬ µ±Á½¿ÅÐÇÐÇÒ»ÏÂÏÔʾ»ÒÉ«ÐÇÐÇ
  *
  * @param Bitmap [] images ÐÇÌõ״̬µÄͼƬ×é
  */
 private Drawable buildRatingBarDrawables(Bitmap[] images) {
   final int[] requiredIds = {
     android.R.id.background, android.R.id.secondaryProgress, android.R.id.progress
   };
   final float[] roundedCorners = new float[] {5, 5, 5, 5, 5, 5, 5, 5};
   Drawable[] pieces = new Drawable[3];
   for (int i = 0; i < 3; i++) {
     ShapeDrawable sd = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
     BitmapShader bitmapShader =
         new BitmapShader(images[i], Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
     sd.getPaint().setShader(bitmapShader);
     ClipDrawable cd = new ClipDrawable(sd, Gravity.LEFT, ClipDrawable.HORIZONTAL);
     if (i == 0) {
       pieces[i] = sd;
     } else {
       pieces[i] = cd;
     }
   }
   LayerDrawable ld = new LayerDrawable(pieces);
   for (int i = 0; i < 3; i++) {
     ld.setId(i, requiredIds[i]);
   }
   return ld;
 }