androidx.vectordrawable.graphics.drawable.VectorDrawableCompat Java Examples

The following examples show how to use androidx.vectordrawable.graphics.drawable.VectorDrawableCompat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SwirlView.java    From swirl with Apache License 2.0 6 votes vote down vote up
public void setState(State state, boolean animate) {
  if (state == this.state) return;

  @DrawableRes int resId = getDrawable(this.state, state, animate);
  if (resId == 0) {
    setImageDrawable(null);
  } else {
    Drawable icon = null;
    if (animate) {
      icon = AnimatedVectorDrawableCompat.create(getContext(), resId);
    }
    if (icon == null) {
      icon = VectorDrawableCompat.create(getResources(), resId, getContext().getTheme());
    }
    setImageDrawable(icon);

    if (icon instanceof Animatable) {
      ((Animatable) icon).start();
    }
  }

  this.state = state;
}
 
Example #2
Source File: ImageUtils.java    From Status with Apache License 2.0 6 votes vote down vote up
public static Drawable getVectorDrawable(Context context, int resId) {
    VectorDrawableCompat drawable;
    try {
        drawable = VectorDrawableCompat.create(context.getResources(), resId, context.getTheme());
    } catch (Exception e) {
        e.printStackTrace();
        return new ColorDrawable(Color.TRANSPARENT);
    }

    if (drawable != null) {
        Drawable icon = drawable.getCurrent();
        DrawableCompat.setTint(icon, Color.WHITE);
        return icon;
    } else {
        Log.wtf(context.getClass().getName(), "Can't get a vector drawable.");
        return new ColorDrawable(Color.TRANSPARENT);
    }
}
 
Example #3
Source File: PostItemsAdapter.java    From mimi-reader with Apache License 2.0 6 votes vote down vote up
private Pair<VectorDrawableCompat, VectorDrawableCompat> initMetadataDrawables() {
    final Resources.Theme theme = MimiApplication.getInstance().getTheme();
    final Resources res = MimiApplication.getInstance().getResources();
    final int drawableColor = MimiUtil.getInstance().getTheme() == MimiUtil.THEME_LIGHT ? R.color.md_grey_800 : R.color.md_green_50;
    final VectorDrawableCompat pin;
    final VectorDrawableCompat lock;

    pin = VectorDrawableCompat.create(res, R.drawable.ic_pin, theme);
    lock = VectorDrawableCompat.create(res, R.drawable.ic_lock, theme);

    if (pin != null) {
        pin.setTint(res.getColor(drawableColor));
    }

    if (lock != null) {
        lock.setTint(res.getColor(drawableColor));
    }

    return Pair.create(pin, lock);
}
 
Example #4
Source File: PostItemsAdapter.java    From mimi-reader with Apache License 2.0 6 votes vote down vote up
public PostItemsAdapter(final String boardName, final String boardTitle, final List<ChanPost> postList, final OnPostItemClickListener clickListener) {
    this.boardName = boardName;
    this.boardTitle = boardTitle;
    this.postList.addAll(postList);
    this.postCount = postList.size();
    this.clickListener = clickListener;

    setup();

    Pair<VectorDrawableCompat, VectorDrawableCompat> drawables = initMetadataDrawables();
    pinDrawable = drawables.first;
    lockDrawable = drawables.second;

    final Context appContext = MimiApplication.getInstance().getApplicationContext();

    oneReply = appContext.getString(R.string.one_reply);
    numberReplies = appContext.getString(R.string.number_replies);

    oneImage = appContext.getString(R.string.one_image);
    numberImages = appContext.getString(R.string.number_images);

    imageSpoilersEnabled = MimiPrefs.imageSpoilersEnabled(appContext);
    thumbUrl = MimiUtil.https() + appContext.getString(R.string.thumb_link) + appContext.getString(R.string.thumb_path);
    spoilerUrl = MimiUtil.https() + appContext.getString(R.string.spoiler_link) + appContext.getString(R.string.spoiler_path);
    customSpoilerUrl = MimiUtil.https() + appContext.getString(R.string.spoiler_link) + appContext.getString(R.string.custom_spoiler_path);
}
 
