0% found this document useful (0 votes)
32 views3 pages

Relativelayout: Activity

The document contains code for an Android activity layout and Java code for a pong game. The layout defines three image views - two for the paddles on the left and right sides, and one for the ball in the center. The Java code sets up an activity class that implements touch listening on the left paddle, and animates the ball back and forth between the paddles over 10 seconds. Touch moves allow the left paddle to move up and down, and the ball bounces off the paddles.

Uploaded by

omar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Relativelayout: Activity

The document contains code for an Android activity layout and Java code for a pong game. The layout defines three image views - two for the paddles on the left and right sides, and one for the ball in the center. The Java code sets up an activity class that implements touch listening on the left paddle, and animates the ball back and forth between the paddles over 10 seconds. Touch moves allow the left paddle to move up and down, and the ball bounces off the paddles.

Uploaded by

omar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

activity

<?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_pong.MainActivity"
android:background="@color/colorPrimaryDark"
>

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

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="19dp"
app:srcCompat="@drawable/bar"
android:layout_alignTop="@+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="12dp"
android:layout_marginEnd="12dp" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/bal"
android:layout_alignTop="@+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp" />

</RelativeLayout>

MainActivity
package com.example.mac.test_pong;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements


View.OnTouchListener{
ImageView iv1,iv2,iv3;
TextView txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1=(ImageView) findViewById(R.id.imageView);
iv2=(ImageView) findViewById(R.id.imageView2);
iv3=(ImageView) findViewById(R.id.imageView3);

iv1.setOnTouchListener(this);
iv3.animate().translationX(1400).setDuration(10000);

float y = 0.0f;
int k=0;
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_MOVE) {
y = arg1.getRawY() -iv1.getHeight() ;

iv1.setY(y);

}
if(iv3.getX()>iv2.getX()){

iv3.animate().translationX(-1400).setDuration(10000);
}

return true;
}
}

You might also like