Android 用CoverFlow的效果

我们还是先来看看效果图有多么的好看吧

3.png

       此类是从Gallery继承过来的,用法自然也就和Gallery一样了,程序的背景是一个xml陪的渐变背景,具体可以参看另外一篇”渐变背景”的文章。图片的倒影效果是适用了ReflectionImage控件,具体可以参看上一篇博文.

       先看代码吧,看了代码什么都明白了

Java代码:
  1. package eoe.view;

  2. import android.content.Context;
  3. import android.graphics.Camera;
  4. import android.graphics.Matrix;
  5. import android.util.AttributeSet;
  6. import android.view.View;
  7. import android.view.animation.Transformation;
  8. import android.widget.Gallery;
  9. import android.widget.ImageView;

  10. public class CoverFlow extends Gallery {

  11. private Camera mCamera = new Camera();
  12. private int mMaxRotationAngle = 50;
  13. private int mMaxZoom = -380;
  14. private int mCoveflowCenter;
  15. private boolean mAlphaMode = true;
  16. private boolean mCircleMode = false;

  17. public CoverFlow(Context context) {
  18. super(context);
  19. this.setStaticTransformationsEnabled(true);
  20. }
  21. public CoverFlow(Context context, AttributeSet attrs) {
  22. super(context, attrs);
  23. this.setStaticTransformationsEnabled(true);
  24. }
  25. public CoverFlow(Context context, AttributeSet attrs, int defStyle) {
  26. super(context, attrs, defStyle);
  27. this.setStaticTransformationsEnabled(true);
  28. }
  29. public int getMaxRotationAngle() {
  30. return mMaxRotationAngle;
  31. }
  32. public void setMaxRotationAngle(int maxRotationAngle) {
  33. mMaxRotationAngle = maxRotationAngle;
  34. }
  35. public boolean getCircleMode() {
  36. return mCircleMode;
  37. }
  38. public void setCircleMode(boolean isCircle) {
  39. mCircleMode = isCircle;
  40. }
  41. public boolean getAlphaMode() {
  42. return mAlphaMode;
  43. }
  44. public void setAlphaMode(boolean isAlpha) {
  45. mAlphaMode = isAlpha;
  46. }
  47. public int getMaxZoom() {
  48. return mMaxZoom;
  49. }
  50. public void setMaxZoom(int maxZoom) {
  51. mMaxZoom = maxZoom;
  52. }
  53. private int getCenterOfCoverflow() {
  54. return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2+ getPaddingLeft();
  55. }
  56. private static int getCenterOfView(View view) {
  57. return view.getLeft() + view.getWidth() / 2;
  58. }
  59. protected boolean getChildStaticTransformation(View child, Transformation t) {
  60. final int childCenter = getCenterOfView(child);
  61. final int childWidth = child.getWidth();
  62. int rotationAngle = 0;
  63. t.clear();
  64. t.setTransformationType(Transformation.TYPE_MATRIX);
  65. if (childCenter == mCoveflowCenter) {
  66. transformImageBitmap((ImageView) child, t, 0);
  67. } else {
  68. rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
  69. if (Math.abs(rotationAngle) > mMaxRotationAngle) {
  70. rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle: mMaxRotationAngle;
  71. }
  72. transformImageBitmap((ImageView) child, t, rotationAngle);
  73. }
  74. return true;
  75. }
  76. /**
  77. * 这就是所谓的在大小的布局时,这一观点已经发生了改变。如果
  78. * 你只是添加到视图层次,有人叫你旧的观念
  79. * 价值观为0。
  80. *
  81. * @param w
  82. * Current width of this view.
  83. * @param h
  84. * Current height of this view.
  85. * @param oldw
  86. * Old width of this view.
  87. * @param oldh
  88. * Old height of this view.
  89. */
  90. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  91. mCoveflowCenter = getCenterOfCoverflow();
  92. super.onSizeChanged(w, h, oldw, oldh);
  93. }
  94. /**
  95. * 把图像位图的角度通过
  96. *
  97. * @param imageView
  98. * ImageView the ImageView whose bitmap we want to rotate
  99. * @param t
  100. * transformation
  101. * @param rotationAngle
  102. * the Angle by which to rotate the Bitmap
  103. */
  104. private void transformImageBitmap(ImageView child, Transformation t,
  105. int rotationAngle) {
  106. mCamera.save();
  107. final Matrix imageMatrix = t.getMatrix();
  108. final int imageHeight = child.getLayoutParams().height;
  109. final int imageWidth = child.getLayoutParams().width;
  110. final int rotation = Math.abs(rotationAngle);
  111. mCamera.translate(0.0f, 0.0f, 100.0f);

  112. // 如视图的角度更少,放大
  113. if (rotation <= mMaxRotationAngle) {
  114. float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
  115. mCamera.translate(0.0f, 0.0f, zoomAmount);
  116. if (mCircleMode) {
  117. if (rotation < 40)
  118. mCamera.translate(0.0f, 155, 0.0f);
  119. else
  120. mCamera.translate(0.0f, (255 - rotation * 2.5f), 0.0f);
  121. }
  122. if (mAlphaMode) {
  123. ((ImageView) (child)).setAlpha((int) (255 - rotation * 2.5));
  124. }
  125. }
  126. mCamera.rotateY(rotationAngle);
  127. mCamera.getMatrix(imageMatrix);
  128. imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
  129. imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
  130. mCamera.restore();
  131. }
  132. }
