From cb690ecbf14c732cf2da5d53d18b6b5f223a3460 Mon Sep 17 00:00:00 2001 From: oxovu Date: Thu, 28 Mar 2019 14:29:58 +0300 Subject: [PATCH 1/2] CustomView --- .../technopolis/basketball/MainActivity.java | 20 +++- .../ok/technopolis/basketball/StarView.java | 98 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 39 +++++++- app/src/main/res/values/attrs.xml | 7 ++ 4 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ru/ok/technopolis/basketball/StarView.java create mode 100644 app/src/main/res/values/attrs.xml 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..2dbf7c4 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,31 @@ package ru.ok.technopolis.basketball; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.animation.DynamicAnimation; +import android.support.animation.FlingAnimation; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.ImageView; public class MainActivity extends AppCompatActivity { + private ImageView ball; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + ball = findViewById(R.id.ball); + } + + + public void flingIt(View view) { + FlingAnimation flingAnimation_x = new FlingAnimation(ball, DynamicAnimation.X); + FlingAnimation flingAnimation_y = new FlingAnimation(ball, DynamicAnimation.Y); + flingAnimation_x.setStartVelocity(500f); + flingAnimation_y.setStartVelocity(-500f); + flingAnimation_x.setFriction(0.5f); + flingAnimation_y.setFriction(0.5f); + flingAnimation_x.start(); + flingAnimation_y.start(); } } 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..db71fec --- /dev/null +++ b/app/src/main/java/ru/ok/technopolis/basketball/StarView.java @@ -0,0 +1,98 @@ +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; + + 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; + } + + @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..7e777a5 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" + android:onClick="flingIt" + > + + + + + + + + \ 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 From 3bbd4c30410991abe91e2475255a536ffae05047 Mon Sep 17 00:00:00 2001 From: Vladislava Vasileva Date: Thu, 18 Jul 2019 14:41:10 +0300 Subject: [PATCH 2/2] works --- app/build.gradle | 2 + .../technopolis/basketball/MainActivity.java | 78 ++++++++++++++++--- .../ok/technopolis/basketball/StarView.java | 10 +++ app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/values/strings.xml | 1 + 5 files changed, 81 insertions(+), 12 deletions(-) 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 2dbf7c4..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,31 +1,87 @@ package ru.ok.technopolis.basketball; import android.os.Bundle; -import android.support.animation.DynamicAnimation; -import android.support.animation.FlingAnimation; 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; + } + } - public void flingIt(View view) { - FlingAnimation flingAnimation_x = new FlingAnimation(ball, DynamicAnimation.X); - FlingAnimation flingAnimation_y = new FlingAnimation(ball, DynamicAnimation.Y); - flingAnimation_x.setStartVelocity(500f); - flingAnimation_y.setStartVelocity(-500f); - flingAnimation_x.setFriction(0.5f); - flingAnimation_y.setFriction(0.5f); - flingAnimation_x.start(); - flingAnimation_y.start(); + 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 index db71fec..c05029a 100644 --- a/app/src/main/java/ru/ok/technopolis/basketball/StarView.java +++ b/app/src/main/java/ru/ok/technopolis/basketball/StarView.java @@ -15,6 +15,7 @@ public class StarView extends View { 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; @@ -37,6 +38,15 @@ public StarView(Context context, @Nullable AttributeSet attrs) { 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); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 7e777a5..6d78cbe 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,10 +5,10 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" - android:onClick="flingIt" > basketball_homework + You win! Let\'s play again!