我能想出的唯一解决方案,虽然很老套,但仍然有效。
父级是RelativeLayout,我有一个隐藏的自定义TextView z 位于其他内容上方。当我需要调用动画时,我为 TextView 提供与我需要动画的 View 相同的 LayoutParams/background/text,将其设置为 View.VISIBLE,对其进行动画处理,并在动画完成后将其设置回 View.GONE :
public void doAnimation(final View v){
v.setId(5);
final CustomTextView animatorImage = (CustomTextView) ((MainActivity)mActivity).findViewById(R.id.pick_animator_image); // Our hidden TextView in the FrameLayout
try{
animatorImage.setBackgroundDrawable(v.getBackground());
animatorImage.setText(((TextView)v).getText().toString());
animatorImage.setTextColor(((TextView)v).getCurrentTextColor());
animatorImage.setTextSize(TypedValue.COMPLEX_UNIT_PX, ((TextView)v).getTextSize());
}
catch(Exception e){
}
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight());
int[] windowLocation = new int[2];
v.getLocationInWindow(windowLocation);
params.leftMargin = (int) windowLocation[0];
params.topMargin = (int) windowLocation[1] - ((MainActivity)mActivity).getSupportActionBar().getHeight() - getStatusBarHeight(); // Subtract the ActionBar height and the StatusBar height if they're visible
animatorImage.setLayoutParams(params);
animatorImage.setVisibility(View.VISIBLE);
v.setVisibility(View.INVISIBLE);
ScaleAnimation scaleAnim = new ScaleAnimation(1f, 2.5f, 1f, 2.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnim.setDuration(300);
scaleAnim.setZAdjustment(Animation.ZORDER_TOP);
scaleAnim.setAnimationListener(new AnimationListener(){
public void onAnimationEnd(Animation arg0) {
ScaleAnimation scaleAnim2 = new ScaleAnimation(2.5f, 1f, 2.5f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnim2.setDuration(300);
scaleAnim2.setZAdjustment(Animation.ZORDER_TOP);
scaleAnim2.setAnimationListener(new AnimationListener(){
public void onAnimationEnd(Animation animation) {
animatorImage.clearAnimation();
v.setVisibility(View.VISIBLE);
animatorImage.setVisibility(View.GONE);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}});
animatorImage.startAnimation(scaleAnim2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}});
animatorImage.bringToFront();
animatorImage.startAnimation(scaleAnim);
}
XML 层次结构是:
RelativeLayout (parent)
ViewGroup (main content body)
TextView (hidden View to animate)
只需将“R.id.pick_animator_image”更改为您的 TextView 的 ID 在 RelativeLayout 中的任何内容,并使用您想要动画的视图调用该方法,这将为您伪造它。