Skip to main content

Drag and drop image in android

Ever wonder how to drag and drop in android. Lets check how to drag and drop image in android.

MainActivity.java



package com.example.dragdrop;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnClickListener;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity  {
 ImageView img_model, img_chain;
 private static final String IMAGEVIEW_TAG = "Android Logo";
 private android.widget.RelativeLayout.LayoutParams layoutParams;
 String msg;
 int windowwidth, windowheight;
 float x, y, dx, dy;
 Bitmap bMap;
 //SeekBar seek;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  img_chain = (ImageView) findViewById(R.id.img2);
  img_model = (ImageView) findViewById(R.id.img1);
  bMap = BitmapFactory
    .decodeResource(getResources(), R.drawable.chainsss);
 
  img_chain.setOnTouchListener(new OnTouchListener() {

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

    // TODO Auto-generated method stub
    layoutParams = (RelativeLayout.LayoutParams) img_chain
      .getLayoutParams();
    /*
     * switch(event.getActionMasked()) {
     */

    Matrix matrix = new Matrix();
    img_chain.setScaleType(ScaleType.MATRIX); // required
    matrix.postRotate((float) 30, 20, 50);

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
     x = event.getX();// 2.3 not supported
     y = event.getY();// 2.3 not supported
     dx = x - img_chain.getX();// 2.3 not supported
     dy = y - img_chain.getY();// 2.3 not supported
    }

     return true;
    case MotionEvent.ACTION_MOVE: {
     img_chain.setX(event.getX() - dx);// 2.3 not supported
     img_chain.setY(event.getY() - dy);// 2.3 not supported

    }
     return true;
    case MotionEvent.ACTION_UP: {
     // your stuff
    }
     return true;
    }
    return false;

   }
  });
 }

}
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.dragdrop.MainActivity" >
 <ImageView
            android:id="@+id/img1"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:src="@drawable/model" />
        <ImageView
            android:id="@+id/img2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/chainsss"
            android:layout_gravity="center"
            android:layout_marginTop="60dp" /> 
</RelativeLayout>

Comments

Popular posts from this blog

How to use Preference Activity in Android

Preference Activity is a base class mainly used for setting order of preferences of an user.It is mainly used for creating setting page of an user.In this example I am just showing how to set Preferences. First create an Activity MainActivity.java Here on clicking preference example button a page will be opened to set the preferences. On clicking the show values we can see the see the values that we have set. MainActivity.java import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn_pref); Button btn1 = (Button) findViewById(R.id.btn_values); btn.setOnCli...

Get List of All Installed Apps

For getting the list of all apps installed in the device and to show it on a listview we can use the following code: MainActivity.java import java.util.ArrayList; import java.util.List; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends ActionBarActivity { ArrayList results_sys_app = new ArrayList(); ArrayAdapter<applicationinfo>adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); final List<applicationinfo> pkgAppsList = this.getPackageManager().getInstalledApplications( PackageManager.GET_UNINSTALLED_PAC...

How to save contacts to the phone contacts in android

We can easily save the contacts to the phone contacts without having any database of own . The code is very simple. import android.app.Activity; import android.app.AlertDialog; import android.content.ComponentName; import android.content.ContentValues; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.Contacts; import android.provider.Contacts.People; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { protected static final int PICK_CONTACT = 0; EditText edt1,edt2; Button btn; String s1,s2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activ...