zahra.mf

حرکت تصویر در صفحه

1 ارسال در این موضوع قرار دارد

سلام

من یه برنامه دارم که عکس دوتا صندلی هست و دوتا جای خالی باید برنامه من جوری نوشته بشه که هر عکس صندلی فقط مخصوص یه جای خالی باشه و هر جایی غیر از جای مخصوص خودش قرار داده بشه به جای اولش برگرده

تصاویر و اینجوری توی یه کلاس به اسم DragLayout تعریف کردم

[shcode=java]package com.test.dragme;

import java.util.ArrayList;

import android.animation.AnimatorSet;

import android.animation.ObjectAnimator;

import android.content.Context;

import android.util.DisplayMetrics;

import android.util.Log;

import android.view.DragEvent;

import android.view.View;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.widget.RelativeLayout.LayoutParams;

public class DragLayout extends RelativeLayout{

boolean DEBUG = true;

Context context;

int screenWidth,screenHeight;

static float up_x=0,up_y=0;

boolean mIsScrolling = false;

    

public DragLayout(Context context) {

super(context);

// TODO Auto-generated constructor stub

this.context = context;

//not to include in main programe

getDimensionsofScreen();

setLayout();

setViews();

}

private void setLayout() {

// set according to parent layout (not according to current layout)

RelativeLayout.LayoutParams rLp = new RelativeLayout.LayoutParams(

LayoutParams.MATCH_PARENT,  LayoutParams.MATCH_PARENT);

rLp.topMargin = 2 * (screenHeight / 25); // calculating 1/10 of 4/5

// screen

this.setLayoutParams(rLp);

}

void setViews() {

ImageView img1 = new ImageView(context);

////RelativeLayout rLp = (RelativeLayout)findViewById(R.id.relativeLayout1);

////rLp.setOnTouchListener(new ChoiceTouchListener(context, 1));

 RelativeLayout.LayoutParams rLp = new RelativeLayout.LayoutParams(

            (screenWidth / 5), (screenHeight / 5));

    rLp.topMargin = (screenHeight / 10);

    rLp.leftMargin = (screenWidth / 10);

img1.setLayoutParams(rLp);

img1.setBackgroundDrawable(getResources().getDrawable(R.drawable.chair));

img1.setOnTouchListener(new ChoiceTouchListener(context, 1));

this.addView(img1);

////RelativeLayout rLp1 = (RelativeLayout)findViewById(R.id.relativeLayout2);

////rLp1.setOnDragListener(new ChoiceDragListener(context));

            ImageView img2 = new ImageView(context);

 rLp = new RelativeLayout.LayoutParams(

(screenWidth / 5), (screenHeight / 5));

rLp.topMargin = (screenHeight / 10);

rLp.leftMargin = (4*screenWidth / 10);

img2.setLayoutParams(rLp);

img2.setBackgroundDrawable(getResources().getDrawable(R.drawable.box_o));

img2.setOnDragListener(new ChoiceDragListener(context));

this.addView(img2);

ImageView img3 = new ImageView(context);

RelativeLayout.LayoutParams rLp1 = new RelativeLayout.LayoutParams(

(screenWidth / 5), (screenHeight / 5));

rLp1.topMargin = (screenHeight / 10);

rLp1.leftMargin = (7*screenWidth / 10);

img3.setLayoutParams(rLp1);

img3.setBackgroundDrawable(getResources().getDrawable(R.drawable.chair));

img3.setOnTouchListener(new ChoiceTouchListener(context, 1));

this.addView(img3);

 ImageView img4 = new ImageView(context);

 rLp = new RelativeLayout.LayoutParams(

(screenWidth / 5), (screenHeight / 5));

rLp.topMargin = (screenHeight / 2);

rLp.leftMargin = (4*screenWidth / 10);

img4.setLayoutParams(rLp);

img4.setBackgroundDrawable(getResources().getDrawable(R.drawable.box_o));

img4.setOnDragListener(new ChoiceDragListener(context));

this.addView(img4);

}

public ArrayList getDimensionsofScreen() {

//metrics that holds the value of height and width

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();;

ArrayList vals = new ArrayList();

vals.add(displayMetrics.widthPixels);

vals.add(displayMetrics.heightPixels);

screenHeight = displayMetrics.heightPixels;

screenWidth = displayMetrics.widthPixels;

return vals;

}

/*

@SuppressLint("NewApi")*/

@Override

public boolean onDragEvent(DragEvent event) {

int mCurX = (int) event.getX();

        int mCurY = (int) event.getY();

        

if(event.getAction() == DragEvent.ACTION_DROP || event.getAction() == DragEvent.ACTION_DRAG_EXITED) {

        Log.v("here","it is :: " + mCurX + ", "+mCurY);

        

View view1 = (View) event.getLocalState();

view1.setVisibility(View.VISIBLE);

//ObjectAnimator animationx = ObjectAnimator.ofFloat(view1,"translationX", mCurX - ChoiceTouchListener.offsetX-(screenWidth / 10),0.0f);

//ObjectAnimator animationy = ObjectAnimator.ofFloat(view1, "translationY", mCurY - ChoiceTouchListener.offsetY-(screenHeight / 10),0.0f);

//AnimatorSet animSet = new AnimatorSet();

//animSet.setDuration(500);

//animSet.playTogether(animationx,animationy);

//animSet.start();

}

return true;

}

}

