效果图
View源码
package com.android.circlescalebar.view;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import com.android.circlescalebar.R;
public class SatelliteOrbitView extends View {
private Paint orbitPaint;
private Bitmap satelliteBitmap;
private float orbitRadius;
private float satelliteAngle;
public SatelliteOrbitView(Context context) {
super(context);
init();
}
public SatelliteOrbitView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SatelliteOrbitView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
orbitPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
orbitPaint.setColor(Color.argb(35, 255, 255, 0));
orbitPaint.setStyle(Paint.Style.STROKE);
orbitPaint.setStrokeWidth(80);
// 加载卫星图片
satelliteBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_satellite);
orbitRadius = 260; // 轨道半径
satelliteAngle = 270 % 360; // 初始角度
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制圆形轨道
canvas.drawCircle(getWidth() / 2, getHeight() / 2, orbitRadius, orbitPaint);
// 计算卫星位置
float satelliteX = getWidth() / 2 + orbitRadius * (float) Math.cos(Math.toRadians(satelliteAngle));
float satelliteY = getHeight() / 2 + orbitRadius * (float) Math.sin(Math.toRadians(satelliteAngle));
// 绘制卫星
Rect src = new Rect(0, 0, satelliteBitmap.getWidth(), satelliteBitmap.getHeight());
Rect dst = new Rect((int) (satelliteX - satelliteBitmap.getWidth() / 6),
(int) (satelliteY - satelliteBitmap.getHeight() / 6),
(int) (satelliteX + satelliteBitmap.getWidth() / 6),
(int) (satelliteY + satelliteBitmap.getHeight() / 6));
// 卫星跟随轨道对齐
canvas.rotate(satelliteAngle + 90, satelliteX, satelliteY);
canvas.drawBitmap(satelliteBitmap, src, dst, null);
}
public void startAnimation() {
ObjectAnimator animator = ObjectAnimator.ofFloat(this, "satelliteAngle", 0, 360);
animator.setDuration(5000); // 设置动画持续时间
animator.setInterpolator(new LinearInterpolator()); // 设置插值器
animator.setRepeatCount(ObjectAnimator.INFINITE); // 设置重复次数为无限
animator.setRepeatMode(ObjectAnimator.RESTART); // 设置重复模式为重新开始
animator.start();
}
public void setSatelliteAngle(float angle) {
this.satelliteAngle = (angle + 270) % 360;
invalidate(); // 请求重绘视图
}
}
调用实现
SatelliteOrbitView satelliteOrbitView = findViewById(R.id.satelliteOrbitView);
count = 0;
mHandler.removeCallbacks(mRunnable);
mRunnable = new Runnable() {
@Override
public void run() {
satelliteOrbitView.setSatelliteAngle(count);
count++;
if (count > 360) {
// 停止循环
mHandler.removeCallbacks(this);
} else {
// 继续循环
mHandler.postDelayed(this, 15); // 每秒更新一次
}
}
};
mHandler.post(mRunnable);
布局
<com.android.circlescalebar.view.SatelliteOrbitView
android:id="@+id/satelliteOrbitView"
android:layout_width="215dp"
android:layout_height="215dp"
android:layout_gravity="center" />