Android UI開發第二十三篇——分享書架UI實現

來源:互聯網
上載者:User

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://blog.csdn.net/xyz_lmn/article/details/8123929

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.