[/shcode]

یه کلاس ChoiceTouchListener هم دارم که این جوری نوشتم 

[shcode=java]package com.test.dragme;

import android.content.ClipData;

import android.content.Context;

import android.graphics.Rect;

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.OnTouchListener;

public final  class ChoiceTouchListener implements OnTouchListener{

Context context;

int index;

static float offsetX = 0,offsetY = 0;

    boolean canImageMove;  

static float offsetXx = 0,offsetYy = 0;

DragShadowBuilder shadowBuilder;

public ChoiceTouchListener(Context context,int index) {

super();

this.context = context;

this.index = index;

}

private Rect rect;    // Variable rect to hold the bounds of the view

public boolean onTouch(View view, MotionEvent motionEvent) {

   // int eventaction = motionEvent.getAction();

int eventaction = motionEvent.getAction();

//switch (motionEvent.getAction()) {

        //case motionEvent.ACTION_DOWN:

/*

 * Drag details: we only need default behavior

 * - clip data could be set to pass data as part of drag

 * - shadow can be tailored

 */

//view.setTag("option"+index);

//ClipData data = ClipData.newPlainText("tag", "option"+index);

//shadowBuilder = new View.DragShadowBuilder(view);

////start dragging the item touched

//view.startDrag(data, shadowBuilder, view, 0);

////offsetX = view.getLeft();//(int)view.getX();//(int)motionEvent.getX();

//// offsetY = view.getTop();//(int)view.getY();//motionEvent.getY();

// view.setVisibility(View.GONE);}

//// Log.v("here","it is ::" + (int)motionEvent.getX() + " , "+(int)motionEvent.getY());}

//break;

 switch (eventaction) {

        case MotionEvent.ACTION_DOWN: 

            // finger touches the screen

        

        

         view.setTag("option"+index);

ClipData data = ClipData.newPlainText("tag", "option"+index);

shadowBuilder = new View.DragShadowBuilder(view);

////start dragging the item touched

view.startDrag(data, shadowBuilder, view, 0);

////offsetX = view.getLeft();//(int)view.getX();//(int)motionEvent.getX();

//// offsetY = view.getTop();//(int)view.getY();//motionEvent.getY();

view.setVisibility(View.GONE);

//// Log.v("here","it is ::" + (int)motionEvent.getX() + " , "+(int)motionEvent.getY());

            break;

            /* case motionEvent.ACTION_MOVE:  

 public boolean onDrag(View v, DragEvent event) {

int mCurX = (int) event.getX();

        int mCurY = (int) event.getY();

        

if(event.getAction() == DragEvent.ACTION_DROP || event.getAction() == DragEvent.ACTION_DRAG_EXITED) {

        Log.v("here","it is :: " + mCurX + ", "+mCurY);

        

View view1 = (View) event.getLocalState();

view1.setVisibility(View.VISIBLE);

}

 

 }

     break; */

            

            

            

       // case MotionEvent.ACTION_MOVE:

            // finger moves on the screen

         // public boolean onDrag(View v, DragEvent event) {

//int mCurX = (int) event.getX();

        //int mCurY = (int) event.getY();

        

// if(event.getAction() == DragEvent.ACTION_DROP || event.getAction() == DragEvent.ACTION_DRAG_EXITED){ 

       // Log.v("here","it is :: " + mCurX + ", "+mCurY);

        

// view view1 = (View) event.getLocalState();

//view1.setVisibility(View.VISIBLE);

//}

 

// }

         //   break;

       case MotionEvent.ACTION_UP:

// view.setVisibility(View.FOCUS_UP);

           canImageMove = true;  

          // if (!rect.contains(view.getLeft() + (int) motionEvent.getX(), view.getTop() + (int) motionEvent.getY())) 

// view.setTag("option"+index);

//ClipData data1 = ClipData.newPlainText("tag", "option"+index);

//shadowBuilder = new View.DragShadowBuilder(view);

////start dragging the item touched

//view.startDrag(data1, shadowBuilder, view, 0);

//offsetX = view.getLeft();//(int)view.getX();//(int)motionEvent.getX();

 //offsetY = view.getTop();//(int)view.getY();//motionEvent.getY();

 //view.setVisibility(View.FOCUS_UP);

 //Log.v("here","it is ::" + (int)motionEvent.getX() + " , "+(int)motionEvent.getY());

 

 //return false;

//default:

 break;

            

            

       // case MotionEvent.ACTION_UP:   

            // finger leaves the screen

         //   break;

 

}

return true;

//}

}

//@Override

//public boolean onTouch(View v, MotionEvent event) {

// TODO Auto-generated method stub

//return false;

//}

}[/shcode]