Example #5
Source File: ThreadListAdapter.java    From mimi-reader with Apache License 2.0 6 votes vote down vote up
private Pair<VectorDrawableCompat, VectorDrawableCompat> initMetadataDrawables() {
    final Resources.Theme theme = MimiApplication.getInstance().getTheme();
    final Resources res = MimiApplication.getInstance().getResources();
    final int drawableColor = MimiUtil.getInstance().getTheme() == MimiUtil.THEME_LIGHT ? R.color.md_grey_800 : R.color.md_green_50;
    final VectorDrawableCompat pin;
    final VectorDrawableCompat lock;

    pin = VectorDrawableCompat.create(res, R.drawable.ic_pin, theme);
    lock = VectorDrawableCompat.create(res, R.drawable.ic_lock, theme);

    if (pin != null) {
        pin.setTint(res.getColor(drawableColor));
    }

    if (lock != null) {
        lock.setTint(res.getColor(drawableColor));
    }

    return Pair.create(pin, lock);
}
 
Example #6
Source File: ThreadListAdapter.java    From mimi-reader with Apache License 2.0 5 votes vote down vote up
public ThreadListAdapter(@NonNull final ChanThread thread) {
    this.items.addAll(thread.getPosts());
    this.boardName = thread.getBoardName();
    this.threadId = thread.getThreadId();
    final Context context = MimiApplication.getInstance().getApplicationContext();

    this.flagUrl = MimiUtil.https() + context.getString(R.string.flag_int_link);
    this.trollUrl = MimiUtil.https() + context.getString(R.string.flag_pol_link);

    if (MimiUtil.getInstance().getTheme() == MimiUtil.THEME_LIGHT) {
        defaultPostBackground = R.color.row_item_background_light;
        highlightPostBackground = R.color.post_highlight_light;
        highlightTextBackground = ResourcesCompat.getColor(context.getResources(), R.color.text_highlight_background_light, context.getTheme());
        selectedTextBackground = ResourcesCompat.getColor(context.getResources(), R.color.text_select_background_light, context.getTheme());
    } else if (MimiUtil.getInstance().getTheme() == MimiUtil.THEME_DARK) {
        defaultPostBackground = R.color.row_item_background_dark;
        highlightPostBackground = R.color.post_highlight_dark;
        highlightTextBackground = ResourcesCompat.getColor(context.getResources(), R.color.text_highlight_background_dark, context.getTheme());
        selectedTextBackground = ResourcesCompat.getColor(context.getResources(), R.color.text_select_background_dark, context.getTheme());
    } else {
        defaultPostBackground = R.color.row_item_background_black;
        highlightPostBackground = R.color.post_highlight_black;
        highlightTextBackground = ResourcesCompat.getColor(context.getResources(), R.color.text_highlight_background_black, context.getTheme());
        selectedTextBackground = ResourcesCompat.getColor(context.getResources(), R.color.text_select_background_black, context.getTheme());
    }

    Pair<VectorDrawableCompat, VectorDrawableCompat> metadataDrawables = initMetadataDrawables();
    pinDrawable = metadataDrawables.first;
    lockDrawable = metadataDrawables.second;

    Context appContext = MimiApplication.getInstance().getApplicationContext();
    imageSpoilersEnabled = MimiPrefs.imageSpoilersEnabled(appContext);
    spoilerUrl = MimiUtil.https() + appContext.getString(R.string.spoiler_link) + appContext.getString(R.string.spoiler_path);
    customSpoilerUrl = MimiUtil.https() + appContext.getString(R.string.spoiler_link) + appContext.getString(R.string.custom_spoiler_path);

    setupThread();

}
 
Example #7
Source File: CheckInDrawable.java    From cathode with Apache License 2.0 5 votes vote down vote up
public CheckInDrawable(Context context, int animatedCheckInDrawableRes,
    int animatedCancelDrawableRes, int checkInDrawableRes, int cancelDrawableRes) {
  this.context = context;

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    checkInDrawable = context.getDrawable(animatedCheckInDrawableRes);
    cancelDrawable = context.getDrawable(animatedCancelDrawableRes);

    callbacks = new Animatable2.AnimationCallback() {
      @RequiresApi(api = Build.VERSION_CODES.M) @Override
      public void onAnimationEnd(Drawable drawable) {
        ((AnimatedVectorDrawable) checkInDrawable).clearAnimationCallbacks();
        ((AnimatedVectorDrawable) checkInDrawable).reset();
        ((AnimatedVectorDrawable) cancelDrawable).clearAnimationCallbacks();
        ((AnimatedVectorDrawable) cancelDrawable).reset();

        updateWatchingDrawable(watching);
      }
    };
  } else {
    checkInDrawable =
        VectorDrawableCompat.create(context.getResources(), checkInDrawableRes, null);
    cancelDrawable = VectorDrawableCompat.create(context.getResources(), cancelDrawableRes, null);
  }

  checkInDrawable.setCallback(this);
  cancelDrawable.setCallback(this);

  currentDrawable = checkInDrawable;
}
 
