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
* iOS入门群:83702688
* android开发进阶群:241395671
*/
分享到:
相关推荐
UI开发第六篇——仿QQ的滑动TabUI开发第六篇——仿QQ的滑动Tab
Android UI开发第十六篇——分享一个popuwindow实例 http://blog.csdn.net/xyz_lmn/article/details/6921097
UI开发第八篇——ViewFlipper 左右滑动效果UI开发第八篇——ViewFlipper 左右滑动效果
UI开发第九篇——SlidingDrawer 抽屉效果 UI开发第九篇——SlidingDrawer 抽屉效果
Android UI开发第二十一篇——下拉刷新列表实现(pull to refresh) http://blog.csdn.net/xyz_lmn/article/details/8101360
Android UI开发——SlidingDrawer 抽屉效果~~
Android UI开发专题 Android UI开发专题(一) 之界面设计 近期很多网友对Android用户界面的设计表示很感兴趣,对于Android UI开发自绘控件和游戏制作而言掌握好绘图基础是必不可少的。本次专题分10节来讲述,有关...
安卓Android源码——ui开发类库示例源码.zip
用Android实现的书架效果的UI的源码
本示例利用了Jquery-UI实现Web桌面系统jWebOS,左侧菜单和桌面菜单的链接都是动态设置。整体效果非常棒。大家可以用于自己的网站建设。 建议开发童鞋使用跨平台开发工具——统一开发环境UDE来进行查看、调试、开发...
android UI开发推荐颜色 android UI开发推荐颜色 android UI开发推荐颜色
《Android应用程序开发教程(第2版)》教学课件02Android UI设计.pdf《Android应用程序开发教程(第2版)》教学课件02Android UI设计.pdf《Android应用程序开发教程(第2版)》教学课件02Android UI设计.pdf《Android应用...
QMUI 团队出品,一个致力于提高 Android 项目 UI 开发效率的解决方案
Android开发笔记,UI基础编程,PDF文档。
教程名称: 老罗Android开发视频教程-Android常用UI控件编程【32集】【】Android常用UI控件编程第七集【】Android常用UI控件编程第二十三集【】Android常用UI控件编程第二十九集【】Android常用UI控件编程第二十二...
安卓Android源码——UI界面源码.zip
Android UI开发经典资料,学习设计Android界面的必备资料
详细信息可见博客,Android:shape开发详解-UI设计
android UI界面开发图片,数量巨大,而且经典,觉得值得收藏 android UI界面开发图片,数量巨大,而且经典,觉得值得收藏