protected override void OnDraw(Canvas canvas) { base.OnDraw(canvas); if (mGrandientMatrix != null) { mTranslate += mViewWidth / 5; if (mTranslate > 2 * mViewWidth) { mTranslate = -mViewWidth; } mGrandientMatrix.SetTranslate(mTranslate, 0); mLinearGradient.SetLocalMatrix(mGrandientMatrix); PostInvalidateDelayed(30); } }
private void CalculateMatrices() { _shouldDraw = true; if (!_isFoldPrepared) { return; } if (FloatNearlyEqual(_foldFactor, 1)) { _shouldDraw = false; return; } if (FloatNearlyEqual(_foldFactor, 0) && _previousFoldFactor > 0 && FoldEnded != null) { FoldEnded(this, EventArgs.Empty); } if (FloatNearlyEqual(_previousFoldFactor, 0) && _foldFactor > 0 && FoldStarted != null) { FoldStarted(this, EventArgs.Empty); } _previousFoldFactor = _foldFactor; for (var i = 0; i < _numberOfFolds; i++) { _matrix[i].Reset(); } var translationFactor = 1 - _foldFactor; var translatedDistance = _isHorizontal ? _originalWidth * translationFactor : _originalHeight * translationFactor; var translatedDistancePerFold = Math.Round(translatedDistance / _numberOfFolds); _foldDrawWidth = (float)(_foldMaxWidth < translatedDistancePerFold ? translatedDistancePerFold : _foldMaxWidth); _foldDrawHeight = (float)(_foldMaxHeight < translatedDistancePerFold ? translatedDistancePerFold : _foldMaxHeight); var translatedDistanceFoldSquared = translatedDistancePerFold * translatedDistancePerFold; var depth = _isHorizontal ? (float)Math.Sqrt(_foldDrawWidth * _foldDrawWidth - translatedDistanceFoldSquared) : (float)Math.Sqrt(_foldDrawHeight * _foldDrawHeight - translatedDistanceFoldSquared); var scaleFactor = DepthConstant / (DepthConstant + depth); float scaledWidth, scaledHeight; if (_isHorizontal) { scaledWidth = _foldDrawWidth * translationFactor; scaledHeight = _foldDrawHeight * scaleFactor; } else { scaledWidth = _foldDrawWidth * scaleFactor; scaledHeight = _foldDrawHeight * translationFactor; } var topScaledPoint = (_foldDrawHeight - scaledHeight) / 2.0f; var bottomScaledPoint = topScaledPoint + scaledHeight; var leftScaledPoint = (_foldDrawWidth - scaledWidth) / 2.0f; var rightScaledPoint = leftScaledPoint + scaledWidth; var anchorPoint = _isHorizontal ? AnchorFactor * _originalWidth : AnchorFactor * _originalHeight; var midFold = _isHorizontal ? (anchorPoint / _foldDrawWidth) : anchorPoint / _foldDrawHeight; _src[0] = 0; _src[1] = 0; _src[2] = 0; _src[3] = _foldDrawHeight; _src[4] = _foldDrawWidth; _src[5] = 0; _src[6] = _foldDrawWidth; _src[7] = _foldDrawHeight; for (var i = 0; i < _numberOfFolds; i++) { var isEven = (i % 2 == 0); if (_isHorizontal) { _dst[0] = (anchorPoint > i * _foldDrawWidth) ? anchorPoint + (i - midFold) * scaledWidth : anchorPoint - (midFold - i) * scaledWidth; _dst[1] = isEven ? 0 : topScaledPoint; _dst[2] = _dst[0]; _dst[3] = isEven ? _foldDrawHeight : bottomScaledPoint; _dst[4] = (anchorPoint > (i + 1) * _foldDrawWidth) ? anchorPoint + (i + 1 - midFold) * scaledWidth : anchorPoint - (midFold - i - 1) * scaledWidth; _dst[5] = isEven ? topScaledPoint : 0; _dst[6] = _dst[4]; _dst[7] = isEven ? bottomScaledPoint : _foldDrawHeight; } else { _dst[0] = isEven ? 0 : leftScaledPoint; _dst[1] = (anchorPoint > i * _foldDrawHeight) ? anchorPoint + (i - midFold) * scaledHeight : anchorPoint - (midFold - i) * scaledHeight; _dst[2] = isEven ? leftScaledPoint : 0; _dst[3] = (anchorPoint > (i + 1) * _foldDrawHeight) ? anchorPoint + (i + 1 - midFold) * scaledHeight : anchorPoint - (midFold - i - 1) * scaledHeight; _dst[4] = isEven ? _foldDrawWidth : rightScaledPoint; _dst[5] = _dst[1]; _dst[6] = isEven ? rightScaledPoint : _foldDrawWidth; _dst[7] = _dst[3]; } for (var j = 0; j < NumOfPolyPoints; j++) { _dst[j] = (float)Math.Round(_dst[j]); } if (_isHorizontal) { if (_dst[4] <= _dst[0] || _dst[6] <= _dst[2]) { _shouldDraw = false; return; } } else { if (_dst[3] <= _dst[1] || _dst[7] <= _dst[5]) { _shouldDraw = false; return; } } _matrix[i].SetPolyToPoly(_src, 0, _dst, 0, NumOfPolyPoints / 2); } var alpha = (int)(_foldFactor * 255 * ShadingAlpha); _solidShadow.Color = Color.Argb(alpha, 0, 0, 0); if (_isHorizontal) { _shadowGradientMatrix.SetScale(_foldDrawWidth, 1); _shadowLinearGradient.SetLocalMatrix(_shadowGradientMatrix); } else { _shadowGradientMatrix.SetScale(1, _foldDrawHeight); _shadowLinearGradient.SetLocalMatrix(_shadowGradientMatrix); } _gradientShadow.Alpha = alpha; }