Android ImageButton未拖动和绘制

Android ImageButton未拖动和绘制,android,Android,在我的问题中,我将一个图像按钮移动到该事件上,拖动停止的位置将在该位置重新绘制一个图像按钮。实际上,这对于简单的按钮来说是非常有效的,但对于图像按钮来说则不然 public class NActivity extends Activity implements OnTouchListener { private final static int START_DRAGGING = 0; private final static int STOP_DRAGGING = 1;

在我的问题中,我将一个图像按钮移动到该事件上,拖动停止的位置将在该位置重新绘制一个图像按钮。实际上,这对于简单的按钮来说是非常有效的,但对于图像按钮来说则不然

public class NActivity extends Activity implements OnTouchListener {

    private final static int START_DRAGGING = 0;
    private final static int STOP_DRAGGING = 1;

    private ImageButton btn;
    private FrameLayout layout;
    private int status;
    private LayoutParams params;
    private ImageView image;

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

        layout = (FrameLayout) findViewById(R.id.LinearLayout01);
        // layout.setOnTouchListener(this);

        btn = (ImageButton) findViewById(R.id.imageButton1);
        btn.setDrawingCacheEnabled(true);
        btn.setOnTouchListener(this);

        params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);

    }

    public boolean onTouch(View view, MotionEvent me) {
        if (me.getAction() == MotionEvent.ACTION_DOWN) {
            status = START_DRAGGING;
            image = new ImageView(this);
            image.setImageBitmap(btn.getDrawingCache());
            layout.addView(image, params);
        }
        if (me.getAction() == MotionEvent.ACTION_UP) {
            status = STOP_DRAGGING;
            Log.i("Drag", "Stopped Dragging");
        } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
            if (status == START_DRAGGING) {
                System.out.println("Dragging");
                image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
                image.invalidate();
            }
        }
        return false;
    }
}

在我的示例中,我使用了一个布局,并将和ImageButton放在其中,这样图像按钮将继承它所在布局的属性 xml:


它非常简单,最后,哪个组件在线性布局中并不重要,因为这个组件不重要

您可能希望首先接受您前面问题的最佳答案。好的,我理解您的观点,但它也不起作用。图像按钮获取既不移动也不离开该点重画
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:src="@drawable/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Testing" />
    </LinearLayout>

</FrameLayout>
public class SomeClass extends Activity implements OnTouchListener {

    private LinearLayout ll1;
    private FrameLayout layout;

    private float xAxis = 0;
    private float yAxis = 0;

    private LinearLayout newLinearLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        layout = (FrameLayout) findViewById(R.id.LinearLayout01);

        ll1 = (LinearLayout) findViewById(R.id.ll);
        newLinearLayout = ll1;

        ll1.setOnTouchListener(this);
    }

    public void redraw() {
        layout.removeAllViews();

        newLinearLayout.setPadding((int) xAxis, (int) yAxis, 0, 0);

        layout.addView(newLinearLayout);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            xAxis = event.getX();
            yAxis = event.getY();
            redraw();

        case MotionEvent.ACTION_UP:
            System.out.println("intermediate finished");

        }
        return true;
    }
}