Vote count:
0
I'm creating a small application for practice some in Android programming. It's a kind of puzzle game like this:
I have a following issue: when I slide a button left or right in x property all is OK. I can move each button frequently left and right. The problem begins when I move the button down or up in y property. I can only move the button once and next it will be no longer available for the onTouchLister. It become immobile in any direction and any action isn't performed. Here is my java activity file and the layout xml.
MainActivity.java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
class MainActivity extends ActionBarActivity {
int DX,DY;
View[] views = new View[16];
LinearLayout mainScreen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainScreen = (LinearLayout)findViewById(R.id.main_screen);
View.OnTouchListener mTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event){
float x1=0, x2, y1=0, y2, dx, dy;
// MyPoint associatedPoint = blankPoint;
switch(event.getActionMasked()) {
case(MotionEvent.ACTION_DOWN):
x1 = event.getX();
y1 = event.getY();
break;
case(MotionEvent.ACTION_UP): {
x2 = event.getX();
y2 = event.getY();
dx = x2-x1;
dy = y2-y1;
if(Math.abs(dx) > Math.abs(dy)) {
if(dx>0){
v.animate().xBy(DX);}
else{
v.animate().xBy(-DX);}
} else {
if(dy>0){
v.animate().yBy(DY);}
else{
v.animate().yBy(-DY);}
}
}
}
return true;
}
};
views[0] = findViewById(R.id.button1);
views[1] = findViewById(R.id.button2);
views[2] = findViewById(R.id.button3);
views[3] = findViewById(R.id.button4);
views[4] = findViewById(R.id.button5);
views[5] = findViewById(R.id.button6);
views[6] = findViewById(R.id.button7);
views[7] = findViewById(R.id.button8);
views[8] = findViewById(R.id.button9);
views[9] = findViewById(R.id.button10);
views[10] = findViewById(R.id.button11);
views[11] = findViewById(R.id.button12);
views[12] = findViewById(R.id.button13);
views[13] = findViewById(R.id.button14);
views[14] = findViewById(R.id.button15);
views[15] = findViewById(R.id.space);
for(int i=0; i<15; i++)
views[i].setOnTouchListener(mTouchListener);
}
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int offsetY = displayMetrics.heightPixels - mainScreen.getMeasuredHeight();
int[] coord0 = new int[2];
int[] coord1 = new int[2];
int[] coord4 = new int[2];
views[0].getLocationOnScreen(coord0);
views[1].getLocationOnScreen(coord1);
views[4].getLocationOnScreen(coord4);
DX = coord1[0] - coord0[0];
DY = coord4[1] - coord0[1];
}
}
activity_main.xml
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:collapseColumns="4"
android:orientation="vertical"
android:clipChildren="false"
android:id="@+id/main_screen"
android:clipToPadding="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clipChildren="false"
android:clipToPadding="false"
android:id="@+id/row1">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b1"
android:id="@+id/button1"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b2"
android:id="@+id/button2"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b3"
android:id="@+id/button3"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b4"
android:id="@+id/button4"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clipChildren="false"
android:clipToPadding="false"
android:id="@+id/row2">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b5"
android:id="@+id/button5"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b6"
android:id="@+id/button6"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b7"
android:id="@+id/button7"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b8"
android:id="@+id/button8"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clipChildren="false"
android:clipToPadding="false"
android:id="@+id/row3">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b9"
android:id="@+id/button9"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b10"
android:id="@+id/button10"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b11"
android:id="@+id/button11"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b12"
android:id="@+id/button12"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clipChildren="false"
android:clipToPadding="false"
android:id="@+id/row4">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b13"
android:id="@+id/button13"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b14"
android:id="@+id/button14"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/b15"
android:id="@+id/button15"
android:layout_weight="1" />
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/space"
android:visibility="invisible"/>
</LinearLayout>
I have really no idea what cause this problem. Please give me a small prompt on what may cause this issue. Thanks in advance
asked 1 min ago
Error in property animation
Aucun commentaire:
Enregistrer un commentaire