(Android) Drag And Drop

Android drag and drop merupakan fitur yang di berikan oleh pihak developer android , Drag and Drop yang digunakan teknogi ACTION_UP , ACTION_DOWN , ACTION_MOVE .

main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	android:gravity="center" android:id="@+id/LinearLayout01">

	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:id="@+id/btn"
		android:text=""
		android:background="@drawable/ic_launcher"></Button>
</FrameLayout>

Class.java

package com.example.draganddrop;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.FrameLayout.LayoutParams;

public class DragAndDropMainActivity extends Activity implements OnTouchListener{
	
	private final static int START_DRAGGING = 0;
	private final static int STOP_DRAGGING = 0;

	private Button btn;
	private FrameLayout layout;
	private int status;
	private LayoutParams params;

	private ImageView image;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drag_and_drop_main);
        layout = (FrameLayout) findViewById(R.id.LinearLayout01);
	
		btn = (Button) findViewById(R.id.btn);
		btn.setDrawingCacheEnabled(true);
		btn.setOnTouchListener(this);

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_drag_and_drop_main, menu);
        return true;
    }
    public boolean onTouch(View view, MotionEvent me) {
                //penyataan ke bawah
		if (me.getAction() == MotionEvent.ACTION_DOWN) {
			status = START_DRAGGING;
			image = new ImageView(this);
			image.setImageBitmap(btn.getDrawingCache());
			layout.addView(image, params);
		}
                //pernyataan ke atas
		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;
	}
}

Advertisements

Published by

jarcode

hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s