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

HW5 Vasekha #11

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-dynamic-animation:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
105 changes: 104 additions & 1 deletion app/src/main/java/ru/ok/technopolis/basketball/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,116 @@
package ru.ok.technopolis.basketball;

import android.annotation.SuppressLint;
import android.support.animation.DynamicAnimation;
import android.support.animation.FlingAnimation;
import android.support.animation.SpringAnimation;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
private GestureDetector detector;

private ImageView ballView;
private ScoreView scoreView;
private ImageView hoopView;
private Button resetButton;
Copy link

Choose a reason for hiding this comment

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

Может быть локальной переменной


private FlingAnimation flingX;
private FlingAnimation flingY;
private SpringAnimation springX;
private SpringAnimation springY;

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

detector = new GestureDetector(this, new MyGesturedListener());

ballView = findViewById(R.id.activity_main_ball);
scoreView = findViewById(R.id.activity_main_score);
hoopView = findViewById(R.id.activity_main_hoop);
resetButton = findViewById(R.id.activity_main_reset);

flingX = new FlingAnimation(ballView, DynamicAnimation.X);
flingY = new FlingAnimation(ballView, DynamicAnimation.Y);
flingY.addEndListener(animationEndListener);
springX = new SpringAnimation(ballView, DynamicAnimation.TRANSLATION_X, 0);
springY = new SpringAnimation(ballView, DynamicAnimation.TRANSLATION_Y, 0);

ballView.setOnTouchListener(touchListener);
resetButton.setOnClickListener(clickListener);
}

private DynamicAnimation.OnAnimationEndListener animationEndListener = new DynamicAnimation.OnAnimationEndListener() {
@Override
public void onAnimationEnd(DynamicAnimation dynamicAnimation, boolean b, float v, float v1) {
float ballMidX = ballView.getX() + ballView.getWidth() / 2f;
float ballMidY = ballView.getY() + ballView.getHeight() / 2f;

float targetWidth = hoopView.getWidth() / 3f;
float targetHeight = hoopView.getHeight() / 3f;

float leftTargetBorder = hoopView.getLeft() + targetWidth;
float rightTargetBorder = hoopView.getRight() - targetWidth;
float bottomTargetBorder = hoopView.getBottom() - targetHeight;
float topTargetBorder = hoopView.getTop() + targetHeight;

if (ballMidX > leftTargetBorder && ballMidX < rightTargetBorder && ballMidY > topTargetBorder && ballMidY < bottomTargetBorder) {
scoreView.incrementScore();
}
springX.start();
springY.start();
}
};

private View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
scoreView.resetScore();
springX.start();
springY.start();
}
};

private View.OnTouchListener touchListener = new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
return detector.onTouchEvent(event);
}
};

private class MyGesturedListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
flingX.setStartVelocity(velocityX);
flingY.setStartVelocity(velocityY);
flingX.setFriction(2f);
flingY.setFriction(4f);
flingX.start();
flingY.start();
return true;
}

@Override
public boolean onDown(MotionEvent event) {
return true;
}
}

@Override
public void onStop() {
super.onStop();
flingX.cancel();
flingY.cancel();
springX.cancel();
springY.cancel();
}
}
}
83 changes: 83 additions & 0 deletions app/src/main/java/ru/ok/technopolis/basketball/ScoreView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ru.ok.technopolis.basketball;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class ScoreView extends View {
private int score;
private Paint paint;
private Path path;
private float starBlockWidth;
private float starBlockMidY;
private float starBlockMidX;
private float starDiam;

public ScoreView(Context context) {
super(context, null);
}

public ScoreView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
score = 0;
path = new Path();
paint = new Paint();
}

public void incrementScore() {
score++;
if (score > 39) {
paint.setColor(getResources().getColor(R.color.colorGold));
Copy link

Choose a reason for hiding this comment

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

Цвета лучше заранее считать и тут только ставить их

} else if (score > 19) {
paint.setColor(getResources().getColor(R.color.colorSilver));
} else if (score > 9) {
paint.setColor(getResources().getColor(R.color.colorBronze));
}
invalidate();
}