Example #8
Source File: DrawableManager.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static Drawable getVectorDrawable(@NonNull Resources res,
        @DrawableRes int resId, @Nullable Resources.Theme theme) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return res.getDrawable(resId, theme);
    } else {
        return VectorDrawableCompat.create(res, resId, theme);
    }
}
 
Example #9
Source File: IconStyleData.java    From Status with Apache License 2.0 5 votes vote down vote up
/**
 * Get a drawable of the style at a particular index.
 *
 * @param context               The current application context.
 * @param value                 The index to obtain.
 * @return A created Drawable, or null if
 *                              something has gone horribly wrong.
 */
@Nullable
public Drawable getDrawable(Context context, int value) {
    if (value < 0 || value >= getSize()) return null;
    switch (type) {
        case TYPE_VECTOR:
            return VectorDrawableCompat.create(context.getResources(), resource[value], context.getTheme());
        case TYPE_IMAGE:
            return ContextCompat.getDrawable(context, resource[value]);
        case TYPE_FILE:
            String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (!StaticUtils.isPermissionsGranted(context, permissions)) {
                if (context instanceof Activity)
                    StaticUtils.requestPermissions((Activity) context, permissions);

                return null;
            } else {
                try {
                    return Drawable.createFromPath(path[value]);
                } catch (OutOfMemoryError e) {
                    e.printStackTrace();
                    return null;
                }
            }
        default:
            return null;
    }
}
 
Example #10
Source File: BottomSheetSpinnerAdapter.java    From TwistyTimer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.item_bottom_spinner, parent, false);

    TextView titleView = view.findViewById(R.id.item);
    Drawable icon;

    titleView.setText(mTitles[position]);

    if (iconResLenght > 0) {
        if (mIconRes[position] != 0) {
            try {
                icon = VectorDrawableCompat.create(mContext.getResources(), mIconRes[position], null);
                titleView.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
            } catch (Exception e) {
                Log.e("BottomSheetSpinner", "Error populating list!: " + e);
            }
        }
    } else {
        icon = VectorDrawableCompat.create(mContext.getResources(), R.drawable.ic_label, null);
        if (icon != null)
            icon.setAlpha(90);
        titleView.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
    }

    return view;
}
 
Example #11
Source File: BottomSheetSpinnerDialog.java    From TwistyTimer with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("ClickableViewAccessibility")
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    listView.setAdapter(mAdapter);
    listView.setOnItemClickListener(mClickListener);

    titleTextView.setText(titleText);

    if (titleIcon != 0) {
        Drawable icon = VectorDrawableCompat.create(mContext.getResources(), titleIcon, null);
        titleTextView.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null);
    }

    listView.setOnTouchListener((v, event) -> {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }

        v.onTouchEvent(event);
        return true;
    });
}
 
Example #12
Source File: SkinCompatDrawableManager.java    From Android-skin-support with MIT License 5 votes vote down vote up
@SuppressLint("NewApi")
@Override
public Drawable createFromXmlInner(@NonNull Context context, @NonNull XmlPullParser parser,
                                   @NonNull AttributeSet attrs, @Nullable Resources.Theme theme) {
    try {
        return VectorDrawableCompat
                .createFromXmlInner(context.getResources(), parser, attrs, theme);
    } catch (Exception e) {
        Log.e("VdcInflateDelegate", "Exception while inflating <vector>", e);
        return null;
    }
}
 
Example #13
Source File: ImageUtils.java    From Asteroid with Apache License 2.0 5 votes vote down vote up
public static Bitmap getVectorBitmap(Context context, @DrawableRes int id) {
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), id, context.getTheme());
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        drawable = (DrawableCompat.wrap(drawable)).mutate();

    Bitmap result = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return result;
}
 
