예제 #1
0
			protected override void OnDraw (Canvas canvas)
			{
				canvas.DrawColor (Color.White);

				canvas.Translate (10, 10);

				canvas.SaveLayerAlpha (0, 0, 200, 200, 0x88, SaveFlags.All);

				mPaint.Color = Color.Red;
				canvas.DrawCircle (75, 75, 75, mPaint);
				mPaint.Color = Color.Blue;
				canvas.DrawCircle (125, 125, 75, mPaint);

				canvas.Restore ();
			}
			public void draw(Canvas canvas) {
				int width = mBounds.Width ();
				int height = mBounds.Height();
				int cx = width / 2;
				int cy = height / 2;
				Boolean drawTriggerWhileFinishing = false;
				int restoreCount = canvas.Save();
				canvas.ClipRect(mBounds);

				if (mRunning || (mFinishTime > 0)) {
					long now = AnimationUtils.CurrentAnimationTimeMillis();
					long elapsed = (now - mStartTime) % ANIMATION_DURATION_MS;
					long iterations = (now - mStartTime) / ANIMATION_DURATION_MS;
					float rawProgress = (elapsed / (ANIMATION_DURATION_MS / 100f));

					// If we're not running anymore, that means we're running through
					// the finish animation.
					if (!mRunning) {
						// If the finish animation is done, don't draw anything, and
						// don't repost.
						if ((now - mFinishTime) >= FINISH_ANIMATION_DURATION_MS) {
							mFinishTime = 0;
							return;
						}

						// Otherwise, use a 0 opacity alpha layer to clear the animation
						// from the inside out. This layer will prevent the circles from
						// drawing within its bounds.
						long finishElapsed = (now - mFinishTime) % FINISH_ANIMATION_DURATION_MS;
						float finishProgress = (finishElapsed / (FINISH_ANIMATION_DURATION_MS / 100f));
						float pct = (finishProgress / 100f);
						// Radius of the circle is half of the screen.
						float clearRadius = width / 2;//* INTERPOLATOR.getInterpolation(pct);
						mClipRect.Set(cx - clearRadius, 0, cx + clearRadius, height);
						canvas.SaveLayerAlpha(mClipRect, 0, 0);
						// Only draw the trigger if there is a space in the center of
						// this refreshing view that needs to be filled in by the
						// trigger. If the progress view is just still animating, let it
						// continue animating.
						drawTriggerWhileFinishing = true;
					}

					// First fill in with the last color that would have finished drawing.
					if (iterations == 0) {
						canvas.DrawColor (Android.Graphics.Color.Blue);
					} else {
						if (rawProgress >= 0 && rawProgress < 25) {
							canvas.DrawColor(Android.Graphics.Color.AliceBlue);
						} else if (rawProgress >= 25 && rawProgress < 50) {
							canvas.DrawColor (Android.Graphics.Color.Blue);
						} else if (rawProgress >= 50 && rawProgress < 75) {
							canvas.DrawColor (Android.Graphics.Color.BlueViolet);
						} else {
							canvas.DrawColor (Android.Graphics.Color.CadetBlue);
						}
					}

					// Then draw up to 4 overlapping concentric circles of varying radii, based on how far
					// along we are in the cycle.
					// progress 0-50 draw mColor2
					// progress 25-75 draw mColor3
					// progress 50-100 draw mColor4
					// progress 75 (wrap to 25) draw mColor1
					if ((rawProgress >= 0 && rawProgress <= 25)) {
						float pct = (((rawProgress + 25) * 2) / 100f);
						drawCircle(canvas, cx, cy, mColor1, pct);
					}
					if (rawProgress >= 0 && rawProgress <= 50) {
						float pct = ((rawProgress * 2) / 100f);
						drawCircle(canvas, cx, cy, mColor2, pct);
					}
					if (rawProgress >= 25 && rawProgress <= 75) {
						float pct = (((rawProgress - 25) * 2) / 100f);
						drawCircle(canvas, cx, cy, mColor3, pct);
					}
					if (rawProgress >= 50 && rawProgress <= 100) {
						float pct = (((rawProgress - 50) * 2) / 100f);
						drawCircle(canvas, cx, cy, mColor4, pct);
					}
					if ((rawProgress >= 75 && rawProgress <= 100)) {
						float pct = (((rawProgress - 75) * 2) / 100f);
						drawCircle(canvas, cx, cy, mColor1, pct);
					}
					if (mTriggerPercentage > 0 && drawTriggerWhileFinishing) {
						// There is some portion of trigger to draw. Restore the canvas,
						// then draw the trigger. Otherwise, the trigger does not appear
						// until after the bar has finished animating and appears to
						// just jump in at a larger width than expected.
						canvas.RestoreToCount(restoreCount);
						restoreCount = canvas.Save();
						canvas.ClipRect(mBounds);
						drawTrigger(canvas, cx, cy);
					}
					// Keep running until we finish out the last cycle.
					ViewCompat.PostInvalidateOnAnimation(mParent);
				} else {
					// Otherwise if we're in the middle of a trigger, draw that.
					if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {
						drawTrigger(canvas, cx, cy);
					}
				}
				canvas.RestoreToCount(restoreCount);
			}