下面列出了android.animation.AnimatorSet#setTarget ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* use property animation by xml;
*
* @return
*/
private Animator getAnimationByXml() {
final int height = mPuppet.getLayoutParams().height;
final int width = mPuppet.getLayoutParams().width;
AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.animatorset);
//ValueAnimator usage:add AnimatorUpdateListener;
ArrayList<Animator> childAnimations = animatorSet.getChildAnimations();
((ValueAnimator) childAnimations.get(childAnimations.size() - 1))
.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float animatedValue = (float) valueAnimator.getAnimatedValue();
mPuppet.getLayoutParams().height = (int) (height * animatedValue);
mPuppet.getLayoutParams().width = (int) (width * animatedValue);
mPuppet.requestLayout();
}
});
animatorSet.setTarget(mPuppet);
return animatorSet;
}
@Override
public void onClick(View view) {
AnimatorSet cardLeftIn = (AnimatorSet) AnimatorInflater.loadAnimator(AppMain.getContext(),
R.animator.flip_left_in);
if (!mIsBackVisible) {
mCardFrontLayout.setVisibility(View.INVISIBLE);
mCardBackLayout.setVisibility(View.VISIBLE);
cardLeftIn.setTarget(mCardBackLayout);
cardLeftIn.start();
mIsBackVisible = true;
} else {
mCardBackLayout.setVisibility(View.INVISIBLE);
mCardFrontLayout.setVisibility(View.VISIBLE);
cardLeftIn.setTarget(mCardFrontLayout);
cardLeftIn.start();
mIsBackVisible = false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
searchEngine = new SearchEngine(getApplicationContext());
setContentView(R.layout.activity_live_object);
preview = findViewById(R.id.camera_preview);
graphicOverlay = findViewById(R.id.camera_preview_graphic_overlay);
graphicOverlay.setOnClickListener(this);
cameraSource = new CameraSource(graphicOverlay);
promptChip = findViewById(R.id.bottom_prompt_chip);
promptChipAnimator =
(AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.bottom_prompt_chip_enter);
promptChipAnimator.setTarget(promptChip);
searchButton = findViewById(R.id.product_search_button);
searchButton.setOnClickListener(this);
searchButtonAnimator =
(AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.search_button_enter);
searchButtonAnimator.setTarget(searchButton);
searchProgressBar = findViewById(R.id.search_progress_bar);
setUpBottomSheet();
findViewById(R.id.close_button).setOnClickListener(this);
flashButton = findViewById(R.id.flash_button);
flashButton.setOnClickListener(this);
settingsButton = findViewById(R.id.settings_button);
settingsButton.setOnClickListener(this);
setUpWorkflowModel();
}
public void onAnimatorSetTest(View view) {
// 获取屏幕宽度
int maxWidth = getWindowManager().getDefaultDisplay().getWidth();
ViewWrapper wrapper = new ViewWrapper(view, maxWidth);
AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.animator_set);
animatorSet.setTarget(wrapper);
animatorSet.start();
}
private Animator getAnimator(LeafHolder target, RectF leafFlyRect, float progress) {
ValueAnimator bezierValueAnimator = getBezierValueAnimator(target, leafFlyRect, progress);
AnimatorSet finalSet = new AnimatorSet();
finalSet.playSequentially(bezierValueAnimator);
finalSet.setInterpolator(INTERPOLATORS[mRandom.nextInt(INTERPOLATORS.length)]);
finalSet.setTarget(target);
return finalSet;
}
/**
* 生成进入动画
* @param target 动画作用 View
* @return 动画集合
*/
private AnimatorSet generateEnterAnimation(View target) {
ObjectAnimator alpha = ObjectAnimator.ofFloat(target, "alpha", 0.2f, 1f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(target, "scaleX", 0.5f, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(target, "scaleY", 0.5f, 1f);
AnimatorSet enterAnimation = new AnimatorSet();
enterAnimation.playTogether(alpha, scaleX, scaleY);
enterAnimation.setDuration(10);
enterAnimation.setTarget(target);
return enterAnimation;
}
/**
* 开始动画处理
* @param target 动画作用 View
*/
private void startAnimation(View target) {
// 进入动画
AnimatorSet enterAnimator = generateEnterAnimation(target);
// 路径动画
ValueAnimator curveAnimator = generateCurveAnimation(target);
// 设置动画集合, 先执行进入动画, 最后再执行运动曲线动画
AnimatorSet finalAnimatorSet = new AnimatorSet();
finalAnimatorSet.setTarget(target);
finalAnimatorSet.playSequentially(enterAnimator, curveAnimator);
finalAnimatorSet.addListener(new AnimationEndListener(target));
finalAnimatorSet.start();
}
private Animator getFinalAnimator(View target) {
AnimatorSet set = getEnterAnimator(target);
ValueAnimator bezierValueAnimator = getBezierValueAnimator(target);
AnimatorSet finalSet = new AnimatorSet();
finalSet.playTogether(set, bezierValueAnimator);
finalSet.setInterpolator(new AccelerateInterpolator());
finalSet.setTarget(target);
return finalSet;
}
private void playAnimation(int animationResourceId) {
if (!isInEditMode()) {
AnimatorSet animator = (AnimatorSet) AnimatorInflater
.loadAnimator(getContext(), animationResourceId);
animator.setTarget(getChildAt(0));
animator.start();
}
}
private AnimatorSet getEnterAnimtor(final View target) {
ObjectAnimator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, 0.2f, 1f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(target, View.SCALE_X, 0.2f, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(target, View.SCALE_Y, 0.2f, 1f);
AnimatorSet enter = new AnimatorSet();
enter.setDuration(500);
enter.setInterpolator(new LinearInterpolator());
enter.playTogether(alpha, scaleX, scaleY);
enter.setTarget(target);
return enter;
}
private Animator getAnimator(View target) {
AnimatorSet set = getEnterAnimtor(target);
ValueAnimator bezierValueAnimator = getBezierValueAnimator(target);
AnimatorSet finalSet = new AnimatorSet();
finalSet.playSequentially(set);
finalSet.playSequentially(set, bezierValueAnimator);
finalSet.setInterpolator(interpolators[random.nextInt(4)]);
finalSet.setTarget(target);
return finalSet;
}
private AnimatorSet getEnterAnimtor(final View target) {
ObjectAnimator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, 0.2f, 1f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(target, View.SCALE_X, 0.2f, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(target, View.SCALE_Y, 0.2f, 1f);
AnimatorSet enter = new AnimatorSet();
enter.setDuration(500);
enter.setInterpolator(new LinearInterpolator());
enter.playTogether(alpha, scaleX, scaleY);
enter.setTarget(target);
return enter;
}
private void playAnimationClickDown() {
if (!isInEditMode()) {
AnimatorSet animator = (AnimatorSet) AnimatorInflater
.loadAnimator(getContext(), R.animator.bubble_down_click_animator);
animator.setTarget(this);
animator.start();
}
}
@Override
protected AnimatorSet generateAddAnimator(RecyclerView.ViewHolder holder) {
View target = holder.itemView;
AnimatorSet animator = new AnimatorSet();
animator.playTogether(
ObjectAnimator.ofFloat(target, "alpha", 0.0f, 1.0f)
);
animator.setTarget(target);
animator.setDuration(getAddDuration());
return animator;
}
private void playAnimation(int animationResourceId) {
if (!isInEditMode()) {
AnimatorSet animator = (AnimatorSet) AnimatorInflater
.loadAnimator(getContext(), animationResourceId);
animator.setTarget(getChildAt(0));
animator.start();
}
}
private Animator getAnimator(View target){
AnimatorSet set = getEnterAnimtor(target);
ValueAnimator bezierValueAnimator = getBezierValueAnimator(target);
AnimatorSet finalSet = new AnimatorSet();
finalSet.playSequentially(set);
finalSet.playSequentially(set, bezierValueAnimator);
finalSet.setInterpolator(interpolators[random.nextInt(4)]);
finalSet.setTarget(target);
return finalSet;
}
@Override
protected AnimatorSet generateRemoveAnimator(RecyclerView.ViewHolder holder) {
View target = holder.itemView;
AnimatorSet animator = new AnimatorSet();
animator.playTogether(
ObjectAnimator.ofFloat(target, "alpha", 1.0f, 0.0f)
);
animator.setTarget(target);
animator.setDuration(getRemoveDuration());
return animator;
}
public static void collectAnimator(Context context, View view){
AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(context, R.animator.collect_anim);
animatorSet.setTarget(view);
animatorSet.start();
}
@Override
public void onSearchCompleted(DetectedObject object, List<Product> productList) {
Log.d(TAG, "Search completed for object index: " + object.getObjectIndex());
searchedObjectMap.put(
object.getObjectIndex(), new SearchedObject(getResources(), object, productList));
if (searchedObjectMap.size() < detectedObjectNum) {
// Hold off showing the result until the search of all detected objects completes.
return;
}
showBottomPromptChip(getString(R.string.static_image_prompt_detected_results));
loadingView.setVisibility(View.GONE);
previewCardCarousel.setAdapter(
new PreviewCardAdapter(ImmutableList.copyOf(searchedObjectMap.values()), this));
previewCardCarousel.addOnScrollListener(
new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
Log.d(TAG, "New card scroll state: " + newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
for (int i = 0; i < recyclerView.getChildCount(); i++) {
View childView = recyclerView.getChildAt(i);
if (childView.getX() >= 0) {
int cardIndex = recyclerView.getChildAdapterPosition(childView);
if (cardIndex != currentSelectedObjectIndex) {
selectNewObject(cardIndex);
}
break;
}
}
}
}
});
for (SearchedObject searchedObject : searchedObjectMap.values()) {
StaticObjectDotView dotView = createDotView(searchedObject);
dotView.setOnClickListener(
v -> {
if (searchedObject.getObjectIndex() == currentSelectedObjectIndex) {
showSearchResults(searchedObject);
} else {
selectNewObject(searchedObject.getObjectIndex());
showSearchResults(searchedObject);
previewCardCarousel.smoothScrollToPosition(searchedObject.getObjectIndex());
}
});
dotViewContainer.addView(dotView);
AnimatorSet animatorSet =
((AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.static_image_dot_enter));
animatorSet.setTarget(dotView);
animatorSet.start();
}
}
private void buildAnimViewWithDrawableResAndStart() {
//构造出 动画的承载体View,并添加进本容器View
IBezierAnimControler animControler = this.animControler;
View theHoldAnimView = null;
if (animControler != null) {
theHoldAnimView = animControler.provideHoldAnimView(this);
// if (theHoldAnimView instanceof ImageView) {
// ((ImageView) theHoldAnimView).setImageResource(willAnimDrawableRes);
// }
}
if (theHoldAnimView == null) {
ImageView holdAnimView = new ImageView(getContext());
holdAnimView.setImageResource(willAnimDrawableRes);
theHoldAnimView = holdAnimView;
}
ViewGroup.LayoutParams vlp = theHoldAnimView.getLayoutParams();
addView(theHoldAnimView, vlp != null ? vlp : flp4ChildView);
//构造初始化 show出的动画,如果有的话
Animator animatorOfAnimViewBorn = null;
if (animControler != null) {
animatorOfAnimViewBorn = animControler.provideFirstShowAnimator(this, theHoldAnimView);
}
if (animatorOfAnimViewBorn == null && isWillUseDefFirstShowAnimator) {
animatorOfAnimViewBorn = buildDefBornViewAnimator(theHoldAnimView, durationOfAnimViewBorn);
}
//贝塞尔曲线动画
ValueAnimator bezierAnimator = buildBezierAnimator(theHoldAnimView);
//动画合集
AnimatorSet wholeAnimatorSet = new AnimatorSet();
AnimatorSet.Builder animsBuilder = wholeAnimatorSet.play(bezierAnimator);
//是否有提供和贝塞尔曲线动画一起执行的动画
Animator willPlayWithBezierAnim = null;
if (animControler != null) {
willPlayWithBezierAnim = animControler.provideAnimatorPlayWithBezierAnim(this, theHoldAnimView);
if (willPlayWithBezierAnim != null) {
//执行时间一定要和贝塞尔曲线动画 执行时间一致
willPlayWithBezierAnim.setDuration(this.beziaAnimatorDuration);
animsBuilder.with(willPlayWithBezierAnim);
}
}
//是否要在初始展示的动画之后再执行贝塞尔曲线动画
if (animatorOfAnimViewBorn != null) {
animsBuilder.after(animatorOfAnimViewBorn);
}
wholeAnimatorSet.setTarget(theHoldAnimView);
wholeAnimatorSet.start();
}