将图像从一种布局拖放到另一种布局

时间:2015-11-28 06:49:15

标签: android android-studio

我已经实现了将图像从水平滚动视图拖放到另一个布局,在该布局中可以使用相机的表面视图,当我放下图像时,包含图像的布局会移除,但我不想这样做。请帮助我,我搜索了很多,但没有得到。我的XML和Java代码在这里

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

<HorizontalScrollView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:scrollbars="none"
    android:layout_marginBottom="5dp" >

    <LinearLayout
        android:id="@+id/inhorizontalscrollview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/civ"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/a"/>

    </LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, View.OnTouchListener, View.OnDragListener {

private static final String LOGCAT = "CamTestActivity";

SurfaceView cameraView;
SurfaceHolder surfaceHolder;
Camera camera;
RelativeLayout.LayoutParams params;

int[] img={R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f};


RelativeLayout fm;
LinearLayout inHorizontalScrollView;
ImageView civ;

int width,height;


@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.content_main);
    /*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);*/

    inHorizontalScrollView = (LinearLayout) findViewById(R.id.inhorizontalscrollview);        

    /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });*/


    fm = ((RelativeLayout) findViewById(R.id.layout));

    civ = (ImageView) findViewById(R.id.civ);

    inHorizontalScrollView.setOnDragListener(this);
    civ.setOnTouchListener(this);
    fm.setOnDragListener(this);
    //findViewById(R.id.mainLayout).setOnDragListener(this);
    cameraView = (SurfaceView) this.findViewById(R.id.surfaceView);
    surfaceHolder = cameraView.getHolder();
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surfaceHolder.addCallback(this);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    width = size.x;
    height = size.y;
    Log.d(LOGCAT, "width = "+width+"   height = "+height);

    /*for(int i=0;i<img.length;i++){
        //Log.v("on recent frnds", "on post execute");
        //if(recfrnd.get(i).length()>1){
            //Log.v("on recent frnds", "in if   "+recfrnd.get(i));
            civ = new ImageView(MainActivity.this);
            civ.setLayoutParams(new LayoutParams(120, 120));
            civ.setPadding(0, 0, 25, 0);
            civ.setImageResource(img[i]);
        inHorizontalScrollView.addView(civ);
        civ.setOnLongClickListener(this);


    }*/

}


@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_main, 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_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



@Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
    try {
        camera.setPreviewDisplay(holder);
        Camera.Parameters parameters = camera.getParameters();
        if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
            parameters.set("orientation", "portrait");
            camera.setDisplayOrientation(90);
        }
        camera.setParameters(parameters);
    } catch (IOException exception) {
        camera.release();
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    camera.startPreview();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
}

@Override
public boolean onTouch(View view, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(null, shadowBuilder, view, 0);
        //view.setVisibility(View.INVISIBLE);
        return true;
    }
    else {
        return false;
    }
}

@Override
public boolean onDrag(View layoutview, DragEvent dragevent) {
    int x_cord=0,y_cord=0;
    int action = dragevent.getAction();
    switch (action) {
        case DragEvent.ACTION_DRAG_STARTED:
            Log.d(LOGCAT, "Drag event started");
            return true;
            //break;
        case DragEvent.ACTION_DRAG_ENTERED:
            Log.d(LOGCAT, "Drag event entered into "+layoutview.toString());
            if(layoutview.getId()==R.id.layout) {
               // params = (FrameLayout.LayoutParams) layoutview.getLayoutParams();
                params=new RelativeLayout.LayoutParams(civ.getWidth(),civ.getHeight());
                return true;
            }
            return false;
           // break;
        case DragEvent.ACTION_DRAG_EXITED:
            Log.d(LOGCAT, "Drag event exited from "+layoutview.toString());
            return false;
            //break;
        case DragEvent.ACTION_DROP:
            Log.d(LOGCAT, "Dropped");
            View view = (View) dragevent.getLocalState();
            ViewGroup owner = (ViewGroup) view.getParent();
            ViewGroup container = (ViewGroup) layoutview;
            owner.removeView(view);
            if(layoutview.getId()==R.id.layout) {
               // RelativeLayout container = (RelativeLayout)layoutview;
                x_cord = (int) dragevent.getX();
                y_cord = (int) dragevent.getY();
                Log.d(LOGCAT, "Dropped  x="+x_cord+"    y="+y_cord+"   w="+civ.getWidth()+"    h="+civ.getHeight());
                if(x_cord+civ.getWidth()>width){
                    int min = x_cord+civ.getWidth()-width;
                    x_cord = x_cord-min;
                }
                if(y_cord+civ.getHeight()>height){
                    int min = y_cord+civ.getHeight()-height;
                    y_cord = y_cord - min;
                }
                params.leftMargin = x_cord;
                params.topMargin = y_cord;
                Log.d(LOGCAT, "Dropped  x=" + x_cord + "    y=" + y_cord + "   w=" + civ.getWidth() + "    h=" + civ.getHeight());
                //v.setLayoutParams(params);
                //view.setX(x_cord);
                //view.setY(y_cord);
                container.addView(view, params);
                view.setVisibility(View.VISIBLE);
            }else{
               // LinearLayout container = (LinearLayout) layoutview;
                container.addView(view);
                view.setVisibility(View.VISIBLE);

            }
            return true;
            //break;
        case DragEvent.ACTION_DRAG_ENDED:
            Log.d(LOGCAT, "Drag ended");
            return false;
            //break;
        default:
            break;
    }
    return false;
}
}

1 个答案:

答案 0 :(得分:0)

只有布局区域显示一个视图显示您无法在此问题的另一个布局中看到该图像,您必须创建宽度和高度与匹配父级的假布局,并在运行时在该假布局上添加您的视图在视图位置并在拖动后拖动您想要的假布局。