یه کلاس ChoiceDragListener هم دارم که اینطوری نوشتم

[shcode=java]package com.test.dragme;

import android.app.Activity;

import android.content.ClipData;

import android.content.Context;

import android.content.ClipData.Item;

import android.util.Log;

import android.view.DragEvent;

import android.view.View;

import android.view.View.OnDragListener;

import android.widget.ImageView;

public class ChoiceDragListener implements OnDragListener{

boolean DEBUG = true;

Context context;

ChoiceDragListener(Context context){

this.context = context;

}

@Override

public boolean onDrag(View v, DragEvent event) {

switch (event.getAction()) {

case DragEvent.ACTION_DRAG_STARTED:

if(DEBUG) Log.v("here","drag started");

break;

case DragEvent.ACTION_DRAG_ENTERED:

break;

case DragEvent.ACTION_DRAG_LOCATION:

 int mCurX = (int) event.getX();

        int mCurY = (int) event.getY();

        

        

        

        if(DEBUG) Log.v("Cur(X, Y) : " ,"here ::" + mCurX + ", " + mCurY );

        

        

        break;

case DragEvent.ACTION_DRAG_EXITED:        

if(DEBUG)

Log.v("here","drag exits");

break;

case DragEvent.ACTION_DROP:

//handle the dragged view being dropped over a drop view

View view = (View) event.getLocalState();

ClipData cd =  event.getClipData();

Item item = cd.getItemAt(0);

String resp = item.coerceToText(context).toString();

//stop displaying the view where it was before it was dragged

view.setVisibility(View.INVISIBLE);

//view dragged item is being dropped on

ImageView dropTarget = (ImageView) v;

//view being dragged and dropped

ImageView dropped = (ImageView) view;

dropped.setEnabled(false);

//update the text in the target view to reflect the data being dropped

Object tag = dropTarget.getTag();

//set the tag in the target view being dropped on - to the ID of the view being dropped

dropTarget.setTag(dropped.getId());

break;

case DragEvent.ACTION_DRAG_ENDED:

if(DEBUG) Log.i("drag event","ended::" + ChoiceTouchListener.offsetX+ "," + ChoiceTouchListener.offsetY);

/**

 * returning false so that goes to parentView onDrag function

 */

return false;

//break;

default:

break;

}

return true;

}

}

[/shcode]

مشکل من اینه که وقتی روی عکس کلیک میکنم و اون و میکشم توی صفحه نمیدونم چجوری بگم این عکس صندلی مخصوص این عکس خالیه

اونجور که فهمیدم باید توی کلاس ChoiceTouchListener  توی تابع onTouch یه switch گذاشتم که تو قسمت action_up  باید با استفاده از یه متد release  این کار و بکنم ولی واقعا نمیدونم چجوری باید پیاده سازی بشه

اگه کمک کنید ممنون میشم

به اشتراک گذاری این پست


لینک به ارسال
به اشتراک گذاری در سایت های دیگر

برای ارسال نظر یک حساب کاربری ایجاد کنید یا وارد حساب خود شوید

برای اینکه بتوانید نظر ارسال کنید نیاز دارید که کاربر سایت شوید

ایجاد یک حساب کاربری

برای حساب کاربری جدید در انجمن ما ثبت نام کنید. عضویت خیلی ساده است !


ثبت نام یک حساب کاربری جدید

ورود به حساب کاربری

دارای حساب کاربری هستید؟ از اینجا وارد شوید


ورود به حساب کاربری