Example #14
Source File: ReceiveDialogFragment.java    From natrium-android-wallet with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    // Set QR bg for compat
    if (getResources() != null && getContext() != null && binding != null && binding.receiveOuter != null) {
        Drawable qrBackground = VectorDrawableCompat.create(getResources(), R.drawable.qr_border, getContext().getTheme());
        binding.receiveOuter.setBackground(qrBackground);
    }
}
 
Example #15
Source File: DrawableManager.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
public static Drawable getVectorDrawable(@NonNull Resources res,
        @DrawableRes int resId, @Nullable Resources.Theme theme) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return res.getDrawable(resId, theme);
    } else {
        return VectorDrawableCompat.create(res, resId, theme);
    }
}
 
Example #16
Source File: SegmentedButton.java    From SegmentedButton with Apache License 2.0 5 votes vote down vote up
private Drawable readCompatDrawable(Context context, int drawableResId)
{
    Drawable drawable = AppCompatResources.getDrawable(context, drawableResId);

    // API 28 has a bug with vector drawables where the selected tint color is always applied to the drawable
    // To prevent this, the vector drawable is converted to a bitmap
    if ((VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable)
        || drawable instanceof VectorDrawableCompat)
    {
        Bitmap bitmap = getBitmapFromVectorDrawable(drawable);
        return new BitmapDrawable(context.getResources(), bitmap);
    }
    else
        return drawable;
}
 
Example #17
Source File: VideoThumbnailsRangeSelectorView.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private void init(final @Nullable AttributeSet attrs) {
  if (attrs != null) {
    TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.VideoThumbnailsRangeSelectorView, 0, 0);
    try {
      thumbSizePixels          = typedArray.getDimensionPixelSize(R.styleable.VideoThumbnailsRangeSelectorView_thumbWidth, 1);
      cursorPixels             = typedArray.getDimensionPixelSize(R.styleable.VideoThumbnailsRangeSelectorView_cursorWidth, 1);
      thumbColor               = typedArray.getColor(R.styleable.VideoThumbnailsRangeSelectorView_thumbColor, 0xffff0000);
      thumbColorEdited         = typedArray.getColor(R.styleable.VideoThumbnailsRangeSelectorView_thumbColorEdited, thumbColor);
      cursorColor              = typedArray.getColor(R.styleable.VideoThumbnailsRangeSelectorView_cursorColor, thumbColor);
      thumbTouchRadius         = typedArray.getDimensionPixelSize(R.styleable.VideoThumbnailsRangeSelectorView_thumbTouchRadius, 50);
      thumbHintTextSize        = typedArray.getDimensionPixelSize(R.styleable.VideoThumbnailsRangeSelectorView_thumbHintTextSize, 0);
      thumbHintTextColor       = typedArray.getColor(R.styleable.VideoThumbnailsRangeSelectorView_thumbHintTextColor, 0xffff0000);
      thumbHintBackgroundColor = typedArray.getColor(R.styleable.VideoThumbnailsRangeSelectorView_thumbHintBackgroundColor, 0xff00ff00);
    } finally {
      typedArray.recycle();
    }
  }

  chevronLeft  = VectorDrawableCompat.create(getResources(), R.drawable.ic_chevron_left_black_8dp, null);
  chevronRight = VectorDrawableCompat.create(getResources(), R.drawable.ic_chevron_right_black_8dp, null);

  paintGrey.setColor(0x7f000000);
  paintGrey.setStyle(Paint.Style.FILL_AND_STROKE);
  paintGrey.setStrokeWidth(1);

  paint.setStrokeWidth(2);

  thumbTimeTextPaint.setTextSize(thumbHintTextSize);
  thumbTimeTextPaint.setColor(thumbHintTextColor);

  thumbTimeBackgroundPaint.setStyle(Paint.Style.FILL_AND_STROKE);
  thumbTimeBackgroundPaint.setColor(thumbHintBackgroundColor);
}
 
