Страницы

Поиск по вопросам

суббота, 7 декабря 2019 г.

Рисование кругов в libgdx

#java #android #libgdx


Имеется 3 круга, каждый круг разбит на 3 части в которых задается цвет, каждый круг
должен вращаться в разные стороны. Как это сделать?


    


Ответы

Ответ 1



Мой ответ не для libgdx, а для просто Android. Но может быть кому-то будет интересно. Это кастомное представление: public class ProgressView extends View { private Paint paint; private float mProgress; private final RectF oval = new RectF(); public ProgressView(Context context) { super(context); initPaint(); } public void setProgress(final float progress) { if (progress == mProgress) { return; } mProgress = progress; invalidate(); } private void initPaint() { paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float width = (float)getWidth(); float height = (float)getHeight(); float radius; if (width > height){ radius = height/4; } else{ radius = width/4; } float center_x, center_y; center_x = width/2; center_y = height/2; oval.set(center_x - radius, center_y - radius, center_x + radius, center_y + radius); int initialAngle = (int)(mProgress / 100 * 360); drawCircle(canvas, oval, 15, initialAngle); oval.set(center_x - 2*radius/3, center_y - 2*radius/3, center_x + 2*radius/3, center_y + 2*radius/3); initialAngle = -initialAngle; drawCircle(canvas, oval, 10, initialAngle); oval.set(center_x - radius/3, center_y - radius/3, center_x + radius/3, center_y + radius/3); initialAngle = -initialAngle; drawCircle(canvas, oval, 5, initialAngle); } private void drawCircle(Canvas canvas, RectF rectF, int strokeWidth, int initAngle) { paint.setStrokeWidth(strokeWidth); paint.setColor(Color.RED); canvas.drawArc(rectF, initAngle, 120, false, paint); paint.setColor(Color.YELLOW); canvas.drawArc(rectF, initAngle + 120, 120, false, paint); paint.setColor(Color.WHITE); canvas.drawArc(rectF, initAngle + 240, 120, false, paint); } } Это активность, использующая этот View, где движение в разные стороны достигается с помощью ValueAnimator: public class MainActivity extends AppCompatActivity { private ProgressView progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); progressBar = new ProgressView(this); setContentView(progressBar); ValueAnimator anim = ValueAnimator.ofInt(0, 1000); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { progressBar.setProgress((int)animation.getAnimatedValue()); } }); anim.setDuration(40000); anim.setRepeatCount(ValueAnimator.INFINITE); anim.start(); } }

Комментариев нет:

Отправить комментарий