Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

Homework #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
138 changes: 138 additions & 0 deletions app/src/main/java/ru/ok/technopolis/basketball/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,151 @@
package ru.ok.technopolis.basketball;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Стоит сохранять аниматоры и останавливать их при уничтожении активити

import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.support.constraint.ConstraintLayout;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.annotation.SuppressLint;

public class MainActivity extends AppCompatActivity {
private ImageView ball;
private ImageView hoop;
private float beginX;
private float beginY;
public int hits = 0;
private boolean hit = false;

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ball = findViewById(R.id.activity_main_ball);
hoop = findViewById(R.id.activity_main_hoop);

final ConstraintLayout idMain = findViewById(R.id.activity_main);
Nadenokk marked this conversation as resolved.
Show resolved Hide resolved

final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
beginX = ball.getX();
beginY = ball.getY();
return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

float targetSize = (float) hoop.getWidth() / 3 - (float) ball.getWidth() *0.8f;
float xHoop = hoop.getX();
float yHoop = hoop.getY();

return AnimatorTrack((e2.getRawX() - e1.getRawX()), (e2.getRawY() - e1.getRawY()), xHoop, yHoop, targetSize);
}
});

idMain.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
});
}

public boolean AnimatorTrack(final float X, final float Y, final float xHoop, final float yHoop, final float targetSize) {
ValueAnimator anyView = ValueAnimator.ofFloat(0, 1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тоже странно название. Почему стало any?


anyView.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float x = ball.getX();
float y = ball.getY();
int rotation = 50 + (int) (Math.random() * 101);

float value = (float) valueAnimator.getAnimatedValue();
float angle = value * 3.14f;

ball.setRotation(rotation * value);
ball.setTranslationX((float) ((1 - Math.cos(angle)) * X));
ball.setTranslationY((float) (-Math.sin(angle) * Math.abs(Y)));

if ((Math.abs(xHoop - x) <= targetSize) && (Math.abs(yHoop - y) <= targetSize)) { hit = true;}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше писать на следующей строчке то, что находится внутри фигурных скобок. Можно использовать автоформатирование

}
});

anyView.addListener(new AnimatorListenerAdapter() {

@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
ball.setX(beginX);
ball.setY(beginY);
if (hits ==5 || hits ==0) {
hits =0;
SetStars(hits);
}
}

@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationStart(animation);
ball.setX(beginX);
ball.setY(beginY);
if (hit) {
hits++;
SetStars(hits);
hit = false;
}
}
});
anyView.setDuration(1200);
anyView.start();

return true;
}

public void SetStars(int hits) {
Drawable redStar = getResources().getDrawable(R.drawable.ic_grade_red_24dp);
Drawable blackStar = getResources().getDrawable(R.drawable.ic_grade_black_24dp);
ImageView star1=findViewById(R.id.image_Star1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Методы должны называться с маленьких букв
  2. Не нужно каждый раз считывать ресурсы, можно счиать родин раз
  3. Нейминг ресурсов должен включать только маленькие буквы (image_Star2 и тп)

ImageView star2=findViewById(R.id.image_Star2);
ImageView star3=findViewById(R.id.image_Star3);
ImageView star4=findViewById(R.id.image_Star4);
ImageView star5=findViewById(R.id.image_Star5);

switch (hits) {
case 1:
star1.setImageDrawable(redStar);
break;
case 2:
star2.setImageDrawable(redStar);
break;
case 3:
star3.setImageDrawable(redStar);
break;
case 4:
star4.setImageDrawable(redStar);
break;
case 5:
star5.setImageDrawable(redStar);
hits = 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем это тут?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hits не используется

break;
default:
star1.setImageDrawable(blackStar);
star2.setImageDrawable(blackStar);
star3.setImageDrawable(blackStar);
star4.setImageDrawable(blackStar);
star5.setImageDrawable(blackStar);
hits = 0;
break;
}
return;
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_grade_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#4C82FF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_grade_red_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FF5244"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
</vector>
90 changes: 90 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,99 @@
<android.support.constraint.ConstraintLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/activity_main_hoop"
android:layout_width="149dp"
android:layout_height="101dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/hoop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/activity_main_player"
android:layout_width="76dp"
android:layout_height="131dp"
android:layout_marginLeft="50dp"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

стоит добавить layout_marginStart

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавила

android:layout_marginStart="60dp"
android:layout_marginBottom="10dp"
android:src="@drawable/player"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ImageView
android:id="@+id/activity_main_ball"
android:layout_width="18dp"
android:layout_height="26dp"
android:layout_marginStart="92dp"
android:layout_marginLeft="92dp"
android:layout_marginBottom="140dp"
android:src="@drawable/ball"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<ImageView
android:id="@+id/image_Star1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_grade_black_24dp"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Посмотри ворнинг от андроид студии:

To use VectorDrawableCompat, you need to make two modifications to your project. First, set android.defaultConfig.vectorDrawables.useSupportLibrary = true in your build.gradle file, and second, use app:srcCompat instead of android:src to refer to vector drawables.

tools:alpha="4"
android:contentDescription="TODO" />

<ImageView
android:id="@+id/image_Star2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toEndOf="@+id/image_Star1"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_grade_black_24dp" />

<ImageView
android:id="@+id/image_Star3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toEndOf="@+id/image_Star2"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_grade_black_24dp" />

<ImageView
android:id="@+id/image_Star4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toEndOf="@+id/image_Star3"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_grade_black_24dp" />

<ImageView
android:id="@+id/image_Star5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toEndOf="@+id/image_Star4"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_grade_black_24dp" />


</android.support.constraint.ConstraintLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:3.4.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Mar 24 00:28:03 MSK 2019
#Sat Apr 27 16:19:44 MSK 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip