Android3D广告图片滚动控件_3D图片轮播器Demo
程序员文章站
2022-03-01 14:53:14
...
Android高级图片滚动控件、3D版的图片轮播器Demo、具有超强的3D效果
程序运行效果
原理也很简单、下面一起来看看整个实现的核心代码吧
如果Bitmap对象还不存在、先使用父类的onDraw方法进行绘制
绘图时需要注意、只有当图片可见的时候才进行绘制、这样可以节省运算效率
protected void onDraw(Canvas canvas) { if (mBitmap == null) { super.onDraw(canvas); } else { if (isImageVisible()) { computeRotateData(); mCamera.save(); mCamera.translate(0.0f, 0.0f, mDeep); mCamera.rotateY(mRotateDegree); mCamera.getMatrix(mMaxtrix); mCamera.restore(); mMaxtrix.preTranslate(-mDx, -getHeight() / 2); mMaxtrix.postTranslate(mDx, getHeight() / 2); canvas.drawBitmap(mBitmap, mMaxtrix, null); } } }
在这里计算所有旋转所需要的数据
private void computeRotateData() { float degreePerPix = BASE_DEGREE / mWidth; float deepPerPix = BASE_DEEP / ((mLayoutWidth - mWidth) / 2); switch (mIndex) { case 0: mDx = mWidth; mRotateDegree = 360f - (2 * mWidth + mScrollX) * degreePerPix; if (mScrollX < -mWidth) { mDeep = 0; } else { mDeep = (mWidth + mScrollX) * deepPerPix; } break; case 1: if (mScrollX > 0) { mDx = mWidth; mRotateDegree = (360f - BASE_DEGREE) - mScrollX * degreePerPix; mDeep = mScrollX * deepPerPix; } else { if (mScrollX < -mWidth) { mDx = -Image3DSwitchView.IMAGE_PADDING * 2; mRotateDegree = (-mScrollX - mWidth) * degreePerPix; } else { mDx = mWidth; mRotateDegree = 360f - (mWidth + mScrollX) * degreePerPix; } mDeep = 0; } break; case 2: if (mScrollX > 0) { mDx = mWidth; mRotateDegree = 360f - mScrollX * degreePerPix; mDeep = 0; if (mScrollX > mWidth) { mDeep = (mScrollX - mWidth) * deepPerPix; } } else { mDx = -Image3DSwitchView.IMAGE_PADDING * 2; mRotateDegree = -mScrollX * degreePerPix; mDeep = 0; if (mScrollX < -mWidth) { mDeep = -(mWidth + mScrollX) * deepPerPix; } } break; case 3: if (mScrollX < 0) { mDx = -Image3DSwitchView.IMAGE_PADDING * 2; mRotateDegree = BASE_DEGREE - mScrollX * degreePerPix; mDeep = -mScrollX * deepPerPix; } else { if (mScrollX > mWidth) { mDx = mWidth; mRotateDegree = 360f - (mScrollX - mWidth) * degreePerPix; } else { mDx = -Image3DSwitchView.IMAGE_PADDING * 2; mRotateDegree = BASE_DEGREE - mScrollX * degreePerPix; } mDeep = 0; } break; case 4: mDx = -Image3DSwitchView.IMAGE_PADDING * 2; mRotateDegree = (2 * mWidth - mScrollX) * degreePerPix; if (mScrollX > mWidth) { mDeep = 0; } else { mDeep = (mWidth - mScrollX) * deepPerPix; } break; } }
源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1kT1JwBt 密码: q6pi