如何在安卓上使imageView动态跨越布局?

5

我正在尝试将ImageView从子布局(RelativeLayout)动画到父布局(RelativeLayout)。当进入父布局时,ImageView没有被绘制。

enter code 




/** Called when the activity is first created. */

private static final int   SWIPE_MIN          = 20;
private static final int   SWIPE_MAX_OFF_PATH = 250;
private static final int   SWIPE_THRESHOLD    = 20;
GestureDetector detector;
ImageView topImage;
ImageView bottomImage;
ImageView aView;
RelativeLayout rlBottomChild;
RelativeLayout rlTopChild;
FrameLayout flTopChild;
FrameLayout flBottomChild;
RelativeLayout table;
RelativeLayout relativeLayout ;
FrameLayout frameLayout ;
WindowManager wManger;
int screenWidth;
int screenHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    wManger = getWindowManager();
    screenWidth = wManger.getDefaultDisplay().getWidth();
    screenHeight = wManger.getDefaultDisplay().getHeight();
    detector = new GestureDetector(this);

    bottomImage = new ImageView(this);
    bottomImage.setImageResource(R.drawable.icon);
    bottomImage.setOnTouchListener(this);
    bottomImage.setDrawingCacheEnabled(true);

    topImage = new ImageView(this);
    topImage.setImageResource(R.drawable.icon);
    topImage.setOnTouchListener(this);
    topImage.setDrawingCacheEnabled(true);


    initRelativeLayout();


    setContentView(relativeLayout);
}


/**
 * <p> Used to </p>
 */
private void initRelativeLayout() {



    // Top Player 
    rlTopChild = new RelativeLayout(this);
    rlTopChild.setId(1);
    LayoutParams p2 = new LayoutParams(LayoutParams.FILL_PARENT  , ((10*screenHeight)/100));
    p2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    rlTopChild.setBackgroundColor(Color.BLUE);
    rlTopChild.addView(topImage);
    rlTopChild.setLayoutParams(p2);

    // Bottom Player 
    rlBottomChild = new RelativeLayout(this);
    rlBottomChild.setId(3);
    LayoutParams p = new LayoutParams(LayoutParams.FILL_PARENT  , ((10*screenHeight)/100));
    p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    rlBottomChild.setBackgroundColor(Color.BLUE);
    rlBottomChild.addView(bottomImage);
    rlBottomChild.setClipChildren(false);
    rlBottomChild.setLayoutParams(p);


    // Table Layout 
    table = new RelativeLayout(this);
    table.setId(2);
    LayoutParams p3 = new LayoutParams(LayoutParams.FILL_PARENT  , ((50*screenHeight)/100));
    p3.addRule(RelativeLayout.CENTER_IN_PARENT);
    p3.addRule(RelativeLayout.BELOW , rlTopChild.getId());
    p3.addRule(RelativeLayout.ABOVE , rlBottomChild.getId());
    table.setBackgroundColor(Color.WHITE);
    table.setLayoutParams(p3);


    relativeLayout =new RelativeLayout(this);
    relativeLayout.setBackgroundColor(Color.GREEN);
    relativeLayout.addView(rlBottomChild);
    relativeLayout.addView(rlTopChild);
    relativeLayout.addView(table);
}

/**
 * <p> Overriding the method </p>
 * @param v
 * @param event
 * @return
 * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
 */
@Override
public boolean onTouch(View v, MotionEvent event) {
    return  detector.onTouchEvent(event);

}

/**
 * <p> Overriding the method </p>
 * @param e
 * @return
 * @see android.view.GestureDetector.OnGestureListener#onDown(android.view.MotionEvent)
 */
@Override
public boolean onDown(MotionEvent e) {
    // TODOAuto-generated method stub
    return true;

}

