如何避免使用setLayoutPramas()来复制ImageView?

时间:2014-07-28 12:34:33

标签: android

我在FrameLayout中有ImageView,我使用onTouchListener仅在Y轴上移动ImageView。

我的问题:当我尝试移动ImageView进行复制时,我需要移动ImageView而不需要复制。

这里我创建了图像

public void onAnimationEnd(Animation arg0) {
    // TODO Auto-generated method stub
    for (int i = 0; i < 20; i++) {
        if (arg0 == cardsAnimation[i]) {
            cardsView[i + 1].startAnimation(cardsAnimation[i + 1]);
        }
        if (arg0 == ia11) {
            int w = (int) (cardsView[9].getWidth() / 2);
            int h = (int) (cardsView[9].getHeight() / 2);
            int x = (int) (rl.getWidth() * 0.14);
            int y = (int) (rl.getHeight() * 0.50);
            ImageView iv = new ImageView(MainActivity.this);
            iv.setId(R.id.Card1);
            cardsView[9 - (d * 3)].clearAnimation();
            cardsView[9 - (d * 3)].setVisibility(View.GONE);
            iv.setImageResource(ids.get(P1ids[0]));
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    w, h);
            params.topMargin = h / 2 + y - 10;
            params.leftMargin = w / 2 + x;
            fl.addView(iv, 0, params);
            iv.setOnTouchListener(this);
        }
        if (arg0 == ia22) {
            int w = (int) (cardsView[10].getWidth() / 2);
            int h = (int) (cardsView[10].getHeight() / 2);
            int x = (int) (rl.getWidth() * 0.10);
            int y = (int) (rl.getHeight() * 0.48);
            ImageView iv = new ImageView(MainActivity.this);
            iv.setId(R.id.Card2);
            cardsView[10 - (d * 3)].clearAnimation();
            cardsView[10 - (d * 3)].setVisibility(View.GONE);
            iv.setImageResource(ids.get(P1ids[1]));
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    w, h);
            params.topMargin = h / 2 + y - 6;
            params.leftMargin = w / 2 + x;
            fl.addView(iv, 1, params);
            iv.setOnTouchListener(this);
        }
    }

}

这里我移动ImageView

public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    v.performClick();
    final int Y = (int) event.getRawY();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) v
                .getLayoutParams();
        yDelta = Y - lParams.topMargin;
        break;
    case MotionEvent.ACTION_UP:

        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        break;
    case MotionEvent.ACTION_POINTER_UP:
        break;
    case MotionEvent.ACTION_MOVE:
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) v
                .getLayoutParams();
        layoutParams.topMargin = Y - yDelta;
        v.setLayoutParams(layoutParams);
        break;

    }

    fl.invalidate();
    return true;
}

enter image description here

0 个答案:

没有答案