private void initAttrs(AttributeSet attrs, int defStyle) {
    TypedArray a =
        getContext()
            .getTheme()
            .obtainStyledAttributes(attrs, R.styleable.ColorPreference, defStyle, defStyle);

    try {
      mItemLayoutId = a.getResourceId(R.styleable.ColorPreference_itemLayout, mItemLayoutId);
      mNumColumns = a.getInteger(R.styleable.ColorPreference_numColumns, mNumColumns);
      int choicesResId =
          a.getResourceId(R.styleable.ColorPreference_choices, R.array.default_color_choice_values);
      if (choicesResId > 0) {
        String[] choices = a.getResources().getStringArray(choicesResId);
        mColorChoices = new int[choices.length];
        for (int i = 0; i < choices.length; i++) {
          mColorChoices[i] = Color.parseColor(choices[i]);
        }
      }

    } finally {
      a.recycle();
    }

    setWidgetLayoutResource(mItemLayoutId);
  }
Exemplo n.º 2
0
  private void initAttrs(AttributeSet attrs, int defStyle) {
    TypedArray a =
        getContext()
            .getTheme()
            .obtainStyledAttributes(attrs, R.styleable.ColorPickerPreference, defStyle, defStyle);

    try {
      mItemLayoutId =
          a.getResourceId(R.styleable.ColorPickerPreference_cal_itemLayout, mItemLayoutId);
      mNumColumns = a.getInteger(R.styleable.ColorPickerPreference_cal_numColumns, mNumColumns);
      int choicesResId =
          a.getResourceId(
              R.styleable.ColorPickerPreference_cal_choices, R.array.default_color_choice_values);
      if (choicesResId > 0) {
        mColorChoices = a.getResources().getIntArray(choicesResId);
      }

    } finally {
      a.recycle();
    }
    setWidgetLayoutResource(mItemLayoutId);
  }
  /** Updates the constant state from the values in the typed array. */
  private void updateStateFromTypedArray(TypedArray a) throws XmlPullParserException {
    final Resources r = a.getResources();
    final NinePatchState state = mNinePatchState;

    // Account for any configuration changes.
    state.mChangingConfigurations |= a.getChangingConfigurations();

    // Extract the theme attributes, if any.
    state.mThemeAttrs = a.extractThemeAttrs();

    state.mDither = a.getBoolean(R.styleable.NinePatchDrawable_dither, state.mDither);

    final int srcResId = a.getResourceId(R.styleable.NinePatchDrawable_src, 0);
    if (srcResId != 0) {
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inDither = !state.mDither;
      options.inScreenDensity = r.getDisplayMetrics().noncompatDensityDpi;

      final Rect padding = new Rect();
      final Rect opticalInsets = new Rect();
      Bitmap bitmap = null;

      try {
        final TypedValue value = new TypedValue();
        final InputStream is = r.openRawResource(srcResId, value);

        bitmap = BitmapFactory.decodeResourceStream(r, value, is, padding, options);

        is.close();
      } catch (IOException e) {
        // Ignore
      }

      if (bitmap == null) {
        throw new XmlPullParserException(
            a.getPositionDescription() + ": <nine-patch> requires a valid src attribute");
      } else if (bitmap.getNinePatchChunk() == null) {
        throw new XmlPullParserException(
            a.getPositionDescription() + ": <nine-patch> requires a valid 9-patch source image");
      }

      bitmap.getOpticalInsets(opticalInsets);

      state.mNinePatch = new NinePatch(bitmap, bitmap.getNinePatchChunk());
      state.mPadding = padding;
      state.mOpticalInsets = Insets.of(opticalInsets);
    }

    state.mAutoMirrored =
        a.getBoolean(R.styleable.NinePatchDrawable_autoMirrored, state.mAutoMirrored);
    state.mBaseAlpha = a.getFloat(R.styleable.NinePatchDrawable_alpha, state.mBaseAlpha);

    final int tintMode = a.getInt(R.styleable.NinePatchDrawable_tintMode, -1);
    if (tintMode != -1) {
      state.mTintMode = Drawable.parseTintMode(tintMode, Mode.SRC_IN);
    }

    final ColorStateList tint = a.getColorStateList(R.styleable.NinePatchDrawable_tint);
    if (tint != null) {
      state.mTint = tint;
    }

    // Update local properties.
    initializeWithState(state, r);

    // Push density applied by setNinePatchState into state.
    state.mTargetDensity = mTargetDensity;
  }