touch drag
Добавлено: 21 окт 2013, 10:38
у меня есть такая форма)
мне нужно что бы я мог "картинку с крестиком" перетащить в красную область.
если я ее отпустил и она не в красной области , вернуть обратно ну и если попал в красную область
выполнить метод и вернуть картинку обратно.
Код layout
события брал из видео урока
на данный момент когда я нажал на изображение, то оно просто исчезает)
если я ее отпустил и она не в красной области , вернуть обратно ну и если попал в красную область
выполнить метод и вернуть картинку обратно.
Код layout
Код: Выделить всё
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="Deprecated,DisableBaselineAlignment" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="0.84"
tools:ignore="UseCompoundDrawables,InefficientWeight" >
<ImageView
android:id="@+id/shop_m_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/coins"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/shop_m_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
tools:ignore="HardcodedText" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp" >
<TextView
android:id="@+id/text_param"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="@string/text_param_age"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/text_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:text="0"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/imageSettings"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="clickKitchen"
android:src="@android:drawable/ic_menu_manage"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/imageInfo"
android:layout_width="45dp"
android:layout_height="match_parent"
android:onClick="clickKitchen"
android:src="@android:drawable/ic_menu_help"
tools:ignore="ContentDescription" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
tools:ignore="InefficientWeight" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textKitchen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="150dp"
android:layout_weight="0"
android:gravity="center"
android:text="@string/textLobby"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
</LinearLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dog"
tools:ignore="ContentDescription" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:orientation="vertical"
tools:ignore="DisableBaselineAlignment,DuplicateIds" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/titleHealth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:text="@string/titleHelath"
android:textColor="#00FF00"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/titleHangry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="@string/titleHangry"
android:textColor="#00FF00"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/titleEnergy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="@string/titleEnergy"
android:textColor="#00FF00"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/titleClean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="@string/titleClean"
android:textColor="#00FF00"
tools:ignore="HardcodedText" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textValueHealth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_weight="1"
android:text="100%"
android:textColor="#FFFFFF"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textValueHangry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="100%"
android:textColor="#FFFFFF"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/TextValueEnergy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:text="100%"
android:textColor="#FFFFFF"
tools:ignore="DuplicateIds,HardcodedText" />
<TextView
android:id="@+id/textValueClean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="100%"
android:textColor="#FFFFFF"
tools:ignore="HardcodedText" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#0000FF"
android:orientation="vertical"
tools:ignore="DuplicateIds" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="@+id/kitchenPrev"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:onClick="onClickKitchen"
android:text="<"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/kitchenImg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:onClick="onClickKitchen"
android:src="@android:drawable/ic_input_add"
tools:ignore="ContentDescription,InefficientWeight,NestedWeights" />
<Button
android:id="@+id/kitchenNext"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="75dp"
android:onClick="onClickKitchen"
android:text=">"
tools:ignore="HardcodedText" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageButton
android:id="@+id/btn_prevActivity"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:onClick="onClickKitchenActivity"
android:src="@android:drawable/ic_media_rew"
tools:ignore="ContentDescription,NestedWeights" />
<LinearLayout
android:layout_width="110dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="@+id/text_k_Count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold"
tools:ignore="InefficientWeight" />
</LinearLayout>
<ImageButton
android:id="@+id/btn_nextActivity"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClickKitchenActivity"
android:src="@android:drawable/ic_media_ff"
tools:ignore="ContentDescription" />
</LinearLayout>
</LinearLayout>
</LinearLayout>Код: Выделить всё
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kitchen);
img = (ImageView)findViewById(R.id.kitchenImg);
img.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
this.moving = true;
case MotionEvent.ACTION_MOVE:
if (moving){
x = event.getRawX() - img.getX()/2;
y = event.getRawY() - img.getY()/2;
img.setX(x);
img.setY(y);
}
break;
case MotionEvent.ACTION_UP:
this.moving = false;
break;
}
return true;
}