/**
 * <p> Overriding the method </p>
 * @param e1
 * @param e2
 * @param velocityX
 * @param velocityY
 * @return
 * @see android.view.GestureDetector.OnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
 */
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    try {
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
            return false;
        }
        if (e1.getX() - e2.getX() > SWIPE_MIN && Math.abs(velocityX) > SWIPE_THRESHOLD) {
           debug("Left Swipe");
           TranslateAnimation animation= new TranslateAnimation(e1.getX(),e2.getX(),e1.getY() ,e2.getY());
           animation.setAnimationListener(this);
           animation.setDuration(3000);
           bottomImage.startAnimation(animation);
        } else if (e2.getX() - e1.getX() > SWIPE_MIN && Math.abs(velocityX) > SWIPE_THRESHOLD) {
            debug("Right Swipe ");
            debug("Swipe down ...");
            TranslateAnimation animation= new TranslateAnimation(e1.getX(),e2.getX(),e1.getY() ,e2.getY());
            animation.setAnimationListener(this);
            animation.setDuration(3000);
            topImage.startAnimation(animation);
        } else if (e1.getY() - e2.getY() > SWIPE_MIN && Math.abs(velocityX) > SWIPE_THRESHOLD) {
           debug("Swipe up ... ");
           TranslateAnimation animation= new TranslateAnimation(e1.getX(),e2.getX(),e1.getY() ,e2.getY());
           animation.setAnimationListener(this);
           animation.setDuration(3000);
           bottomImage.startAnimation(animation);
        } else if (e2.getY() - e1.getY() > SWIPE_MIN && Math.abs(velocityX) > SWIPE_THRESHOLD) {
            debug("Swipe down ...");
            TranslateAnimation animation= new TranslateAnimation(e1.getX(),e2.getX(),e1.getY() ,e2.getY());
            animation.setAnimationListener(this);
            animation.setDuration(3000);
            topImage.startAnimation(animation);
        }
    } catch (Exception e) {
        // nothing
    }
    return true;

}

/**
 * <p> Used to </p>
 * @param string
 */
private void debug(String string) {
    Log.d("TAGGG", string);         
}

/**
 * <p> Overriding the method </p>
 * @param e
 * @see android.view.GestureDetector.OnGestureListener#onLongPress(android.view.MotionEvent)
 */
@Override
public void onLongPress(MotionEvent e) {
    // TODOAuto-generated method stub

}

/**
 * <p> Overriding the method </p>
 * @param e1
 * @param e2
 * @param distanceX
 * @param distanceY
 * @return
 * @see android.view.GestureDetector.OnGestureListener#onScroll(android.view.MotionEvent, android.view.MotionEvent, float, float)
 */
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    // TODOAuto-generated method stub
    return true;

}

/**
 * <p> Overriding the method </p>
 * @param e
 * @see android.view.GestureDetector.OnGestureListener#onShowPress(android.view.MotionEvent)
 */
@Override
public void onShowPress(MotionEvent e) {
    // TODOAuto-generated method stub

}

/**
 * <p> Overriding the method </p>
 * @param e
 * @return
 * @see android.view.GestureDetector.OnGestureListener#onSingleTapUp(android.view.MotionEvent)
 */
@Override
public boolean onSingleTapUp(MotionEvent e) {
    // TODOAuto-generated method stub
    return true;

}

/**
 * <p> Overriding the method </p>
 * @param animation
 * @see android.view.animation.Animation.AnimationListener#onAnimationEnd(android.view.animation.Animation)
 */
@Override
public void onAnimationEnd(Animation animation) {
    // TODOAuto-generated method stub

}

/**
 * <p> Overriding the method </p>
 * @param animation
 * @see android.view.animation.Animation.AnimationListener#onAnimationRepeat(android.view.animation.Animation)
 */
@Override
public void onAnimationRepeat(Animation animation) {
    // TODOAuto-generated method stub

}

/**
 * <p> Overriding the method </p>
 * @param animation
 * @see android.view.animation.Animation.AnimationListener#onAnimationStart(android.view.animation.Animation)
 */
@Override
public void onAnimationStart(Animation animation) {
}

}

这里

谢谢。jega


嘿Jega,你能提供更多的上下文吗?实际上,我对这段代码感到困惑,因为我不知道dx/dy的翻译是从哪里来的。动画监听器是否将ImageView从其原始位置移除,然后将其添加到其他布局中?它是否会将适当的规则添加到ImageView的LayoutPArams中以正确对齐它? - Greg Giacovelli
谢谢 Greg,动画监听器不会删除子元素。我已经更新了我的代码。你能帮我吗? - Jega
3个回答

5
为了让一个视图在其父视图之外显示,您需要将父视图的clipChildren属性设置为false。
在您的代码中添加以下行,它应该就可以工作了:
relativeLayout.setClipChildren(false);
rlBottomChild.setClipChildren(false);
rlTopChild.setClipChildren(false);

1

0

要想在 Android 中保留由动画引起的更改,您需要添加这行代码:

animation.setFillAfter(true);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接