public void resetScore() {
score = 0;
paint.setColor(getResources().getColor(R.color.colorBlack));
invalidate();
}

private void createStarPath(float biasX) {
path.reset();
path.moveTo(biasX + starBlockMidX - starDiam * 0.5f, starBlockMidY - starDiam * 0.16f);
path.lineTo(biasX + starBlockMidX + starDiam * 0.5f, starBlockMidY - starDiam * 0.16f);
path.lineTo(biasX + starBlockMidX - starDiam * 0.32f, starBlockMidY + starDiam * 0.45f);
path.lineTo(biasX + starBlockMidX, starBlockMidY - starDiam * 0.5f);
path.lineTo(biasX + starBlockMidX + starDiam * 0.32f, starBlockMidY + starDiam * 0.45f);
path.lineTo(biasX + starBlockMidX - starDiam * 0.5f, starBlockMidY - starDiam * 0.16f);
path.close();
}

@Override
protected void onDraw(Canvas canvas) {
float biasX = 0;
for (int i = 0; i < score; i++) {
createStarPath(biasX);
canvas.drawPath(path, paint);
if (score > 3) {
canvas.drawText("x" + score, starBlockWidth, starBlockMidY + starDiam * 0.3f, paint);
break;
}
biasX += starBlockWidth;
}
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
starBlockWidth = w / 3f;
starBlockMidX = starBlockWidth / 2f;
starBlockMidY = h / 2f;
starDiam = Math.min(starBlockMidX, starBlockMidY);
paint.setColor(getResources().getColor(R.color.colorBlack));
Copy link

Choose a reason for hiding this comment

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

Этот цвет можно поставить один раз

paint.setTextSize(starDiam);
}
}
71 changes: 71 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,76 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.constraint.Guideline
android:id="@+id/activity_main_guide_line_for_hoop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.15" />

<android.support.constraint.Guideline
android:id="@+id/activity_main_guide_line_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.95" />

<android.support.constraint.Guideline
android:id="@+id/activity_main_guide_line_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.05" />

<android.support.constraint.Guideline
android:id="@+id/activity_main_guide_line_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.95" />

<ImageView
android:id="@+id/activity_main_hoop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hoop_image_desc"
android:src="@drawable/hoop"
app:layout_constraintEnd_toEndOf="@id/activity_main_guide_line_right"
app:layout_constraintTop_toTopOf="@id/activity_main_guide_line_for_hoop" />

<ru.ok.technopolis.basketball.ScoreView
android:id="@+id/activity_main_score"
android:layout_width="150dp"
android:layout_height="80dp"
app:layout_constraintStart_toStartOf="@id/activity_main_guide_line_left"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/activity_main_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/player_image_desc"
android:src="@drawable/player"
app:layout_constraintBottom_toBottomOf="@id/activity_main_guide_line_bottom"
app:layout_constraintStart_toStartOf="@id/activity_main_guide_line_left" />

<ImageView
android:id="@+id/activity_main_ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/ball_image_desc"
android:src="@drawable/ball"
app:layout_constraintBottom_toTopOf="@id/activity_main_player"
app:layout_constraintEnd_toEndOf="@id/activity_main_player"
app:layout_constraintStart_toStartOf="@id/activity_main_player" />

<Button
android:id="@+id/activity_main_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/reset_button_desc"
android:text="@string/reset_button"
app:layout_constraintBottom_toBottomOf="@id/activity_main_guide_line_bottom"
app:layout_constraintEnd_toEndOf="@id/activity_main_guide_line_right" />

</android.support.constraint.ConstraintLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorBlack">#000000</color>
<color name="colorBronze">#9C6D27</color>
<color name="colorSilver">#70878A</color>
<color name="colorGold">#FFC107</color>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<resources>
<string name="app_name">basketball_homework</string>
<string name="reset_button">reset</string>
<string name="hoop_image_desc">this is the hoop</string>
<string name="ball_image_desc">this is the ball</string>
<string name="player_image_desc">this is the player</string>
<string name="reset_button_desc">click to reset the game</string>
</resources>