Android实现3个圆圈的动画

实现了一个类似Windows进度条效果,界面上有三个圆圈,依次有一个圆圈显示白色,其它的圆圈显示蓝色。

画圆圈的View

import Android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

/**
 * 画圆圈的组件
 *
 * @author linweidong
 * @date 2010-11-05
 */
public class Circle extends View {

public Circle(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }

public Circle(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 }

@Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  if (StaticString.circleColor == Color.BLUE) {
   paint.setColor(Color.BLUE);
  } else if (StaticString.circleColor == Color.WHITE) {
   paint.setColor(Color.WHITE);
  }
  paint.setAntiAlias(true);
  paint.setStyle(Style.FILL);
  canvas.drawCircle(4, 4, 4.0f, paint);  //注意圆心的位置
  super.onDraw(canvas);

}

在使用圆圈的Activity的XML上加载圆圈组件

<custom.Circle
    android:id="@+id/circle1" android:layout_width="8dip"
    android:layout_marginTop="6dip" android:padding="4dip"
    android:layout_height="8dip" />

<custom.Circle
    android:id="@+id/circle2" android:layout_width="8dip"
    android:layout_marginTop="6dip" android:layout_marginLeft="2dip"
    android:padding="4dip" android:layout_height="8dip" />

<custom.Circle
    android:id="@+id/circle3" android:layout_width="8dip"
    android:layout_marginTop="6dip" android:layout_marginLeft="2dip"
    android:padding="4dip" android:layout_height="8dip" />

android:padding="4dip" 提供半径为4 画圆圈的空间,如果android:layout_width="wrap_content "会占很大空间,因此限定大小 

在相关的Activity使用

mCityCircle1 = (View) findViewById(R.id.circle1);
  mCityCircle2 = (View) findViewById(R.id.circle2);
  mCityCircle3 = (View) findViewById(R.id.circle3);

class CircleThread implements Runnable {
  public void run() {
   int count = 0;
   while (true) {
    try {
     count++;
     if (count % 3 == 0) {
      Thread.sleep(100);
      StaticString.circleColor = Color.WHITE;
      mCityCircle1.postInvalidate();
      Thread.sleep(100);
      StaticString.circleColor = Color.BLUE;
      mCityCircle2.postInvalidate();
      Thread.sleep(100);
      StaticString.circleColor = Color.BLUE;
      mCityCircle3.postInvalidate();

} else if (count % 3 == 1) {
      Thread.sleep(100);
      StaticString.circleColor = Color.BLUE;
      mCityCircle1.postInvalidate();
      Thread.sleep(100);
      StaticString.circleColor = Color.WHITE;
      mCityCircle2.postInvalidate();
      Thread.sleep(100);
      StaticString.circleColor = Color.BLUE;
      mCityCircle3.postInvalidate();
     } else {
      Thread.sleep(100);
      StaticString.circleColor = Color.BLUE;
      mCityCircle1.postInvalidate();
      Thread.sleep(100);
      StaticString.circleColor = Color.BLUE;
      mCityCircle2.postInvalidate();
      Thread.sleep(100);
      StaticString.circleColor = Color.WHITE;
      mCityCircle3.postInvalidate();
     }

} catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }

}
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wypjsg.html