Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.canvas.animacija.animacijasuncevogsistema;
- import android.support.v7.app.AppCompatActivity;
- i
- mport android.os.Bundle;
- public class MainActivity extends AppCompatActivity {
- AnimationLayout animation;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- animation = new AnimationLayout(this);
- setContentView(animation);
- }
- @Override
- protected void onPause() {
- super.onPause();
- animation.pause();
- }
- @Override
- protected void onResume() {
- super.onResume();
- animation.resume();
- }
- }
- package com.canvas.animacija.animacijasuncevogsistema;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- public class AnimationLayout extends SurfaceView implements Runnable {
- Thread thread;
- boolean canDraw;
- Canvas canvas;
- SurfaceHolder holder;
- Paint yellow_fill_and_stroke, red_fill, green_fill, blue_fill, magenta_fill;
- int circle1_x, circle1_y, circle2_x, circle2_y, circle3_x, circle3_y, circle4_x, circle4_y,
- circle5_x, circle5_y;
- int radius1, radius2, radius3, radius4, radius5;
- public AnimationLayout(Context context) {
- super(context);
- thread = null;
- canDraw = false;
- holder = getHolder();
- circle1_x = toPxs(180);
- circle1_y = toPxs(250);
- radius1 = toPxs(30);
- circle2_x = toPxs(180);
- circle2_y = toPxs(240) - radius1;
- radius2 = toPxs(7);
- }
- @Override
- public void run() {
- prepareBrushes();
- while (canDraw) {
- if (!holder.getSurface().isValid()) {
- continue;
- }
- canvas = holder.lockCanvas();
- canvas.drawCircle(circle1_x, circle1_y, radius1, yellow_fill_and_stroke);
- canvas.drawCircle(circle2_x, circle2_y, radius2, red_fill);
- moveToRight(circle2_x, circle2_y, radius2, 10);
- holder.unlockCanvasAndPost(canvas);
- }
- }
- public void pause() {
- canDraw = false;
- while (true) {
- try {
- thread.join();
- break;
- } catch (InterruptedException ie) {
- ie.printStackTrace();
- }
- }
- thread = null;
- }
- public void resume() {
- canDraw = true;
- thread = new Thread(this, "Animation Thread");
- thread.start();
- }
- private void prepareBrushes() {
- yellow_fill_and_stroke = new Paint();
- yellow_fill_and_stroke.setColor(Color.YELLOW);
- yellow_fill_and_stroke.setStyle(Paint.Style.FILL_AND_STROKE);
- yellow_fill_and_stroke.setStrokeWidth(toPxs(3));
- red_fill = new Paint();
- red_fill.setColor(Color.RED);
- red_fill.setStyle(Paint.Style.FILL);
- green_fill = new Paint();
- green_fill.setColor(Color.GREEN);
- green_fill.setStyle(Paint.Style.FILL);
- blue_fill = new Paint();
- blue_fill.setColor(Color.BLUE);
- blue_fill.setStyle(Paint.Style.FILL);
- magenta_fill = new Paint();
- magenta_fill.setColor(Color.MAGENTA);
- magenta_fill.setStyle(Paint.Style.FILL);
- }
- private int toPxs(int dps) {
- return (int) (dps * getResources().getDisplayMetrics().density);
- }
- private void moveToRight(int coordinate_x, int coordinate_y, int radius, int speed) {
- int d_speed = toPxs(speed);
- if (coordinate_x >= coordinate_y - radius/2 && coordinate_y >= coordinate_x + radius/2 ) {
- coordinate_x += d_speed;
- coordinate_y += d_speed;
- }
- if (coordinate_x >= coordinate_y + radius/2 && coordinate_y < coordinate_x + radius/2) {
- coordinate_x -= d_speed;
- coordinate_y += d_speed;
- }
- if (coordinate_x < coordinate_y + radius/2 && coordinate_y >= coordinate_x - radius/2) {
- coordinate_x -= d_speed;
- coordinate_y -= d_speed;
- }
- if (coordinate_x < coordinate_y - radius/2 && coordinate_y < coordinate_x - radius/2) {
- coordinate_x += d_speed;
- coordinate_y -= d_speed;
- }
- }
- }
Add Comment
Please, Sign In to add comment