现在的位置: 首页 > 综合 > 正文

分享书架UI实现

2013年09月07日 ⁄ 综合 ⁄ 共 3810字 ⁄ 字号 评论关闭
  1. public class ShelvesView extends GridView {  
  2.     private Bitmap mShelfBackground;  
  3.     private int mShelfWidth;  
  4.     private int mShelfHeight;  
  5.   
  6.     private Bitmap mWebLeft;  
  7.     private Bitmap mWebRight;  
  8.     private int mWebRightWidth;  
  9.   
  10.     public ShelvesView(Context context) {  
  11.         super(context);  
  12.         init(context);  
  13.     }  
  14.   
  15.     public ShelvesView(Context context, AttributeSet attrs) {  
  16.         super(context, attrs);  
  17.         load(context, attrs, 0);  
  18.         init(context);  
  19.     }  
  20.   
  21.     public ShelvesView(Context context, AttributeSet attrs, int defStyle) {  
  22.         super(context, attrs, defStyle);  
  23.         load(context, attrs, defStyle);  
  24.         init(context);  
  25.     }  
  26.   
  27.     private void load(Context context, AttributeSet attrs, int defStyle) {  
  28.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShelvesView, defStyle, 0);  
  29.   
  30.         final Resources resources = getResources();  
  31.         final int background = a.getResourceId(R.styleable.ShelvesView_shelfBackground, 0);  
  32.         final Bitmap shelfBackground = BitmapFactory.decodeResource(resources, background);  
  33.         if (shelfBackground != null) {  
  34.             mShelfWidth = shelfBackground.getWidth();  
  35.             mShelfHeight = shelfBackground.getHeight();  
  36.             mShelfBackground = shelfBackground;  
  37.         }  
  38.   
  39.         mWebLeft = BitmapFactory.decodeResource(resources, R.drawable.web_left);  
  40.   
  41.         final Bitmap webRight = BitmapFactory.decodeResource(resources, R.drawable.web_right);  
  42.         mWebRightWidth = webRight.getWidth();  
  43.         mWebRight = webRight;  
  44.   
  45.         a.recycle();  
  46.     }  
  47.   
  48.     private void init(Context context) {  
  49.         StateListDrawable drawable = new StateListDrawable();  
  50.   
  51.         SpotlightDrawable start = new SpotlightDrawable(context, this);  
  52.         start.disableOffset();  
  53.         SpotlightDrawable end = new SpotlightDrawable(context, this, R.drawable.spotlight_blue);  
  54.         end.disableOffset();  
  55.         TransitionDrawable transition = new TransitionDrawable(start, end);  
  56.         drawable.addState(new int[] { android.R.attr.state_pressed },  
  57.                 transition);  
  58.   
  59.         final SpotlightDrawable normal = new SpotlightDrawable(context, this);  
  60.         drawable.addState(new int[] { }, normal);  
  61.   
  62.         normal.setParent(drawable);  
  63.         transition.setParent(drawable);  
  64.   
  65.         setSelector(drawable);  
  66.         setDrawSelectorOnTop(false);  
  67.     }  
  68.   
  69.     @Override  
  70.     protected void dispatchDraw(Canvas canvas) {  
  71.         final int count = getChildCount();  
  72.         final int top = count > 0 ? getChildAt(0).getTop() : 0;  
  73.         final int shelfWidth = mShelfWidth;  
  74.         final int shelfHeight = mShelfHeight;  
  75.         final int width = getWidth();  
  76.         final int height = getHeight();  
  77.         final Bitmap background = mShelfBackground;  
  78.   
  79.         for (int x = 0; x < width; x += shelfWidth) {  
  80.             for (int y = top; y < height; y += shelfHeight) {  
  81.                 canvas.drawBitmap(background, x, y, null);  
  82.             }  
  83.         }  
  84.   
  85.         if (count == 0) {  
  86.             canvas.drawBitmap(mWebLeft, 0.0f, top + 1null);  
  87.             canvas.drawBitmap(mWebRight, width - mWebRightWidth, top + shelfHeight + 1null);  
  88.         }  
  89.   
  90.         super.dispatchDraw(canvas);  
  91.     }  
  92.   
  93.       
  94.   
  95.     @Override  
  96.     public void setPressed(boolean pressed) {  
  97.         super.setPressed(pressed);  
  98.   
  99.         final Drawable current = getSelector().getCurrent();  
  100.         if (current instanceof TransitionDrawable) {  
  101.             if (pressed) {  
  102.                 ((TransitionDrawable) current).startTransition(  
  103.                         ViewConfiguration.getLongPressTimeout());  
  104.             } else {  
  105.                 ((TransitionDrawable) current).resetTransition();  
  106.             }  
  107.         }  

抱歉!评论已关闭.