代码,看了代码什么都明白
CoverFlow从Gallery继承过来
package com.coverflow; |
<wbr></wbr> <wbr></wbr> |
import android.content.Context; |
import android.graphics.Camera; |
import android.graphics.Matrix; |
import android.util.AttributeSet; |
import android.view.View; |
import android.view.animation.Transformation; |
import android.widget.Gallery; |
import android.widget.ImageView; |
<wbr></wbr> <wbr></wbr> |
public class CoverFlow extends Gallery { |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private Camera mCamera = new Camera(); |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private int mMaxRotationAngle = 50 ; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private int mMaxZoom = - 380 ; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private int mCoveflowCenter; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private boolean mAlphaMode = true ; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private boolean mCircleMode = false ; |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public CoverFlow(Context context) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> super (context); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> this .setStaticTransformations<wbr>Enabled(</wbr> true ); |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public CoverFlow(Context context, AttributeSet attrs) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> super (context, attrs); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> this .setStaticTransformations<wbr>Enabled(</wbr> true ); |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public CoverFlow(Context context, AttributeSet attrs, int defStyle) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> super (context, attrs, defStyle); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> this .setStaticTransformations<wbr>Enabled(</wbr> true ); |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public int getMaxRotationAngle() { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return mMaxRotationAngle; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public void setMaxRotationAngle( int maxRotationAngle) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mMaxRotationAngle = maxRotationAngle; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public boolean getCircleMode() { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return mCircleMode; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public void setCircleMode( boolean isCircle) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mCircleMode = isCircle; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public boolean getAlphaMode() { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return mAlphaMode; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public void setAlphaMode( boolean isAlpha) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mAlphaMode = isAlpha; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public int getMaxZoom() { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return mMaxZoom; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public void setMaxZoom( int maxZoom) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mMaxZoom = maxZoom; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private int getCenterOfCoverflow() { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> + getPaddingLeft(); |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private static int getCenterOfView(View view) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return view.getLeft() + view.getWidth() / 2 ; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> protected boolean getChildStaticTransforma<wbr>tion(View child, Transformation t) {</wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> final int childCenter = getCenterOfView(child); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> final int childWidth = child.getWidth(); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> int rotationAngle = 0 ; |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> t.clear(); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> t.setTransformationType(Transformation.TYPE_MATRIX); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> if (childCenter == mCoveflowCenter) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> transformImageBitmap((ImageView) child, t, 0 ); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> } else { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> rotationAngle = ( int ) ((( float ) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> if (Math.abs(rotationAngle) > mMaxRotationAngle) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> rotationAngle = (rotationAngle < 0 ) ? -mMaxRotationAngle |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> : mMaxRotationAngle; |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> } |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> transformImageBitmap((ImageView) child, t, rotationAngle); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> } |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return true ; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> protected void onSizeChanged( int w, int h, int oldw, int oldh) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mCoveflowCenter = getCenterOfCoverflow(); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> super .onSizeChanged(w, h, oldw, oldh); |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private void transformImageBitmap(ImageView child, Transformation t, |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> int rotationAngle) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mCamera.save(); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> final Matrix imageMatrix = t.getMatrix(); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> final int imageHeight = child.getLayoutParams().height; |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> final int imageWidth = child.getLayoutParams().width; |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> final int rotation = Math.abs(rotationAngle); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mCamera.translate( 0 .0f, 0 .0f, 100 .0f); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // 如视图的角度更少,放大 |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> if (rotation <= mMaxRotationAngle) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> float zoomAmount = ( float ) (mMaxZoom + (rotation * 1.5 )); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mCamera.translate( 0 .0f, 0 .0f, zoomAmount); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> if (mCircleMode) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> if (rotation < 40 ) |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mCamera.translate( 0 .0f, 155 , 0 .0f); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> else |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mCamera.translate( 0 .0f, ( 255 - rotation * 2 .5f), 0 .0f); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> } |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> if (mAlphaMode) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ((ImageView) (child)).setAlpha(( int ) ( 255 - rotation * 2.5 )); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> } |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> } |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mCamera.rotateY(rotationAngle); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mCamera.getMatrix(imageMatrix); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> imageMatrix.preTranslate(-(imageWidth / 2 ), -(imageHeight / 2 )); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> imageMatrix.postTranslate((imageWidth / 2 ), (imageHeight / 2 )); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mCamera.restore(); |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
} |
这个就是CoverFlow类,说明几点:
1. 成员函数
mCamera是用来做类3D效果处理,比如z轴方向上的平移,绕y轴的旋转等
mMaxRotationAngle是图片绕y轴最大旋转角度,也就是屏幕最边上那两张图片的旋转角度
mMaxZoom是图片在z轴平移的距离,视觉上看起来就是放大缩小的效果.
其他的变量都可以无视
也就是说把这个属性设成true的时候每次viewGroup(看Gallery的源码就可以看到它是从ViewGroup间接继承过来的)在重新画它的child的时候都会促发getChildStaticTransforma<wbr>tion</wbr>这个函数,所以我们只需要在这个函数里面去加上旋转和放大的操作就可以了
其他的getter和setter函数都可以无视
ImageAdapter适配器:
<wbr></wbr>
package com.coverflow; |
<wbr></wbr> <wbr></wbr> |
import android.content.Context; |
import android.graphics.Bitmap; |
import android.graphics.BitmapFactory; |
import android.graphics.Canvas; |
import android.graphics.LinearGradient; |
import android.graphics.Matrix; |
import android.graphics.Paint; |
import android.graphics.PorterDuffXfermode; |
import android.graphics.Bitmap.Config; |
import android.graphics.PorterDuff.Mode; |
import android.graphics.Shader.TileMode; |
import android.graphics.drawable.BitmapDrawable; |
import android.view.View; |
import android.view.ViewGroup; |
import android.widget.BaseAdapter; |
import android.widget.ImageView; |
<wbr></wbr> <wbr></wbr> |
import com.gallery.R; |
<wbr></wbr> <wbr></wbr> |
public class ImageAdapter extends BaseAdapter { |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> int mGalleryItemBackground; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private Context mContext; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> private Integer[] mImageIds = {<wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> R.drawable.a1, |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> R.drawable.a2,<wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> R.drawable.a3, |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> R.drawable.a4,<wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> R.drawable.a5 }; |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public ImageAdapter(Context c) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> mContext = c; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public int getCount() { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return mImageIds.length; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public Object getItem( int position) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return position; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public long getItemId( int position) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return position; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public View getView( int position, View convertView, ViewGroup parent) { |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ImageView i = createReflectedImages(mContext,mImageIds[position]); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> i.setLayoutParams( new CoverFlow.LayoutParams( 120 , 100 )); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // 设置的抗锯齿 |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> BitmapDrawable drawable = (BitmapDrawable) i.getDrawable(); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> drawable.setAntiAlias( true ); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return i; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public float getScale( boolean focused, int offset) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return Math.max( 0 , 1 .0f / ( float ) Math.pow( 2 , Math.abs(offset))); |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public ImageView createReflectedImages(Context mContext, int imageId) { |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> final int reflectionGap = 4 ; |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> int width = originalImage.getWidth(); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> int height = originalImage.getHeight(); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> Matrix matrix = new Matrix(); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> matrix.preScale( 1 , - 1 ); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0 , |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> height / 2 , width, height / 2 , matrix, false ); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> Bitmap bitmapWithReflection = Bitmap.createBitmap(width, |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> (height + height / 2 ), Config.ARGB_8888); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> Canvas canvas = new Canvas(bitmapWithReflection); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> canvas.drawBitmap(originalImage, 0 , 0 , null ); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> Paint deafaultPaint = new Paint(); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> canvas.drawRect( 0 , height, width, height + reflectionGap, deafaultPaint); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> canvas.drawBitmap(reflectionImage, 0 , height + reflectionGap, null ); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> Paint paint = new Paint(); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> LinearGradient shader = new LinearGradient( 0 , originalImage |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> .getHeight(), 0 , bitmapWithReflection.getHeight() |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> + reflectionGap, 0x70ffffff , 0x00ffffff , TileMode.MIRROR); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> paint.setShader(shader); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> paint.setXfermode( new PorterDuffXfermode(Mode.DST_IN)); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> canvas.drawRect( 0 , height, width, bitmapWithReflection.getHeight() |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> + reflectionGap, paint); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ImageView imageView = new ImageView(mContext); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> imageView.setImageBitmap(bitmapWithReflection); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> return imageView; |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr> <wbr></wbr> |
} |
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
是保证图片绕Y旋转了以后不会出现锯齿.
下面是Activity:
package com.coverflow; |
<wbr></wbr> <wbr></wbr> |
import android.app.Activity; |
import android.graphics.Color; |
import android.os.Bundle; |
<wbr></wbr> <wbr></wbr> |
import com.gallery.R; |
<wbr></wbr> <wbr></wbr> |
public class HelloAndroid extends Activity { |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> @Override |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> public void onCreate(Bundle savedInstanceState) { |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> super .onCreate(savedInstanceState); |
<wbr></wbr> <wbr></wbr> |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> CoverFlow cf = new CoverFlow( this ); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // cf.setBackgroundResource(R.drawable.shape); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> cf.setBackgroundColor(Color.BLACK); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> cf.setAdapter( new ImageAdapter( this )); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ImageAdapter imageAdapter = new ImageAdapter( this ); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> cf.setAdapter(imageAdapter); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // cf.setAlphaMode(false); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // cf.setCircleMode(false); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> cf.setSelection( 2 , true ); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> cf.setAnimationDuration( 1000 ); |
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> setContentView(cf); |
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> } |
<wbr></wbr> <wbr></wbr> |
} |
参考自:
参考自:
http://www.eoeandroid.com/thread-70209-1-1.html
http://www.apkbus.com/android-18441-1-1.html