拖放 - 如何?

时间:2017-10-31 17:32:21

标签: android

Basiaclly我对此非常陌生并且我有点挣扎,我已经有了这个代码来完美地拖放一个图像。

我现在想要这样做,所以我可以拖动第二个单独的图像,我将如何转换这个允许我拖动另一个图像究竟如何第一个

 import android.app.Activity;
 import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.FrameLayout.LayoutParams;
public class ApproachingLeft extends Activity {
private CanvasView customCanvas;
int windowwidth;
int windowheight;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_approaching_left);
    customCanvas = (CanvasView) findViewById(R.id.signature_canvas);


            windowwidth = getWindowManager().getDefaultDisplay().getWidth();
            windowheight = getWindowManager().getDefaultDisplay().getHeight();
            final ImageView balls = (ImageView)findViewById(R.id.car);


            balls.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    LayoutParams layoutParams = (LayoutParams) balls.getLayoutParams();
                    switch(event.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int x_cord = (int)event.getRawX();
                            int y_cord = (int)event.getRawY();
                            if(x_cord>windowwidth){x_cord=windowwidth;}
                            if(y_cord>windowheight){y_cord=windowheight;}

                            layoutParams.leftMargin = x_cord -25;
                            layoutParams.topMargin = y_cord - 75;

                            balls.setLayoutParams(layoutParams);
                            break;
                        default:
                            break;
                    }
                    return true;
                }
            });
        }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_start_screen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_menu) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void clearCanvas(View v) {
    customCanvas.clearCanvas();
}

}

我现在想要这样做,所以我可以拖动第二个单独的图像,我将如何转换这个允许我拖动另一个图像究竟如何第一个

0 个答案:

没有答案