在Android中旋转一个圆围绕另一个圆,可以使用动画和自定义视图来实现。下面是一个实现的示例:
public class CircleView extends View {
private Paint paint;
private float centerX, centerY;
private float radius1, radius2;
private float angle = 0;
public CircleView(Context context) {
super(context);
init();
}
public CircleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
centerX = w / 2f;
centerY = h / 2f;
radius1 = Math.min(w, h) / 4f;
radius2 = Math.min(w, h) / 8f;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(centerX, centerY, radius1, paint);
float x = (float) (centerX + radius1 * Math.cos(Math.toRadians(angle)));
float y = (float) (centerY + radius1 * Math.sin(Math.toRadians(angle)));
canvas.drawCircle(x, y, radius2, paint);
}
public void setAngle(float angle) {
this.angle = angle;
invalidate();
}
}
public class MainActivity extends AppCompatActivity {
private CircleView circleView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
circleView = findViewById(R.id.circle_view);
ObjectAnimator animator = ObjectAnimator.ofFloat(circleView, "angle", 0, 360);
animator.setDuration(2000);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.start();
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<com.example.myapplication.CircleView
android:id="@+id/circle_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
这样就可以在Android中实现一个圆围绕另一个圆旋转的效果了。
TAIC
云+社区技术沙龙 [第30期]
微搭低代码直播互动专栏
云+社区技术沙龙[第17期]
云+社区沙龙online [技术应变力]
微搭低代码直播互动专栏
云+社区技术沙龙[第29期]
云+社区技术沙龙[第2期]
云+社区技术沙龙[第11期]
云+社区技术沙龙[第23期]
领取专属 10元无门槛券
手把手带您无忧上云