下面列出了怎么用android.graphics.drawable.Animatable的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.ucrop_menu_activity, menu);
// Change crop & loader menu icons color to match the rest of the UI colors
MenuItem menuItemLoader = menu.findItem(R.id.menu_loader);
Drawable menuItemLoaderIcon = menuItemLoader.getIcon();
if (menuItemLoaderIcon != null) {
try {
menuItemLoaderIcon.mutate();
menuItemLoaderIcon.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP);
menuItemLoader.setIcon(menuItemLoaderIcon);
} catch (IllegalStateException e) {
Log.i(TAG, String.format("%s - %s", e.getMessage(), getString(R.string.ucrop_mutate_exception_hint)));
}
((Animatable) menuItemLoader.getIcon()).start();
}
MenuItem menuItemCrop = menu.findItem(R.id.menu_crop);
Drawable menuItemCropIcon = ContextCompat.getDrawable(this, mToolbarCropDrawable);
if (menuItemCropIcon != null) {
menuItemCropIcon.mutate();
menuItemCropIcon.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP);
menuItemCrop.setIcon(menuItemCropIcon);
}
return true;
}
/**
* Close the menu.
*/
public void close() {
updateDimensions(width, height);
state = State.CLOSED;
showIcons(false);
animator[LEFT].setFloatValues(0, button[LEFT]);
animator[RIGHT].setFloatValues(width, button[RIGHT]);
animator[RADIUS].setFloatValues(0, button[RADIUS]);
animator[TOP].setFloatValues(0, button[TOP]);
animator[BOTTOM].setFloatValues(height, button[BOTTOM]);
animatorSet.cancel();
animatorSet.start();
if (iconClosedDrawable instanceof Animatable) {
((Animatable) iconClosedDrawable).start();
}
this.animate()
.y(yPosition)
.setDuration(animationDuration)
.setInterpolator(DECELERATE_INTERPOLATOR)
.start();
}
public void startAnimation() {
Drawable drawable = getDrawable();
Animatable animatable = (Animatable) drawable;
AVDWrapper.Callback callback = new AVDWrapper.Callback() {
@Override
public void onAnimationDone() {
Log.d(LOG_TAG, "Enter animation finished");
mListener.onEnterAnimationFinished();
}
@Override
public void onAnimationStopped() {
}
};
AVDWrapper wrapper = new AVDWrapper(animatable, new Handler(), callback);
wrapper.start(getContext().getResources().getInteger(R.integer.enter_animation_duration));
}
@Override
public synchronized void onFinalImageSet(
String id, @Nullable INFO imageInfo, @Nullable Animatable animatable) {
final int numberOfListeners = mListeners.size();
for (int i = 0; i < numberOfListeners; ++i) {
try {
ControllerListener<? super INFO> listener = mListeners.get(i);
if (listener != null) {
listener.onFinalImageSet(id, imageInfo, animatable);
}
} catch (Exception exception) {
// Don't punish the other listeners if we're given a bad one.
onException("InternalListener exception in onFinalImageSet", exception);
}
}
}
public void toggleCheckBoxMenu(ListItemView listItemView, boolean toggle) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable drawable = listItemView.isChecked() ?
AnimatedVectorDrawableCompat
.create(mContext, R.drawable.avd_checkbox_checked_to_unchecked) :
AnimatedVectorDrawableCompat
.create(mContext, R.drawable.avd_checkbox_unchecked_to_checked);
ImageView imageView =
(ImageView) listItemView.findMenuItem(R.id.action_checkable).getActionView();
imageView.setImageDrawable(drawable);
((Animatable) drawable).start();
}
if (toggle) {
listItemView.toggle();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Drawable drawable = item.getIcon();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
switch (item.getItemId()) {
case R.id.action_cut:
return true;
case R.id.action_copy:
return true;
case R.id.action_share:
return true;
case R.id.action_delete:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Anim Items");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (menu != null) {
for (int i = 0; i < menu.size(); i++) {
Drawable drawable = menu.getItem(i).getIcon();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
}
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Drawable drawable = item.getIcon();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
switch (item.getItemId()) {
case R.id.action_cut:
return true;
case R.id.action_copy:
return true;
case R.id.action_share:
return true;
case R.id.action_delete:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setProgress(float progress) {
Drawable progressBarDrawable = mFadeDrawable.getDrawable(PROGRESS_BAR_IMAGE_INDEX);
if (progressBarDrawable == null) {
return;
}
// display progressbar when not fully loaded, hide otherwise
if (progress >= 0.999f) {
if (progressBarDrawable instanceof Animatable) {
((Animatable) progressBarDrawable).stop();
}
fadeOutLayer(PROGRESS_BAR_IMAGE_INDEX);
} else {
if (progressBarDrawable instanceof Animatable) {
((Animatable) progressBarDrawable).start();
}
fadeInLayer(PROGRESS_BAR_IMAGE_INDEX);
}
// set drawable level, scaled to [0, 10000] per drawable specification
progressBarDrawable.setLevel(Math.round(progress * 10000));
}
public void onAttach(@NonNull DraweeTextView view) {
mIsAttached = true;
if (mAttachedView != view) {
mActualDrawable.setCallback(null);
if (mAttachedView != null) {
throw new IllegalStateException("has been attached to view:" + mAttachedView);
}
mAttachedView = view;
setDrawableInner(mDrawable);
mActualDrawable.setCallback(mAttachedView);
}
mDeferredReleaser.cancelDeferredRelease(this);
if (!mIsRequestSubmitted) {
submitRequest();
} else if (mShouldShowAnim && mDrawable instanceof Animatable) {
((Animatable) mDrawable).start();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Drawable drawable = item.getIcon();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
switch (item.getItemId()) {
case R.id.action_cut:
return true;
case R.id.action_copy:
return true;
case R.id.action_share:
return true;
case R.id.action_delete:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_list_to_grid) {
if (!((Animatable) item.getIcon()).isRunning()) {
if (gridLayoutManager.getSpanCount() == 1) {
item.setIcon(AnimatedVectorDrawableCompat.create(MainActivity.this, R.drawable.avd_list_to_grid));
gridLayoutManager.setSpanCount(3);
} else {
item.setIcon(AnimatedVectorDrawableCompat.create(MainActivity.this, R.drawable.avd_grid_to_list));
gridLayoutManager.setSpanCount(1);
}
((Animatable) item.getIcon()).start();
simpleAdapter.notifyItemRangeChanged(0, simpleAdapter.getItemCount());
}
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 设置错误图片
*
* @param drawable 错误图片
*/
public void setErrorDrawable(Drawable drawable) {
if (mErrorDrawable == drawable)
return;
if (mErrorDrawable != null) {
if (mErrorDrawable instanceof Animatable)
((Animatable) mErrorDrawable).stop();
mErrorDrawable.setCallback(null);
}
mErrorDrawable = drawable;
if (mErrorDrawable != null) {
mErrorDrawable.setCallback(this);
if (mErrorDrawable instanceof Animatable) {
Animatable animatable = (Animatable) mErrorDrawable;
if (!animatable.isRunning())
animatable.start();
}
}
invalidate();
}
public void fabClicked(View v) {
if (v instanceof FloatingActionButton) {
FloatingActionButton fab = (FloatingActionButton) v;
Drawable drawable = fab.getDrawable();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent();
i.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
if (i.resolveActivity(getPackageManager()) != null) {
startActivity(i);
} else {
Toast.makeText(MainActivity.this, getString(R.string.error), Toast.LENGTH_SHORT).show();
}
}
}, (int) (500 * Util.getAnimatorSpeed(this)));
}
/**
* 设置开启图片
*
* @param drawable 开启图片
*/
public void setOpenDrawable(Drawable drawable) {
if (mOpenDrawable == drawable)
return;
if (mOpenDrawable != null) {
if (mOpenDrawable instanceof Animatable)
((Animatable) mOpenDrawable).stop();
mOpenDrawable.setCallback(null);
}
mOpenDrawable = drawable;
if (mOpenDrawable != null) {
mOpenDrawable.setCallback(this);
if (mOpenDrawable instanceof Animatable) {
Animatable animatable = (Animatable) mOpenDrawable;
if (!animatable.isRunning())
animatable.start();
}
}
invalidate();
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
GradleButterKnife.bind(this);
Uri uri = Uri.parse("asset:///splash.webp");
mSplashPresenter = new SplashPresenterImpl(this);
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(uri)
.setAutoPlayAnimations(true)
.setControllerListener(new BaseControllerListener<ImageInfo>(){
@Override
public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) {
super.onFinalImageSet(id, imageInfo, animatable);
mSplashPresenter.startCountDown(1);
}
})
.build();
splashDraweee.setController(controller);
}
private void changeAnimation(Operation operation) {
Drawable[] drawables = textView.getCompoundDrawables();
for (Drawable drawable : drawables) {
if (drawable != null && drawable instanceof Animatable) {
Animatable animatable = ((Animatable) drawable);
switch (operation) {
case START:
animatable.start();
break;
case STOP:
animatable.stop();
break;
}
}
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mPresenter.setView(this);
refresh(false);
SwipeRefreshLayout.OnRefreshListener refreshListener = new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refresh(true);
}
};
mRefreshLayout.setColorSchemeResources(R.color.colorAccent);
mRefreshLayout.setOnRefreshListener(refreshListener);
Drawable drawable = mProgressView.getDrawable();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
mAnalytics.trackViewedPrayerTimes();
}
/**
* Starts an animation provided by a <animation-drawable> on Lollipop &
* higher versions, in lower versions a simple set with a scale and a rotate
* animation is shown
*/
@Override
public void animateConfirmationView() {
Drawable drawable = mConfirmationView.getDrawable();
// Animated drawables are supported on Lollipop and higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (drawable instanceof Animatable)
((Animatable) drawable).start();
//
} else {
mConfirmationView.startAnimation(AnimationUtils.loadAnimation(this,
R.anim.appear_rotate));
}
}
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;
}
@Override public Object instantiateItem(ViewGroup viewGroup, int position) {
final PhotoDraweeView photoDraweeView = new PhotoDraweeView(viewGroup.getContext());
PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
controller.setUri(Uri.parse("res:///" + mDrawables[position]));
controller.setOldController(photoDraweeView.getController());
controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
@Override
public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
super.onFinalImageSet(id, imageInfo, animatable);
if (imageInfo == null) {
return;
}
photoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
}
});
photoDraweeView.setController(controller.build());
try {
viewGroup.addView(photoDraweeView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
} catch (Exception e) {
e.printStackTrace();
}
return photoDraweeView;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Anim Items");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (menu != null) {
for (int i = 0; i < menu.size(); i++) {
Drawable drawable = menu.getItem(i).getIcon();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
}
}
}
});
}
private void hideAddFolderView() {
Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.anim_close_folder_view);
mNewFolderView.startAnimation(anim);
mNewFolderView.setVisibility(View.INVISIBLE);
if (DiskUtil.isLollipopAndAbove()) {
mNewFolderImageView.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.drawable_close_to_plus));
// image button animation
Animatable animatable = (Animatable) mNewFolderImageView.getDrawable();
animatable.start();
}
mNewFolderImageView.setOnClickListener(mNewFolderButtonClickListener);
//listview should be clickable
SecondaryChooserAdapter.shouldEnable = true;
mInactiveGradient.startAnimation(anim);
mInactiveGradient.setVisibility(View.INVISIBLE);
// mNewFolderButton.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.plus));
}
@Override
public void stop() {
Drawable dr = getCurrent();
if (dr != null && dr instanceof Animatable) {
((Animatable) dr).stop();
}
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.ucrop_menu_activity, menu);
// Change crop & loader menu icons color to match the rest of the UI colors
MenuItem menuItemLoader = menu.findItem(R.id.menu_loader);
Drawable menuItemLoaderIcon = menuItemLoader.getIcon();
if (menuItemLoaderIcon != null) {
try {
menuItemLoaderIcon.mutate();
menuItemLoaderIcon.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP);
menuItemLoader.setIcon(menuItemLoaderIcon);
} catch (IllegalStateException e) {
Log.i(TAG, String.format("%s - %s", e.getMessage(), getString(R.string.ucrop_mutate_exception_hint)));
}
((Animatable) menuItemLoader.getIcon()).start();
}
MenuItem menuItemCrop = menu.findItem(R.id.menu_crop);
Drawable menuItemCropIcon = ContextCompat.getDrawable(this, mToolbarCropDrawable);
if (menuItemCropIcon != null) {
menuItemCropIcon.mutate();
menuItemCropIcon.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP);
menuItemCrop.setIcon(menuItemCropIcon);
}
return true;
}
@Override
public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
if (mEnableResizing
&& imageInfo != null
&& mDraweeSpan.getDraweeHolder().getTopLevelDrawable() != null) {
Drawable topLevelDrawable = mDraweeSpan.getDraweeHolder().getTopLevelDrawable();
Rect topLevelDrawableBounds = topLevelDrawable.getBounds();
if (mFixedHeight != UNSET_SIZE) {
float imageWidth = ((float) mFixedHeight / imageInfo.getHeight()) * imageInfo.getWidth();
int imageWidthPx = (int) imageWidth;
if (topLevelDrawableBounds.width() != imageWidthPx
|| topLevelDrawableBounds.height() != mFixedHeight) {
topLevelDrawable.setBounds(0, 0, imageWidthPx, mFixedHeight);
if (mDraweeSpanChangedListener != null) {
mDraweeSpanChangedListener.onDraweeSpanChanged(DraweeSpanStringBuilder.this);
}
}
} else if (topLevelDrawableBounds.width() != imageInfo.getWidth()
|| topLevelDrawableBounds.height() != imageInfo.getHeight()) {
topLevelDrawable.setBounds(0, 0, imageInfo.getWidth(), imageInfo.getHeight());
if (mDraweeSpanChangedListener != null) {
mDraweeSpanChangedListener.onDraweeSpanChanged(DraweeSpanStringBuilder.this);
}
}
}
}
void setOverflow(boolean animate) {
if (animate && mBackToOverflow != null) {
mButton.setImageDrawable(mBackToOverflow);
if (mBackToOverflow instanceof Animatable)
((Animatable) mBackToOverflow).start();
} else {
mButton.setImageDrawable(mOverflow);
}
mButton.setContentDescription(mOverflowContentDescription);
}
private void animatePullMe() {
if (!mPullMeBended) {
mPullMeBended = true;
mImageViewPullMe.setImageDrawable(getContext().getDrawable(R.drawable.pull_me_animated));
((Animatable) mImageViewPullMe.getDrawable()).start();
}
}
private void animatePullMeReverse() {
if (mPullMeBended) {
mPullMeBended = false;
mImageViewPullMe.setImageDrawable(getContext().getDrawable(R.drawable.pull_me_animated_reverse));
((Animatable) mImageViewPullMe.getDrawable()).start();
}
}
@Override
public void onFinalImageSet(
String id,
@Nullable Object imageInfo,
@Nullable Animatable animatable) {
ZoomableDraweeView.this.onFinalImageSet();
}