-
Notifications
You must be signed in to change notification settings - Fork 17
Homework #6
base: master
Are you sure you want to change the base?
Homework #6
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,151 @@ | ||
package ru.ok.technopolis.basketball; | ||
|
||
import android.graphics.drawable.Drawable; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.animation.Animator; | ||
import android.animation.AnimatorListenerAdapter; | ||
import android.animation.ValueAnimator; | ||
import android.support.constraint.ConstraintLayout; | ||
import android.view.GestureDetector; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
import android.widget.ImageView; | ||
import android.annotation.SuppressLint; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
private ImageView ball; | ||
private ImageView hoop; | ||
private float beginX; | ||
private float beginY; | ||
public int hits = 0; | ||
private boolean hit = false; | ||
|
||
@SuppressLint("ClickableViewAccessibility") | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
ball = findViewById(R.id.activity_main_ball); | ||
hoop = findViewById(R.id.activity_main_hoop); | ||
|
||
final ConstraintLayout idMain = findViewById(R.id.activity_main); | ||
Nadenokk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { | ||
@Override | ||
public boolean onDown(MotionEvent e) { | ||
beginX = ball.getX(); | ||
beginY = ball.getY(); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | ||
|
||
float targetSize = (float) hoop.getWidth() / 3 - (float) ball.getWidth() *0.8f; | ||
float xHoop = hoop.getX(); | ||
float yHoop = hoop.getY(); | ||
|
||
return AnimatorTrack((e2.getRawX() - e1.getRawX()), (e2.getRawY() - e1.getRawY()), xHoop, yHoop, targetSize); | ||
} | ||
}); | ||
|
||
idMain.setOnTouchListener(new View.OnTouchListener() { | ||
@Override | ||
public boolean onTouch(View v, MotionEvent event) { | ||
gestureDetector.onTouchEvent(event); | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
public boolean AnimatorTrack(final float X, final float Y, final float xHoop, final float yHoop, final float targetSize) { | ||
ValueAnimator anyView = ValueAnimator.ofFloat(0, 1); | ||
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. Тоже странно название. Почему стало any? |
||
|
||
anyView.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { | ||
@Override | ||
public void onAnimationUpdate(ValueAnimator valueAnimator) { | ||
float x = ball.getX(); | ||
float y = ball.getY(); | ||
int rotation = 50 + (int) (Math.random() * 101); | ||
|
||
float value = (float) valueAnimator.getAnimatedValue(); | ||
float angle = value * 3.14f; | ||
|
||
ball.setRotation(rotation * value); | ||
ball.setTranslationX((float) ((1 - Math.cos(angle)) * X)); | ||
ball.setTranslationY((float) (-Math.sin(angle) * Math.abs(Y))); | ||
|
||
if ((Math.abs(xHoop - x) <= targetSize) && (Math.abs(yHoop - y) <= targetSize)) { hit = true;} | ||
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. Лучше писать на следующей строчке то, что находится внутри фигурных скобок. Можно использовать автоформатирование |
||
} | ||
}); | ||
|
||
anyView.addListener(new AnimatorListenerAdapter() { | ||
|
||
@Override | ||
public void onAnimationStart(Animator animation) { | ||
super.onAnimationStart(animation); | ||
ball.setX(beginX); | ||
ball.setY(beginY); | ||
if (hits ==5 || hits ==0) { | ||
hits =0; | ||
SetStars(hits); | ||
} | ||
} | ||
|
||
@Override | ||
public void onAnimationEnd(Animator animation) { | ||
super.onAnimationStart(animation); | ||
ball.setX(beginX); | ||
ball.setY(beginY); | ||
if (hit) { | ||
hits++; | ||
SetStars(hits); | ||
hit = false; | ||
} | ||
} | ||
}); | ||
anyView.setDuration(1200); | ||
anyView.start(); | ||
|
||
return true; | ||
} | ||
|
||
public void SetStars(int hits) { | ||
Drawable redStar = getResources().getDrawable(R.drawable.ic_grade_red_24dp); | ||
Drawable blackStar = getResources().getDrawable(R.drawable.ic_grade_black_24dp); | ||
ImageView star1=findViewById(R.id.image_Star1); | ||
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.
|
||
ImageView star2=findViewById(R.id.image_Star2); | ||
ImageView star3=findViewById(R.id.image_Star3); | ||
ImageView star4=findViewById(R.id.image_Star4); | ||
ImageView star5=findViewById(R.id.image_Star5); | ||
|
||
switch (hits) { | ||
case 1: | ||
star1.setImageDrawable(redStar); | ||
break; | ||
case 2: | ||
star2.setImageDrawable(redStar); | ||
break; | ||
case 3: | ||
star3.setImageDrawable(redStar); | ||
break; | ||
case 4: | ||
star4.setImageDrawable(redStar); | ||
break; | ||
case 5: | ||
star5.setImageDrawable(redStar); | ||
hits = 0; | ||
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. Зачем это тут? 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. hits не используется |
||
break; | ||
default: | ||
star1.setImageDrawable(blackStar); | ||
star2.setImageDrawable(blackStar); | ||
star3.setImageDrawable(blackStar); | ||
star4.setImageDrawable(blackStar); | ||
star5.setImageDrawable(blackStar); | ||
hits = 0; | ||
break; | ||
} | ||
return; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#4C82FF" | ||
android:viewportHeight="24.0" android:viewportWidth="24.0" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="#FF000000" android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/> | ||
</vector> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<vector android:height="24dp" android:tint="#FF5244" | ||
android:viewportHeight="24.0" android:viewportWidth="24.0" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="#FF000000" android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/> | ||
</vector> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,99 @@ | |
<android.support.constraint.ConstraintLayout 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/activity_main" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".MainActivity"> | ||
|
||
<ImageView | ||
android:id="@+id/activity_main_hoop" | ||
android:layout_width="149dp" | ||
android:layout_height="101dp" | ||
android:layout_marginTop="10dp" | ||
android:layout_marginRight="10dp" | ||
android:src="@drawable/hoop" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<ImageView | ||
android:id="@+id/activity_main_player" | ||
android:layout_width="76dp" | ||
android:layout_height="131dp" | ||
android:layout_marginLeft="50dp" | ||
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. стоит добавить layout_marginStart 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. Добавила |
||
android:layout_marginStart="60dp" | ||
android:layout_marginBottom="10dp" | ||
android:src="@drawable/player" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" /> | ||
|
||
<ImageView | ||
android:id="@+id/activity_main_ball" | ||
android:layout_width="18dp" | ||
android:layout_height="26dp" | ||
android:layout_marginStart="92dp" | ||
android:layout_marginLeft="92dp" | ||
android:layout_marginBottom="140dp" | ||
android:src="@drawable/ball" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" /> | ||
|
||
<ImageView | ||
android:id="@+id/image_Star1" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="16dp" | ||
android:layout_marginLeft="16dp" | ||
android:layout_marginTop="8dp" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:srcCompat="@drawable/ic_grade_black_24dp" | ||
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. Посмотри ворнинг от андроид студии:
|
||
tools:alpha="4" | ||
android:contentDescription="TODO" /> | ||
|
||
<ImageView | ||
android:id="@+id/image_Star2" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginLeft="8dp" | ||
android:layout_marginTop="8dp" | ||
app:layout_constraintStart_toEndOf="@+id/image_Star1" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:srcCompat="@drawable/ic_grade_black_24dp" /> | ||
|
||
<ImageView | ||
android:id="@+id/image_Star3" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginLeft="8dp" | ||
android:layout_marginTop="8dp" | ||
app:layout_constraintStart_toEndOf="@+id/image_Star2" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:srcCompat="@drawable/ic_grade_black_24dp" /> | ||
|
||
<ImageView | ||
android:id="@+id/image_Star4" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginLeft="8dp" | ||
android:layout_marginTop="8dp" | ||
app:layout_constraintStart_toEndOf="@+id/image_Star3" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:srcCompat="@drawable/ic_grade_black_24dp" /> | ||
|
||
<ImageView | ||
android:id="@+id/image_Star5" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginLeft="8dp" | ||
android:layout_marginTop="8dp" | ||
app:layout_constraintStart_toEndOf="@+id/image_Star4" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:srcCompat="@drawable/ic_grade_black_24dp" /> | ||
|
||
|
||
</android.support.constraint.ConstraintLayout> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Sun Mar 24 00:28:03 MSK 2019 | ||
#Sat Apr 27 16:19:44 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 |
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.
Стоит сохранять аниматоры и останавливать их при уничтожении активити