1

メソッドで円を作成し、円の周りに文字列と線 (矢印) を描画しましたonDraw()

public class Circle extends Activity {  

  public class OuterCircle extends View {
    Paint paint = new Paint();
    private Animation anim;
    Path path = new Path();
    private static final String s = "Hello world example";

    public OuterCircle(Context context) {
      super(context);
      init();
    }

    private void init() {
      paint.setColor(Color.WHITE);
      paint.setStyle(Style.STROKE);
      paint.setStrokeWidth(2);
      paint.setAntiAlias(true);
    }

    private void drawStringOnCircle(Canvas c) {
      path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
      c.drawTextOnPath(s, path, 0, 10, paint);
      setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    private void createAnimation() {
      anim = new RotateAnimation(0, 360, getWidth()/2, getHeight()/2);
      anim.setRepeatMode(Animation.RESTART);
      anim.setRepeatCount(Animation.INFINITE);
      anim.setDuration(100L);
      startAnimation(anim);
    }

    public void onDraw(Canvas c) {      
      int cx = getWidth()/2;
      int cy = getHeight()/2;

      if (anim == null) {
             createAnimation();
      }

      c.drawCircle(cx, cy, 170, paint);      
      drawStringOnCircle(c);
      c.drawLine(cx, cy, cx, cy+170, paint); // do not include this line in animation
    }
  }

  OuterCircle = compassView;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    compassView = new DrawCompass(this);
    setContentView(compassView);
  }
}

円とその周りのテキストだけにアニメーションを適用したい(Androidアニメーションを使用して円とその周りのテキストを回転させます)。円の内側に描いた線は静止している必要があります (アニメーションは線に適用されません)。ただし、上記のコードは、アニメーションをすべての 3 に適用していCanvasます。どのような変更を加える必要がありますか?

TextViewまた、円の真ん中にを追加したいです。誰でもアイデアを共有できますか?

4

1 に答える 1

0

View線を描画するだけでアニメーション化しないnew を実装できます。次に、レイアウトで OuterCirle-View の上に Line-View を描画するか、コードでaddView(lineView).

同じ方法で追加できますTextView

更新:他のビューを含むことができるsetContentView()a を設定する必要がViewあります -> のようなレイアウトRelativeLayoutViewこのレイアウトでは、 を呼び出して複数追加できますRelativeLayout.addView(childView)。このようにして、回転する円を追加し、次に線を追加し、TextView必要に応じて を追加できます。

于 2013-10-03T11:37:48.133 に答える