android中有很多的电子书阅读应用,应用中都仿真了书架的实现,这里也分享一篇读书应用的书架实现:

java:

public class ShelvesView extends GridView {     private Bitmap mShelfBackground;     private int mShelfWidth;     private int mShelfHeight;      private Bitmap mWebLeft;     private Bitmap mWebRight;     private int mWebRightWidth;      public ShelvesView(Context context) {         super(context);         init(context);     }      public ShelvesView(Context context, AttributeSet attrs) {         super(context, attrs);         load(context, attrs, 0); init(context);     }      public ShelvesView(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);         load(context, attrs, defStyle);         init(context);     }      private void load(Context context, AttributeSet attrs, int defStyle) {         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShelvesView, defStyle, 0);          final Resources resources = getResources();         final int background = a.getResourceId(R.styleable.ShelvesView_shelfBackground, 0);         final Bitmap shelfBackground = BitmapFactory.decodeResource(resources, background);         if (shelfBackground != null) {             mShelfWidth = shelfBackground.getWidth();             mShelfHeight = shelfBackground.getHeight();             mShelfBackground = shelfBackground;         }          mWebLeft = BitmapFactory.decodeResource(resources, R.drawable.web_left);          final Bitmap webRight = BitmapFactory.decodeResource(resources, R.drawable.web_right);         mWebRightWidth = webRight.getWidth();         mWebRight = webRight;          a.recycle();     }      private void init(Context context) {         StateListDrawable drawable = new StateListDrawable();          SpotlightDrawable start = new SpotlightDrawable(context, this);         start.disableOffset();         SpotlightDrawable end = new SpotlightDrawable(context, this, R.drawable.spotlight_blue);         end.disableOffset();         TransitionDrawable transition = new TransitionDrawable(start, end);         drawable.addState(new int[] { android.R.attr.state_pressed },                 transition);          final SpotlightDrawable normal = new SpotlightDrawable(context, this);         drawable.addState(new int[] { }, normal);          normal.setParent(drawable);         transition.setParent(drawable);          setSelector(drawable);         setDrawSelectorOnTop(false);     }      @Override     protected void dispatchDraw(Canvas canvas) {         final int count = getChildCount();         final int top = count > 0 ? getChildAt(0).getTop() : 0;         final int shelfWidth = mShelfWidth;         final int shelfHeight = mShelfHeight;         final int width = getWidth();         final int height = getHeight();         final Bitmap background = mShelfBackground;          for (int x = 0; x < width; x += shelfWidth) {             for (int y = top; y < height; y += shelfHeight) {                 canvas.drawBitmap(background, x, y, null);             }         }          if (count == 0) {             canvas.drawBitmap(mWebLeft, 0.0f, top + 1, null);             canvas.drawBitmap(mWebRight, width - mWebRightWidth, top + shelfHeight + 1, null);         }          super.dispatchDraw(canvas);     }            @Override     public void setPressed(boolean pressed) {         super.setPressed(pressed);          final Drawable current = getSelector().getCurrent();         if (current instanceof TransitionDrawable) {             if (pressed) {                 ((TransitionDrawable) current).startTransition(                         ViewConfiguration.getLongPressTimeout());             } else {                 ((TransitionDrawable) current).resetTransition();             }         }     } } 


代码:http://download.csdn.net/detail/xyz_lmn/4698124


/**
* @author 张兴业
* 邮箱:xy-zhang#163.com
* android开发进阶群:278401545
*
*/


更多相关文章

  1. android win8效果实现进阶(一)
  2. Android菜鸟进阶之路一-Android基础
  3. Android知识体系总结(全方面覆盖Android知识结构,面试&进阶)
  4. Android高手进阶教程(七)----Android 中Preferences的使用!
  5. Android入门进阶教程(12)-SystemService详解

随机推荐

  1. Android操作SQLite数据库(增、删、改、查
  2. 学员_国培阶段性学习总结
  3. VisionMobile: 开放管控指数(三) -- 开源管
  4. android app 开发过程中 对于性能优化的
  5. 【Activity&Task&Stack】Android 中 Acti
  6. 实例+详解剖析Android之自定义View
  7. 最近学习Android的一些体会
  8. 利用Android SDK免root权限抓屏
  9. 八款常见的Android游戏引擎
  10. 2017 Android暑期实习生面试经验谈