This repository has been archived by the owner on Mar 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
HW5 Vasekha #11
Open
irvasek
wants to merge
3
commits into
polis-vk:master
Choose a base branch
from
irvasek:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
HW5 Vasekha #11
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 104 additions & 1 deletion
105
app/src/main/java/ru/ok/technopolis/basketball/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
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
83
app/src/main/java/ru/ok/technopolis/basketball/ScoreView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Этот цвет можно поставить один раз |
||
paint.setTextSize(starDiam); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Может быть локальной переменной