为什么移动GroupView工作不好?

时间:2012-05-03 17:16:49

标签: android android-layout animation event-handling drawing

我制作了两个嵌套RelativeLayout s,用一些TextView填充了一个,并对手指拖动客户做出反应。

但它的效果很糟糕:

1)移动组留下垃圾痕迹,好像背景不重绘

2)leftMargin == 800的列不会绘制

XML布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/stator"
>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp"
    android:id="@+id/mover"
    android:background="@android:color/darker_gray"
    >

</RelativeLayout>

</RelativeLayout>

java代码:

 public class SymbolPadActivity extends Activity {

private RelativeLayout mover;
private RelativeLayout stator;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    mover = (RelativeLayout) findViewById(R.id.mover);

    RelativeLayout.LayoutParams labelParams;
    TextView textView;
    for(int leftMargin = 0; leftMargin<1500; leftMargin += 200) {
        for(int topMargin=0; topMargin<800; topMargin += 80) {

            // I can't omit these 3 lines 
            labelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);

            labelParams.leftMargin = leftMargin;
            labelParams.topMargin = topMargin;

            textView = new TextView(this);
            textView.setText("(" + leftMargin + "," + topMargin + ")");
            mover.addView(textView, labelParams);

        }
    }

    stator = (RelativeLayout) findViewById(R.id.stator);


    stator.setOnTouchListener(new View.OnTouchListener() {

        private int startleft, starttop;
        private float startx, starty;
        private boolean started;

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if( event.getActionMasked() == MotionEvent.ACTION_DOWN ) {
                started = true;
                startx = event.getX();
                starty = event.getY();
                startleft = mover.getLeft();
                starttop = mover.getTop();
                return true;
            }

            else if( event.getActionMasked() == MotionEvent.ACTION_UP ) {
                started = false;
                startx = starty = 0;
                return true;
            }

            else if( event.getActionMasked() == MotionEvent.ACTION_MOVE ) {
                mover.setLeft(  startleft + (int)(event.getX() - startx) );
                mover.setTop(  starttop + (int)(event.getY() - starty) );
                return true;
            }

            else {
                return false;
            }

        }
    });
}
}

如何正确实现目标(简要说明)?

0 个答案:

没有答案