在android中制作Analogclock

时间:2015-02-05 11:39:08

标签: android canvas view time clock

我是Android新手,我想创建一个简单的应用程序,我想要显示模拟时钟。我知道时钟是如何工作的,但我无法设计它。到现在为止我画了一圈,现在我想附上资源,但我不知道怎么做。任何人都可以帮助我,并给我程序。谢谢

public class MyView extends View
{    

public MyView(Context context) 
{
     super(context);
}




@Override
protected void onDraw(Canvas canvas) 
{
   super.onDraw(canvas);

   int x = getWidth();
   int y = getHeight();
   int radius=200;
   int w=2*radius;
   int h=2*radius;
   boolean changed = mChanged;
   if (changed) {
       mChanged = false;
   }
   boolean seconds = mSeconds;
   if (seconds ) {
    mSeconds = false;
   }
  // radius = 200;
   Paint paint = new Paint();
   paint.setStyle(Paint.Style.FILL);
   paint.setColor(Color.BLACK);
   canvas.drawPaint(paint);
   // Use Color.parseColor to define HTML colors
   paint.setColor(Color.parseColor("#b60f2e"));
   canvas.drawCircle(x /2, y / 2, radius, paint);
   paint.setColor(Color.parseColor("#f6e1ff"));
   canvas.drawLine(x/2, y/2 + radius,x/2, y/2-15 + radius, paint);
   canvas.drawLine(x/2 , y/2 - radius,x/2 ,y/2 - radius+15, paint);
   canvas.drawLine(x/2 + radius, y/2 , x/2 + radius-15, y/2, paint);
   canvas.drawLine(x/2 - radius, y/2, x/2 - radius+15, y/2, paint);
   canvas.save();
   canvas.rotate(mHour / 12.0f * 360.0f, x, y);
   final Drawable hourHand = mHourHand;

   hourHand.draw(canvas);
   canvas.restore();


  }
  }

1 个答案:

答案 0 :(得分:0)

以下是一些可能对您感兴趣的示例。 http://mindtherobot.com/blog/272/android-custom-ui-making-a-vintage-thermometer/

否则你可以使用drawable而不是bitmap。基本上你应该引用drawable

Drawable hand = getResources().getDrawable(R.drawable.minute_hand);

并在自定义视图的onDraw回调中在画布上绘制

hand.draw(canvas);

p.s。下次寻找类似的问题,因为已经有很多可用的