下面列出了android.graphics.drawable.Animatable2#androidx.vectordrawable.graphics.drawable.VectorDrawableCompat 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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);
}
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);
}
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);
}
}
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;
}
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);
}
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;
}
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);
}
}
@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);
}
}
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;
}
@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;
}
}
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();
}
@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;
});
}
@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;
}
/**
* 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;
}
}
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);
}
}
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;
}
@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();
}
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);
}
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);
}
private static boolean isVectorDrawable(@NonNull Drawable d) {
return d instanceof VectorDrawableCompat
|| PLATFORM_VD_CLAZZ.equals(d.getClass().getName());
}
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);
}
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);
}