Skip to content

Commit

Permalink
animation scaling issues fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
wesaphzt committed Sep 7, 2019
1 parent cd8226c commit 87e349d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
25 changes: 23 additions & 2 deletions app/src/main/java/com/wesaphzt/privatelock/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@

import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
Expand Down Expand Up @@ -126,10 +127,30 @@ public void onClick(View v) {
}
});

//https://stackoverflow.com/questions/29381474/how-to-draw-a-circle-with-animation-in-android-with-circle-size-based-on-a-value
circle = findViewById(R.id.circle);
circle_bg = findViewById(R.id.circle_bg);

final RelativeLayout relativeLayout = findViewById(R.id.content_main);
final RelativeLayout rlCircle = findViewById(R.id.rlCircle);

//scale height/width of animation
relativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
//only want to do this once
relativeLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);

circle.setRect((circle.getHeight() / 2), (circle.getHeight()) / 2);
circle_bg.setRect((circle_bg.getHeight() / 2), (circle_bg.getHeight()) / 2);

circle.setX((rlCircle.getWidth() - circle.getRect()) / 2);
circle_bg.setX((rlCircle.getWidth() - circle.getRect()) / 2);
}
});


circle.setColor(circleDefaultR, circleDefaultG, circleDefaultB);
//set background circle
CircleAngleAnimation animation = new CircleAngleAnimation(circle_bg, 360);
Expand Down
17 changes: 12 additions & 5 deletions app/src/main/java/com/wesaphzt/privatelock/animation/Circle.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ public class Circle extends View {
private static final int START_ANGLE_POINT = 90;

private final Paint paint;
private final RectF rect;
private RectF rect;

private float angle;
private final int strokeWidth = 10;

public Circle(Context context, AttributeSet attrs) {
super(context, attrs);

final int strokeWidth = 10;

paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(strokeWidth);
//circle color (currently set upon instance)
//paint.setColor(Color.BLUE);

//size
rect = new RectF(strokeWidth, strokeWidth, 600 + strokeWidth, 600 + strokeWidth);
//size (currently set upon instance)
//rect = new RectF(strokeWidth, strokeWidth, 230 + strokeWidth, 230 + strokeWidth);

//initial angle (optional)
angle = 0;
Expand All @@ -53,4 +52,12 @@ public void setColor(int r, int g, int b) {
public void setAngle(float angle) {
this.angle = angle;
}

public void setRect(int right, int bottom) {
this.rect = new RectF(strokeWidth, strokeWidth, right, bottom);
}

public float getRect() {
return rect.right;
}
}
18 changes: 10 additions & 8 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,24 @@
</androidx.cardview.widget.CardView>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rlCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_margin_large"
android:layout_gravity="center_horizontal">

<com.wesaphzt.privatelock.animation.Circle
android:id="@+id/circle_bg"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_centerHorizontal="true" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />

<com.wesaphzt.privatelock.animation.Circle
android:id="@+id/circle"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal" />

</RelativeLayout>
Expand Down

0 comments on commit 87e349d

Please sign in to comment.