diff --git a/app/build.gradle b/app/build.gradle index fb2d42d..7df3b95 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,4 +25,6 @@ dependencies { 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' + implementation 'com.android.support:support-dynamic-animation:28.0.0' + implementation 'org.jetbrains:annotations-java5:15.0' } diff --git a/app/src/main/java/ru/ok/technopolis/basketball/MainActivity.java b/app/src/main/java/ru/ok/technopolis/basketball/MainActivity.java index 8f71141..48325cd 100644 --- a/app/src/main/java/ru/ok/technopolis/basketball/MainActivity.java +++ b/app/src/main/java/ru/ok/technopolis/basketball/MainActivity.java @@ -1,13 +1,87 @@ package ru.ok.technopolis.basketball; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.view.MotionEvent; +import android.view.View; +import android.widget.ImageView; +import android.widget.Toast; public class MainActivity extends AppCompatActivity { + private ImageView ball; + private StarView stars; + private ImageView hoop; + private float dx = 0; + private float dy = 0; + private float x = 0; + private float y = 0; + private int starsCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + ball = findViewById(R.id.ball); + ball.setOnTouchListener(new BallOnTouchListener()); + stars = findViewById(R.id.stars); + hoop = findViewById(R.id.hoop); + starsCount = 0; + stars.setCount(starsCount); + + } + + private class BallOnTouchListener implements View.OnTouchListener { + @Override + public boolean onTouch(View view, MotionEvent motionEvent) { + switch (motionEvent.getAction()) { + case MotionEvent.ACTION_DOWN: + dx = view.getX() - motionEvent.getRawX(); + dy = view.getY() - motionEvent.getRawY(); + x = motionEvent.getRawX(); + y = motionEvent.getRawY(); + break; + case MotionEvent.ACTION_MOVE: + view.animate() + .x(motionEvent.getRawX() + dx) + .y(motionEvent.getRawY() + dy) + .setDuration(0) + .start(); + break; + case MotionEvent.ACTION_UP: + float finalX = view.getX() - (motionEvent.getRawX() - x) * 12f; + float finalY = view.getY() - (motionEvent.getRawY() - y) * 8f; + view.animate() + .x(finalX) + .y(finalY) + .setDuration(400) + .withEndAction(new Runnable() { + @Override + public void run() { + ball.setY(y + dy); + ball.setX(x + dx); + } + }).start(); + if (finalX > hoop.getX() && finalY < hoop.getY() + hoop.getHeight() + && finalX < hoop.getX() + hoop.getWidth() && finalY > 0) { + starsCount += 1; + stars.setCount(starsCount); + if (starsCount > StarView.MAX_STAR_COUNT) { + Toast toast = Toast.makeText(getApplicationContext(), + getString(R.string.win), Toast.LENGTH_SHORT); + toast.show(); + restart(); + } + } + break; + default: + return false; + } + return true; + } + } + + private void restart() { + starsCount = 0; + stars.setCount(starsCount); } } diff --git a/app/src/main/java/ru/ok/technopolis/basketball/StarView.java b/app/src/main/java/ru/ok/technopolis/basketball/StarView.java new file mode 100644 index 0000000..c05029a --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/basketball/StarView.java @@ -0,0 +1,108 @@ +package ru.ok.technopolis.basketball; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Path; +import android.graphics.Point; +import android.support.annotation.Nullable; +import android.util.AttributeSet; +import android.view.View; + +public class StarView extends View { + public static final int STAR_WIDTH = 50; + public static final int STAR_HEIGHT = STAR_WIDTH; + public static final int DEFAULT_STAR_COLOR = 0xFFFFC100; + public static final int DEFAULT_STAR_COUNT = 0; + public static final int MAX_STAR_COUNT = 5; + + private int starCount; + private int starColor; + + public StarView(Context context) { + super(context); + } + + public StarView(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + int starColorAttr = DEFAULT_STAR_COLOR; + int starCountAttr = DEFAULT_STAR_COUNT; + if (attrs != null) { + TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.StarView); + starColorAttr = typedArray.getColor(R.styleable.StarView_color, starColorAttr); + starCountAttr = typedArray.getInt(R.styleable.StarView_count, starCountAttr); + typedArray.recycle(); + } + starColor = starColorAttr; + starCount = starCountAttr; + } + + public void setCount(int starCount){ + this.starCount = starCount; + this.invalidate(); + } + + public int getCount(){ + return starCount; + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int height = MeasureSpec.getSize(heightMeasureSpec); + int leftPadding = getPaddingLeft(); + int rightPadding = getPaddingRight(); + int width = (STAR_WIDTH + leftPadding + rightPadding) * starCount; + width = resolveSize(width, widthMeasureSpec); + setMeasuredDimension(width, height); + } + + @Override + protected void onDraw(Canvas canvas) { + if (starCount == 0) { + return; + } + int paddingTop = getPaddingTop(); + int measuredHeight = getMeasuredHeight() - paddingTop - getPaddingBottom(); + Paint paint = new Paint(); + Path path = new Path(); + + for (int i = 0; i < starCount; i++) { + int size = measuredHeight; + int leftPadding = size * i + 10; + + paint.setStrokeWidth(1); + paint.setColor(starColor); + paint.setStyle(Paint.Style.FILL_AND_STROKE); + paint.setAntiAlias(true); + + Point a = new Point(leftPadding + size / 2, 0); + Point b = new Point(leftPadding + size * 2 / 3, size / 3); + Point c = new Point(leftPadding + size, size / 3); + Point d = new Point(leftPadding + size * 4 / 5, size * 3 / 5); + Point e = new Point(leftPadding + size * 6 / 7, size); + Point f = new Point(leftPadding + size / 2, size * 4 / 5); + Point g = new Point(leftPadding + size - size * 6 / 7, size); + Point h = new Point(leftPadding + size - size * 4 / 5, size * 3 / 5); + Point j = new Point(leftPadding, size / 3); + Point k = new Point(leftPadding + size - size * 2 / 3, size / 3); + + + path.setFillType(Path.FillType.WINDING); + path.lineTo(a.x, a.y); + path.lineTo(b.x, b.y); + path.lineTo(c.x, c.y); + path.lineTo(d.x, d.y); + path.lineTo(e.x, e.y); + path.lineTo(f.x, f.y); + path.lineTo(g.x, g.y); + path.lineTo(h.x, h.y); + path.lineTo(j.x, j.y); + path.lineTo(k.x, k.y); + path.lineTo(a.x, a.y); + path.close(); + + } + canvas.drawPath(path, paint); + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 037bef1..6d78cbe 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,7 +4,44 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MainActivity"> + tools:context=".MainActivity" + > + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml new file mode 100644 index 0000000..c13b7dd --- /dev/null +++ b/app/src/main/res/values/attrs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fa967e1..020c613 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ basketball_homework + You win! Let\'s play again!