Skip to main content
3 of 4
deleted 1 characters in body; edited title
jiduvah
  • 433
  • 7
  • 16

Clock View in Android

So I have built a clock widget. It basically has 2 hands and displays a time, either the current time or a selected time. There is another option which tells the clock if it should move around as the time does.

Is this a good way to do this? It seems to work well in my app but I was wondering if there might be some performance issues? What are the alternatives?

--EDIT--

I am aware that it's poor form to hard code lengths when drawing the lines. I will change that

public class ClockView extends RelativeLayout {

    private Animation minuteRotationAnimation;
    private Animation hourRotationAnimation;
    private LayoutInflater layoutInflater;
    private HandView minuteHandView;
    private HandView hourHandView;

    public ClockView(Context context, AttributeSet attrs) {
        super(context, attrs);
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        RelativeLayout relativeLayout = (RelativeLayout) layoutInflater.inflate(R.layout.clock_view, null);
        minuteHandView = (HandView) relativeLayout.findViewById(R.id.secondHandView);
        hourHandView = (HandView) relativeLayout.findViewById(R.id.hourHandView);
        hourHandView.setHeightPercentage(50);
       // hourHandView.setHalf(true);
        addView(relativeLayout);
        minuteRotationAnimation = AnimationUtils.loadAnimation(context, R.anim.rotate_minutes);
        hourRotationAnimation = AnimationUtils.loadAnimation(context, R.anim.rotate_hour);
    }

    public void setAsCurrentTime() {
        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        Date date = cal.getTime();
        int hour = date.getHours();
        int minute = date.getMinutes();
        setTime(hour, minute, false);
    }

    public void setTime(long timestamp, boolean stop) {
        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timestamp);
        Date date = cal.getTime();
        int hour = date.getHours();
        int minute = date.getMinutes();
        setTime(hour, minute, false);
    }

    public void setTime(int hour, int minute, boolean  stop) {
        int minuteDegrees = getDegrees(false, minute);
        int hourDegrees = getDegrees(true, hour);
        RotateAnimation hourStartAnimation = getRotateAnimation(hourDegrees);
        RotateAnimation minuteStartAnimation = getRotateAnimation(minuteDegrees);
        minuteHandView.startAnimation(minuteStartAnimation);

        if (stop == false) {
            AnimationSet hourAnimationSet = getAnimationSet(hourStartAnimation, hourRotationAnimation);
            AnimationSet minuteAnimationSet = getAnimationSet(minuteStartAnimation, minuteRotationAnimation);
            hourHandView.startAnimation(hourAnimationSet);
            minuteHandView.startAnimation(minuteAnimationSet);
        } else {
            hourHandView.startAnimation(hourStartAnimation);
            minuteHandView.startAnimation(minuteStartAnimation);
        }
    }

    private AnimationSet getAnimationSet(Animation animation1, Animation animation2) {
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(animation1);
        animationSet.addAnimation(animation2);
        return animationSet;
    }

    private RotateAnimation getRotateAnimation(int degrees) {
        RotateAnimation animation = new RotateAnimation(0, degrees, 50, 50);
        animation.setDuration(1000);
        animation.setRepeatCount(0);
        animation.setRepeatMode(Animation.REVERSE);
        animation.setFillAfter(true);
        return animation;
    }

    private int getDegrees(boolean hour, int value) {
        int multiple;
        if (hour) {
            multiple = 12;
        } else {
            multiple = 60;
        }
        return (360 / multiple) * value;
    }
  }


public class HandView extends View {

    private Paint paint = new Paint();
    private int heightPercentage = 100;
    private boolean half = false;


    public void setHalf(boolean half) {
        this.half = half;
    }

    public HandView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setColor(context.getResources().getColor(R.color.clockBlue));
        paint.setStrokeWidth(5);
        paint.setAntiAlias(true);
    }

    public void setHeightPercentage(int heightPercentage) {
        this.heightPercentage = heightPercentage;
    }

    public void setWidth(float width) {
        paint.setStrokeWidth(width);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height;
        if (half) {
            height = 30;
        } else {
            height = 10;
        }
        Rect rect = canvas.getClipBounds();
        canvas.drawLine(rect.exactCenterX(), rect.exactCenterY(), rect.exactCenterX(), height, paint);
    }
}
jiduvah
  • 433
  • 7
  • 16