Example #18
Source File: AddGroupDetailsFragment.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  create  = view.findViewById(R.id.create);
  name    = view.findViewById(R.id.group_name);
  toolbar = view.findViewById(R.id.toolbar);

  setCreateEnabled(false, false);

  GroupMemberListView members    = view.findViewById(R.id.member_list);
  ImageView           avatar     = view.findViewById(R.id.group_avatar);
  View                mmsWarning = view.findViewById(R.id.mms_warning);

  avatarPlaceholder = VectorDrawableCompat.create(getResources(), R.drawable.ic_camera_outline_32_ultramarine, requireActivity().getTheme());

  if (savedInstanceState == null) {
    avatar.setImageDrawable(new InsetDrawable(avatarPlaceholder, ViewUtil.dpToPx(AVATAR_PLACEHOLDER_INSET_DP)));
  }

  initializeViewModel();

  avatar.setOnClickListener(v -> showAvatarSelectionBottomSheet());
  members.setRecipientClickListener(this::handleRecipientClick);
  name.addTextChangedListener(new AfterTextChanged(editable -> viewModel.setName(editable.toString())));
  toolbar.setNavigationOnClickListener(unused -> callback.onNavigationButtonPressed());
  create.setOnClickListener(v -> handleCreateClicked());
  viewModel.getMembers().observe(getViewLifecycleOwner(), members::setMembers);
  viewModel.getCanSubmitForm().observe(getViewLifecycleOwner(), isFormValid -> setCreateEnabled(isFormValid, true));
  viewModel.getIsMms().observe(getViewLifecycleOwner(), isMms -> {
    mmsWarning.setVisibility(isMms ? View.VISIBLE : View.GONE);
    name.setVisibility(isMms ? View.GONE : View.VISIBLE);
    avatar.setVisibility(isMms ? View.GONE : View.VISIBLE);
    toolbar.setTitle(isMms ? R.string.AddGroupDetailsFragment__create_group : R.string.AddGroupDetailsFragment__name_this_group);
  });
  viewModel.getAvatar().observe(getViewLifecycleOwner(), avatarBytes -> {
    if (avatarBytes == null) {
      avatar.setImageDrawable(new InsetDrawable(avatarPlaceholder, ViewUtil.dpToPx(AVATAR_PLACEHOLDER_INSET_DP)));
    } else {
      GlideApp.with(this)
              .load(avatarBytes)
              .circleCrop()
              .skipMemoryCache(true)
              .diskCacheStrategy(DiskCacheStrategy.NONE)
              .into(avatar);
    }
  });

  name.requestFocus();
}
 
Example #19
Source File: ImageUtil.java    From VinylMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
public static Drawable getVectorDrawable(@NonNull Resources res, @DrawableRes int resId, @Nullable Resources.Theme theme) {
    if (Build.VERSION.SDK_INT >= 21) {
        return res.getDrawable(resId, theme);
    }
    return VectorDrawableCompat.create(res, resId, theme);
}
 
Example #20
Source File: SkinCompatDrawableManager.java    From Android-skin-support with MIT License 4 votes vote down vote up
private static boolean isVectorDrawable(@NonNull Drawable d) {
    return d instanceof VectorDrawableCompat
            || PLATFORM_VD_CLAZZ.equals(d.getClass().getName());
}
 
Example #21
Source File: ImageUtil.java    From MusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
public static Drawable getVectorDrawable(@NonNull Resources res, @DrawableRes int resId, @Nullable Resources.Theme theme) {
    if (Build.VERSION.SDK_INT >= 21) {
        return res.getDrawable(resId, theme);
    }
    return VectorDrawableCompat.create(res, resId, theme);
}
 
Example #22
Source File: ImageUtil.java    From Music-Player with GNU General Public License v3.0 4 votes vote down vote up
public static Drawable getVectorDrawable(@NonNull Resources res, @DrawableRes int resId, @Nullable Resources.Theme theme) {
    if (Build.VERSION.SDK_INT >= 21) {
        return res.getDrawable(resId, theme);
    }
    return VectorDrawableCompat.create(res, resId, theme);
}
 
Example #23
Source File: ImageUtil.java    From Phonograph with GNU General Public License v3.0 4 votes vote down vote up
public static Drawable getVectorDrawable(@NonNull Resources res, @DrawableRes int resId, @Nullable Resources.Theme theme) {
    if (Build.VERSION.SDK_INT >= 21) {
        return res.getDrawable(resId, theme);
    }
    return VectorDrawableCompat.create(res, resId, theme);
}