复制代码

       这个就是 CoverFlow类,说明几点

       1. 成员函数

       mCamera是用来做类3D效果处理,比如z轴方向上的平移,绕y轴的旋转等
       mMaxRotationAngle是图片绕y轴最大旋转角度,也就是屏幕最边上那两张图片的旋转角度
       mMaxZoom是图片在z轴平移的距离,视觉上看起来就是放大缩小的效果.
       其他的变量都可以无视

       也就是说把这个属性设成 true的时候每次 viewGroup(看Gallery的源码就可以看到它是从 ViewGroup间接继承过来的)在重新画它的 child的时候都会促发 getChildStaticTransformation这个函数,所以我们只需要在这个函数里面去加上旋转和放大的操作就可以了

       其他的 gettersetter函数都可以无视

Java代码:
  1. package eoe.hello;


  2. import com.hello.R;
  3. import com.myview.*;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.graphics.drawable.BitmapDrawable;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.BaseAdapter;
  11. import android.widget.ImageView;


  12. public class HelloAndroid extends Activity {
  13. /** Called when the activity is first created. */
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);

  17. CoverFlow cf = new CoverFlow(this);
  18. cf.setBackgroundResource(R.drawable.shape);
  19. cf.setAdapter(new ImageAdapter(this));
  20. ImageAdapter imageAdapter = new ImageAdapter(this);
  21. cf.setAdapter(imageAdapter);
  22. // cf.setAlphaMode(false);
  23. // cf.setCircleMode(false);
  24. // cf.setSelection(3, true);
  25. cf.setAnimationDuration(1000);
  26. setContentView(cf);
  27. }

  28. public class ImageAdapter extends BaseAdapter {
  29. int mGalleryItemBackground;
  30. private Context mContext;
  31. private Integer[] mImageIds = {
  32. R.drawable.a,
  33. R.drawable.b,
  34. R.drawable.d,
  35. R.drawable.e,
  36. R.drawable.c};

  37. public ImageAdapter(Context c) {
  38. mContext = c;
  39. }
  40. public int getCount() {
  41. return mImageIds.length;
  42. }
  43. public Object getItem(int position) {
  44. return position;
  45. }
  46. public long getItemId(int position) {
  47. return position;
  48. }
  49. public View getView(int position, View convertView, ViewGroup parent) {
  50. ReflectionImage i = new ReflectionImage(mContext);

  51. i.setImageResource(mImageIds[position]);
  52. i.setLayoutParams(new CoverFlow.LayoutParams(96, 76));
  53. i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
  54. // 确保我们设置的抗锯齿否则我们得到jaggies
  55. BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
  56. drawable.setAntiAlias(true);
  57. return i;
  58. }

  59. public float getScale(boolean focused, int offset) {
  60. return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
  61. }
  62. }
  63. }
复制代码

       BitmapDrawable drawable = (BitmapDrawable) i.getDrawable(); drawable.setAntiAlias(true);是保证图片绕Y旋转了以后不会出现锯齿.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值