You are on page 1of 6

acticity

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/
res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mac.test_tetris2.MainActivity"
android:background="@color/colorPrimaryDark">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="88dp"
android:layout_marginStart="88dp"
android:layout_marginTop="75dp"
app:srcCompat="@drawable/caro2" />

<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/caro2"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp" />

<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/baguete"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/imageView5"
android:layout_toEndOf="@+id/imageView5" />

<ImageView
android:id="@+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/baguete"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/imageView6"
android:layout_toEndOf="@+id/imageView6" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/baguete"
android:layout_alignBottom="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:layout_marginBottom="11dp" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/baguete"
android:layout_alignTop="@+id/imageView"
android:layout_alignRight="@+id/imageView8"
android:layout_alignEnd="@+id/imageView8"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp" />

<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/caro12"
android:layout_alignTop="@+id/imageView5"
android:layout_alignLeft="@+id/imageView5"
android:layout_alignStart="@+id/imageView5" />

<ImageView
android:id="@+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView5"
android:layout_alignStart="@+id/imageView5"
android:layout_below="@+id/imageView4"
app:srcCompat="@drawable/caro12" />
</RelativeLayout>

MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements
View.OnTouchListener{
ImageView iv1,iv2,iv3,iv11,iv21,iv31,iv01,iv02;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

iv1=(ImageView) findViewById(R.id.imageView);
iv11=(ImageView) findViewById(R.id.imageView5);
iv2=(ImageView) findViewById(R.id.imageView2);
iv21=(ImageView) findViewById(R.id.imageView6);

iv3=(ImageView) findViewById(R.id.imageView3);
iv31=(ImageView) findViewById(R.id.imageView8);

iv01=(ImageView) findViewById(R.id.imageView4);
iv02=(ImageView) findViewById(R.id.imageView7);

iv2.setVisibility(View.INVISIBLE);
iv3.setVisibility(View.INVISIBLE);

iv11.setVisibility(View.INVISIBLE);
iv21.setVisibility(View.INVISIBLE);
iv31.setVisibility(View.INVISIBLE);

iv01.setVisibility(View.INVISIBLE);
iv02.setVisibility(View.INVISIBLE);

iv1.animate().translationY(1400).setDuration(10000);

iv2.animate().translationY(700).setDuration(10000);

iv3.animate().translationY(400).setDuration(10000);

iv1.setOnTouchListener(this);
iv2.setOnTouchListener(this);
iv3.setOnTouchListener(this);

}
float x = 0.0f;
int k=0;
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(arg0==iv1){
if (arg1.getAction() == MotionEvent.ACTION_MOVE) {
if(iv1.getY()>600){
iv1.setVisibility(View.INVISIBLE);
iv11.setVisibility(View.VISIBLE);
iv2.setVisibility(View.VISIBLE);

}
x = arg1.getRawX() -iv1.getHeight() ;
iv1.setX(x);

}}
if(arg0==iv2) {
if (arg1.getAction() == MotionEvent.ACTION_MOVE) {

if (iv2.getY() > 600) {


iv2.setVisibility(View.INVISIBLE);
iv21.setVisibility(View.VISIBLE);
iv3.setVisibility(View.VISIBLE);

}
x = arg1.getRawX() - iv2.getHeight();
iv2.setX(x);
}

if(arg0==iv3) {
if (arg1.getAction() == MotionEvent.ACTION_MOVE) {

if (iv3.getY() > 600) {


iv3.setVisibility(View.INVISIBLE);
iv31.setVisibility(View.VISIBLE);

i = 1;
Thread th = new Thread(new Runnable() {

public void run() {


while (i < 5) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (i == 1) {

iv11.setVisibility(View.INVISIBLE);

iv21.setVisibility(View.INVISIBLE);

iv31.setVisibility(View.INVISIBLE);

if (i == 2) {

iv01.setVisibility(View.VISIBLE);
}
if (i == 3) {

iv01.setVisibility(View.INVISIBLE);

iv02.setVisibility(View.VISIBLE);
}

});
try {
Thread.sleep(500);
} catch (InterruptedException
e) {
e.printStackTrace();
}
i++;

}
}
});
th.start();

}
x = arg1.getRawX() - iv3.getHeight();
iv3.setX(x);
}

}
return true;
}