下面列出了怎么用android.graphics.drawable.AnimatedVectorDrawable的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_on_phone_animation);
AmbientModeSupport.attach(this);
mAnimationCallback =
new AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
super.onAnimationEnd(drawable);
// Go back to main Dialogs screen after animation.
finish();
}
};
// Play 'swipe left' animation only once.
ImageView phoneImage = findViewById(R.id.open_on_phone_animation_image);
mAnimatedVectorDrawablePhone = (AnimatedVectorDrawable) phoneImage.getDrawable();
mAnimatedVectorDrawablePhone.registerAnimationCallback(mAnimationCallback);
mAnimatedVectorDrawablePhone.start();
}
private void initialize() {
LayoutInflater inflater = LayoutInflater.from(getContext());
mBinding = DataBindingUtil.inflate(inflater, R.layout.webxr_interstitial, this, true);
mBinding.setLifecycleOwner((VRBrowserActivity)getContext());
setHowToVisible(true);
// AnimatedVectorDrawable doesn't work with a Hardware Accelerated canvas, we disable it for this view.
setIsHardwareAccelerationEnabled(false);
mSpinnerAnimation = (AnimatedVectorDrawable) mBinding.webxrSpinner.getDrawable();
if (DeviceType.isPicoVR()) {
ViewUtils.forceAnimationOnUI(mSpinnerAnimation);
}
mWidgetManager.addWebXRListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mFabView = (ImageView) findViewById(R.id.fab_view);
// animate head to leaf
final AnimatedVectorDrawable faceAVD = (AnimatedVectorDrawable) ContextCompat.getDrawable(this, R.drawable.face_avd);
mFabView.setImageDrawable(faceAVD);
findViewById(R.id.animate_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
faceAVD.start();
}
});
}
public void checkEmptyOrConnection() {
if (!Utils.hasInternet(getActivity())) {
if (getAdapter().getItemCount() == 0) {
mEmpty.setVisibility(View.GONE);
mEmptyImage.setVisibility(View.VISIBLE);
if (UI.isLollipop()) {
final AnimatedVectorDrawable avd =
(AnimatedVectorDrawable) ContextCompat.getDrawable(getActivity(), R.drawable.avd_no_connection);
mEmptyImage.setImageDrawable(avd);
avd.start();
} else {
mEmptyImage.setImageResource(R.drawable.no_connection);
}
} else {
Toast.makeText(getActivity(), R.string.check_network, Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fabSync = findViewById(R.id.fabSync);
FloatingActionButton fabTickCross = findViewById(R.id.fabTickCross);
tickToCross = (AnimatedVectorDrawable) getDrawable(R.drawable.avd_tick2cross);
crossToTick = (AnimatedVectorDrawable) getDrawable(R.drawable.avd_cross2tick);
ImageView imgSettings = findViewById(R.id.imgSettings);
ImageView imgJD = findViewById(R.id.imgJD);
imgSettings.setOnClickListener(this);
imgJD.setOnClickListener(this);
AnimatedVectorDrawableCompat animatedVectorDrawableCompat = AnimatedVectorDrawableCompat.create(this, R.drawable.avd_sync);
fabSync.setImageDrawable(animatedVectorDrawableCompat);
fabSync.setOnClickListener(this);
fabTickCross.setOnClickListener(this);
}
void revealPostingProgress() {
Animator reveal = ViewAnimationUtils.createCircularReveal(fabPosting,
(int) fabPosting.getPivotX(),
(int) fabPosting.getPivotY(),
0f,
fabPosting.getWidth() / 2)
.setDuration(600L);
reveal.setInterpolator(AnimUtils.getFastOutLinearInInterpolator(this));
reveal.start();
AnimatedVectorDrawable uploading =
(AnimatedVectorDrawable) getDrawable(R.drawable.avd_uploading);
if (uploading != null) {
fabPosting.setImageDrawable(uploading);
uploading.start();
}
}
@Override
protected void apply(ImageView view, TypedArray typedArray) {
boolean shouldAnimate = typedArray.getBoolean(R.styleable.SearchAnimateDecorator_decorAnimateSearch, false);
if(!shouldAnimate) return;
mIv = view;
mSearchToBar = (AnimatedVectorDrawable) view.getContext().getResources().getDrawable(R.drawable.anim_search_to_bar);
mBarToSearch = (AnimatedVectorDrawable) view.getContext().getResources().getDrawable(R.drawable.anim_bar_to_search);
mInterp = AnimationUtils.loadInterpolator(view.getContext(), android.R.interpolator.linear_out_slow_in);
mDuration = view.getContext().getResources().getInteger(R.integer.duration_bar);
// iv is sized to hold the search+bar so when only showing the search icon, translate the
// whole view left by half the difference to keep it centered
mOffset = -71f * (int) view.getContext().getResources().getDisplayMetrics().scaledDensity;
view.setTranslationX(mOffset);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animate(v);
}
});
}
/**
* Launch the animation on the currentAnimatedVectorDrawable
*/
private void launchAnimBackup(){
if(!backupRoundTripFirstLaunched) {
if (backupRoundTrip.getLevel() == 1) {
//then reverse
backupRoundTrip.setLevel(0);
} else {
//then reverse
backupRoundTrip.setLevel(1);
}
}else{
backupRoundTripFirstLaunched=false;
}
//find the current AnimatedVectorDrawable displayed
currentBackupDrawable = (AnimatedVectorDrawable) backupRoundTrip.getCurrent();
//start the animation
currentBackupDrawable.start();
}
/**
* Launch the animation on the currentAnimatedVectorDrawable
* As the levellist solution (ahead) has a bug, I do it in a simple way
*/
private void launchAnimBackup() {
//initialize
if (backupRoundTripFirstLaunched) {
reverse = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.animated_ic_check_box_black_24dp_reverse, getTheme());
initial = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.animated_ic_check_box_black_24dp, getTheme());
//if you want to avoid the color bug (animation not played on color at the second time)
//comment that line (ok it's memory consumption)
backupRoundTripFirstLaunched=false;
}
//set the drawable depending on the direction (reverse or not)
if (reverseState) {
//then reverse
imageView2.setImageDrawable(reverse);
} else {
//then reverse
imageView2.setImageDrawable(initial);
}
reverseState=!reverseState;
//launch the animation
((AnimatedVectorDrawable) imageView2.getDrawable()).start();
}
/**
* Launch the animation on the currentAnimatedVectorDrawable
*/
private void launchAnimBackup(){
if(!backupRoundTripFirstLaunched) {
if (backupRoundTrip.getLevel() == 1) {
//then reverse
backupRoundTrip.setLevel(0);
} else {
//then reverse
backupRoundTrip.setLevel(1);
}
}else{
backupRoundTripFirstLaunched=false;
}
//find the current AnimatedVectorDrawable displayed
currentBackupDrawable = (AnimatedVectorDrawable) backupRoundTrip.getCurrent();
//start the animation
currentBackupDrawable.start();
}
/**
* Launch the animation on the currentAnimatedVectorDrawable
*/
private void launchAnimBackup(){
if(!backupRoundTripFirstLaunched) {
if (backupRoundTrip.getLevel() == 1) {
//then reverse
backupRoundTrip.setLevel(0);
} else {
//then reverse
backupRoundTrip.setLevel(1);
}
}else{
backupRoundTripFirstLaunched=false;
}
//find the current AnimatedVectorDrawable displayed
currentBackupDrawable = (AnimatedVectorDrawable) backupRoundTrip.getCurrent();
//start the animation
currentBackupDrawable.start();
}
/**
* Launch the animation on the currentAnimatedVectorDrawable
* As the levellist solution (ahead) has a bug, I do it in a simple way
*/
private void launchAnimBackup() {
//initialize
if (backupRoundTripFirstLaunched) {
reverse = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.animated_ic_check_box_black_24dp_reverse, getTheme());
initial = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.animated_ic_check_box_black_24dp, getTheme());
//if you want to avoid the color bug (animation not played on color at the second time)
//comment that line (ok it's memory consumption)
backupRoundTripFirstLaunched=false;
}
//set the drawable depending on the direction (reverse or not)
if (reverseState) {
//then reverse
imageView2.setImageDrawable(reverse);
} else {
//then reverse
imageView2.setImageDrawable(initial);
}
reverseState=!reverseState;
//launch the animation
((AnimatedVectorDrawable) imageView2.getDrawable()).start();
}
/**
* Launch the animation on the currentAnimatedVectorDrawable
*/
private void launchAnimBackup(){
if(!backupRoundTripFirstLaunched) {
if (backupRoundTrip.getLevel() == 1) {
//then reverse
backupRoundTrip.setLevel(0);
} else {
//then reverse
backupRoundTrip.setLevel(1);
}
}else{
backupRoundTripFirstLaunched=false;
}
//find the current AnimatedVectorDrawable displayed
currentBackupDrawable = (AnimatedVectorDrawable) backupRoundTrip.getCurrent();
//start the animation
currentBackupDrawable.start();
}
public void changeRunStopIcon(final boolean run) {
runStopMenuButton.post(() -> {
if (android.os.Build.VERSION.SDK_INT >= 21) {
AnimatedVectorDrawable anim = (AnimatedVectorDrawable) getDrawable(run ? R.drawable.run_to_stop : R.drawable.stop_to_run);
runStopMenuButton.setImageDrawable(anim);
anim.start();
runStopMenuButtonAnimating = true;
runStopMenuButton.postDelayed(() -> {
supportInvalidateOptionsMenu();
runStopMenuButtonAnimating = false;
}, getResources().getInteger(R.integer.run_stop_animation_duration));
} else {
supportInvalidateOptionsMenu();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_images);
AmbientModeSupport.attach(this);
// Used to repeat animation from the beginning.
mAnimationCallback =
new AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
super.onAnimationEnd(drawable);
((AnimatedVectorDrawable) drawable).start();
}
};
// Play 'swipe left' animation on loop.
ImageView mSwipeLeftImage = findViewById(R.id.swipe_left_image);
mAnimatedVectorDrawableSwipe = (AnimatedVectorDrawable) mSwipeLeftImage.getDrawable();
mAnimatedVectorDrawableSwipe.start();
mAnimatedVectorDrawableSwipe.registerAnimationCallback(mAnimationCallback);
// Play 'tap' animation on loop.
ImageView mTapImage = findViewById(R.id.tap_image);
mAnimatedVectorDrawableTap = (AnimatedVectorDrawable) mTapImage.getDrawable();
mAnimatedVectorDrawableTap.start();
mAnimatedVectorDrawableTap.registerAnimationCallback(mAnimationCallback);
}
public void registerVectorDrawableAnimator(
AnimatedVectorDrawable.VectorDrawableAnimatorRT animatorSet) {
if (mOwningView == null || mOwningView.mAttachInfo == null) {
throw new IllegalStateException("Cannot start this animator on a detached view!");
}
mOwningView.mAttachInfo.mViewRootImpl.registerVectorDrawableAnimator(animatorSet);
}
@Override
public void run() {
final Drawable d = notificationsButton.getDrawable();
if (d instanceof AnimatedVectorDrawable) {
final AnimatedVectorDrawable animatedVectorDrawable = (AnimatedVectorDrawable) d;
animatedVectorDrawable.start();
final int minusSeconds = Math.min((int) (Math.max((notificationCount - NOTIFICATIONS_ALOT) * 0.5f, 0)), 7);
handler.postDelayed(this, (10 - minusSeconds) * D.SECOND);
}
}
public static void addListener(AnimatedVectorDrawable drawable, Object callback) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (callback instanceof Animatable2.AnimationCallback) {
drawable.registerAnimationCallback((Animatable2.AnimationCallback) callback);
} else {
throw new IllegalArgumentException("Callback parameter must implement " + Animatable2.AnimationCallback.class.getName());
}
}
}
public static boolean removeListener(AnimatedVectorDrawable drawable, Object callback) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (callback instanceof Animatable2.AnimationCallback) {
return drawable.unregisterAnimationCallback((Animatable2.AnimationCallback) callback);
} else {
throw new IllegalArgumentException("Callback parameter must implement " + Animatable2.AnimationCallback.class.getName());
}
}
return true;
}
public static void cancel(AnimatedVectorDrawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
drawable.reset();
}
sHandler.removeMessages(AnimatedVectorDrawableHandler.MSG_START);
sHandler.removeMessages(AnimatedVectorDrawableHandler.MSG_STOP);
}
@BindingAdapter("drawableEnd")
public static void setDrawableEnd(TextView textView, Drawable drawable) {
textView.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (drawable instanceof AnimatedVectorDrawable)
((AnimatedVectorDrawable) drawable).start();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.image_edit, menu);
MenuItem rotate = menu.findItem(R.id.rotate);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AnimatedVectorDrawable avd = (AnimatedVectorDrawable)
ContextCompat.getDrawable(this, R.drawable.ic_rotate_90_avd);
rotate.setIcon(avd);
}
return true;
}
public static void addListener(AnimatedVectorDrawable drawable, Object callback) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (callback instanceof Animatable2.AnimationCallback) {
drawable.registerAnimationCallback((Animatable2.AnimationCallback) callback);
} else {
throw new IllegalArgumentException("Callback parameter must implement " + Animatable2.AnimationCallback.class.getName());
}
}
}
public static boolean removeListener(AnimatedVectorDrawable drawable, Object callback) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (callback instanceof Animatable2.AnimationCallback) {
return drawable.unregisterAnimationCallback((Animatable2.AnimationCallback) callback);
} else {
throw new IllegalArgumentException("Callback parameter must implement " + Animatable2.AnimationCallback.class.getName());
}
}
return true;
}
public static void cancel(AnimatedVectorDrawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
drawable.reset();
}
sHandler.removeMessages(AnimatedVectorDrawableHandler.MSG_START);
sHandler.removeMessages(AnimatedVectorDrawableHandler.MSG_STOP);
}
@OnClick({R.id.shot_count, R.id.followers_count, R.id.likes_count})
void playerActionClick(TextView view) {
((AnimatedVectorDrawable) view.getCompoundDrawables()[1]).start();
switch (view.getId()) {
case R.id.followers_count:
PlayerSheet.start(PlayerActivity.this, player);
break;
}
}
private void showClose() {
if (dismissState == DISMISS_CLOSE) return;
dismissState = DISMISS_CLOSE;
final AnimatedVectorDrawable downToClose = (AnimatedVectorDrawable)
ContextCompat.getDrawable(this, R.drawable.avd_down_to_close);
close.setImageDrawable(downToClose);
downToClose.start();
}
private void showDown() {
if (dismissState == DISMISS_DOWN) return;
dismissState = DISMISS_DOWN;
final AnimatedVectorDrawable closeToDown = (AnimatedVectorDrawable)
ContextCompat.getDrawable(this, R.drawable.avd_close_to_down);
close.setImageDrawable(closeToDown);
closeToDown.start();
}
private void togglePlayToPause() {
controlToggle.displayQuick(pauseButton);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AnimatedVectorDrawable playToPauseDrawable = (AnimatedVectorDrawable)getContext().getDrawable(R.drawable.play_to_pause_animation);
pauseButton.setImageDrawable(playToPauseDrawable);
playToPauseDrawable.start();
}
}
private void togglePauseToPlay() {
controlToggle.displayQuick(playButton);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AnimatedVectorDrawable pauseToPlayDrawable = (AnimatedVectorDrawable)getContext().getDrawable(R.drawable.pause_to_play_animation);
playButton.setImageDrawable(pauseToPlayDrawable);
pauseToPlayDrawable.start();
}
}