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

Сделал дз. #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ public class FieldView extends View {
private Float currentY;
private Paint dotPaint = new Paint();
private Path dotPath = new Path();
private static final int DEFAULT_POINT_COLOR = Color.BLACK;
private static final int DEFAULT_POINT_STROKE_WIDTH_DP = 5;
private static final int DEFAULT_POINT_RADIUS_DP = 5;
private int pointRadius;

private static final int DEFAULT_POINT_COLOR = Color.BLACK;
public FieldView(Context context) {
super(context);
}
Expand All @@ -33,8 +30,8 @@ public FieldView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int pointStrokeWidthFromAttr = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_POINT_STROKE_WIDTH_DP, displayMetrics) + 0.5f);
int pointRadiusFromAttr = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_POINT_RADIUS_DP, displayMetrics) + 0.5f);
int pointStrokeWidthFromAttr = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, R.dimen.default_point_stroke_width, displayMetrics) + 0.5f);
Copy link

Choose a reason for hiding this comment

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

Что бы каждый раз не делать +0.5f и не забывать про это, лучше сделать метод для перевода из dp в px

int pointRadiusFromAttr = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, R.dimen.default_point_radius, displayMetrics) + 0.5f);
int pointColorFromAttr = DEFAULT_POINT_COLOR;
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FieldView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
public class ScoreView extends View {

private static final int DEFAULT_MARK_COLOR = Color.BLACK;
private static final int DEFAULT_MARK_STROKE_WIDTH_DP = 5;
private Path markPath = new Path();
private Paint markPaint = new Paint();

Expand All @@ -28,7 +27,7 @@ public ScoreView(Context context) {
public ScoreView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int markStrokeWidthFromAttr = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_MARK_STROKE_WIDTH_DP, displayMetrics) + 0.5f);
int markStrokeWidthFromAttr = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, R.dimen.default_point_stroke_width, displayMetrics) + 0.5f);
int markColorFromAttr = DEFAULT_MARK_COLOR;
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScoreView);
Expand All @@ -52,7 +51,6 @@ public void setMark(int mark) {
int rightPadding = getPaddingRight();
int topPadding = getPaddingTop();
int bottomPadding = getPaddingBottom();
int w = getWidth();
int width = getWidth() / 5 - leftPadding - rightPadding;
int xStart = width / 2 + leftPadding;
int yStart = getHeight() - bottomPadding;
Expand Down
22 changes: 18 additions & 4 deletions app/src/main/java/ru/ok/technopolis/basketball/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import android.animation.ValueAnimator;
Copy link

Choose a reason for hiding this comment

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

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

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
Expand All @@ -29,6 +29,7 @@ public class MainActivity extends AppCompatActivity implements View.OnTouchListe
private Context context;
private float G = 9800;
private float maxDistance;
ValueAnimator animator;
Copy link

Choose a reason for hiding this comment

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

Можно сделать private


@SuppressLint("ClickableViewAccessibility")
@Override
Expand Down Expand Up @@ -79,8 +80,12 @@ void calculateInitialCondition(float x0, float y0, float vx0, float vy0) {
}

void initialiseAnimation() {
if (isScored(startPoint.getX(), fieldView.getHeight() - startPoint.getY())) {
Toast.makeText(context, R.string.cheater, Toast.LENGTH_SHORT).show();
Copy link

Choose a reason for hiding this comment

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

Интересное решение =)

return;
}
float timeStart = 0;
ValueAnimator animator = ValueAnimator.ofFloat(timeStart, motionOfProjectile.getTimeFlight());
animator = ValueAnimator.ofFloat(timeStart, motionOfProjectile.getTimeFlight());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Expand All @@ -89,13 +94,16 @@ public void onAnimationUpdate(ValueAnimator animation) {
ball.setX(currentX - ball.getWidth() / 2);
ball.setY(currentY - ball.getHeight() / 2);
if (isScored(currentX, currentY)) {
Toast.makeText(context, R.string.scored, Toast.LENGTH_SHORT).show();
Toast.makeText(context, R.string.goll, Toast.LENGTH_SHORT).show();
maxDistance = Point.getDistance(player.getWidth(), fieldView.getHeight() - player.getHeight(), currentX, currentY);
startPoint.changeVerticalAxisTo(fieldView.getHeight());
scoreView.setMark(calculateScore(maxDistance, startPoint.getDistance(currentX, currentY)));

}
fieldView.drawDot(currentX, currentY);
if (animation.getAnimatedValue().equals(motionOfProjectile.getTimeFlight())) {
Toast.makeText(context, R.string.not_goll, Toast.LENGTH_SHORT).show();
}
}
});
animator.setInterpolator(new LinearInterpolator());
Expand All @@ -105,13 +113,19 @@ public void onAnimationUpdate(ValueAnimator animation) {

private boolean isScored(float x, float y) {
if (x > (hoop.getLeft() + hoop.getWidth() / 2 - hoop.getWidth() / 6) && x < (hoop.getLeft() + hoop.getWidth() / 2 + hoop.getWidth() / 6)) {
if (y > (hoop.getTop() + hoop.getHeight() / 2) && y < (hoop.getTop() + hoop.getHeight() / 2 + hoop.getHeight() / 3)) {
if (y > (hoop.getTop() + hoop.getHeight() / 3) && y < (hoop.getTop() + hoop.getHeight() / 2 + hoop.getHeight() / 3)) {
return true;
}
}
return false;
}

@Override
protected void onPause() {
super.onPause();
animator.cancel();
Copy link

Choose a reason for hiding this comment

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

animator стоит проверить на нул. Иначе, если не делать бросков и свернуть приложение будет креш

}

private int calculateScore(float maxDist, float dist) {
int score = (int) (dist / (maxDist / 4)) + 1;
return score > 5 ? 5 : score;
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/ru/ok/technopolis/basketball/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public class Point {
private float vx;
private float vy;

Point(float coordinate) {
public Point(float coordinate) {
this(coordinate, coordinate);
}

Point(float x, float y) {
public Point(float x, float y) {
this(x, y, 0, 0);
}

Point(float x, float y, float vx, float vy) {
public Point(float x, float y, float vx, float vy) {
this.x = x;
this.y = y;
this.vx = vx;
Expand Down
82 changes: 40 additions & 42 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout 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/frame_layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout

<ru.ok.technopolis.basketball.CustomView.FieldView
android:id="@+id/field_view_basketball_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout_container">
<ru.ok.technopolis.basketball.CustomView.FieldView
android:id="@+id/field_view_basketball_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:padding="10dp"
app:pointColor="@android:color/holo_orange_dark"
app:pointStrokeWidth="2dp"
app:pointRadius="5dp"
/>
<ru.ok.technopolis.basketball.CustomView.ScoreView
android:id="@+id/score_view_score"
android:layout_width="200dp"
android:layout_height="50dp"
android:padding="5dp"
app:markColor="@android:color/holo_red_dark"
app:markStrokeWidth="2dp"
android:layout_gravity="top|start"
/>
<ImageView
android:id="@+id/image_ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ball"
android:layout_gravity="bottom|center_horizontal"/>
<ImageView
android:id="@+id/image_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/player"
android:layout_gravity="bottom|start"/>
<ImageView
android:id="@+id/image_hoop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/hoop"
android:layout_gravity="top|end"/>
</FrameLayout>
android:background="@android:color/white"
android:padding="10dp"
app:pointColor="@android:color/holo_orange_dark"
app:pointRadius="5dp"
app:pointStrokeWidth="2dp" />

<ru.ok.technopolis.basketball.CustomView.ScoreView
android:id="@+id/score_view_score"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="top|start"
android:padding="5dp"
app:markColor="@android:color/holo_red_dark"
app:markStrokeWidth="2dp" />

<ImageView
android:id="@+id/image_ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:src="@drawable/ball" />

<ImageView
android:id="@+id/image_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:src="@drawable/player" />

</android.support.constraint.ConstraintLayout>
<ImageView
android:id="@+id/image_hoop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:src="@drawable/hoop" />
</FrameLayout>
5 changes: 5 additions & 0 deletions app/src/main/res/values/dimen.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="default_point_stroke_width">5dp</dimen>
<dimen name="default_point_radius">5dp</dimen>
</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<resources>
<string name="app_name">basketball_homework</string>
<string name="scored">Забил гол!</string>
<string name="goll">Забил гол!</string>
<string name="not_goll">Не забил.</string>
<string name="cheater">Играй честно.</string>
</resources>
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.1'

// 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
#Tue May 21 19:39:42 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