티스토리 뷰


누군가를 위해 제작되었다 -_-

완성하면 아래처럼 드래그하면 이미지가 마우스를 따라다니고.. 드롭을 하면 그자리에 머물게 된다.




** CODE
package com.drag;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.AbsoluteLayout.LayoutParams;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
 
 private ImageView me;
 private int viewX = 200;
 private int viewY = 200;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        me = (ImageView)findViewById(R.id.me);
        me.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
     switch(event.getAction()){
     case MotionEvent.ACTION_MOVE:
      viewX = viewX + (int)event.getX();
      viewY = viewY + (int)event.getY();
      
     LayoutParams laypam = new LayoutParams (me.getWidth(), me.getHeight(), viewX, viewY) ;
     me.setLayoutParams(laypam);
      break;
     }//end switch
    return true;
   }
  });
    }
}





** XML

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
     <ImageView 
  android:id="@+id/me"
  android:layout_x="200px"
  android:layout_y="200px"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/pic"
     />
</AbsoluteLayout>



소스파일 다운하기
------------------------------------------------

dragTest.zip

------------------------------------------------

자작된 소스로.. 상용이 아니면 마음대로 사용해도 무방하겠다.

[원본] devbible.tistory.com/48
[출처] devbible.tistory.com

댓글