发布于 2016-05-19 03:23:31 | 144 次阅读 | 评论: 0 | 来源: 网友投递
Android移动端操作系统
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。
这篇文章主要介绍了Android编程之书架效果背景图处理方法,在前面一篇《android书架效果实现原理与代码》的基础上做了一定的修改,重写GridView类处理了背景图效果,需要的朋友可以参考下
本文实例讲述了Android编程之书架效果背景图处理方法。分享给大家供大家参考,具体如下:
在android应用中,做一个小说阅读器是很多人的想法,大家一般都是把如何读取大文件,如果在滚动或是翻页时,让用户感觉不到做为重点。我也在做一个类似一功能,可是在做书架的时候,看了QQ阅读的书架,感觉很好看,就想做一个,前面一篇《android书架效果实现原理与代码》对此做了专门介绍
上面的例子很不错,可是有一个问题是他的背景图的宽必须是手机屏幕的宽,不会改变,这样感觉对于手机的适配不太好。就自己动手改了一下。
不多说,直接说一下原理上代码。
在gridView中为一列加背景,没有别的方法,只能重写GridView类,在这个类里,你加载一个背景图来处理,我的做法就是在加载完背景图后,重新根据你的手机的宽绘一个新图,这样你的背景图,就可以很好看的显示出了。
这里我只放出重写的GridView类。
public class myGridView extends GridView {
private Bitmap background;
private Bitmap newBackGround;
public myGridView(Context context, AttributeSet attrs) {
super(context, attrs);
//加载做为背景的图片
background = BitmapFactory.decodeResource(getResources(), R.drawable.bookcase_bg);
}
protected void dispatchDraw(Canvas canvas) {
//取得加载的背景图片高,宽
int width = background.getWidth();
int height = background.getHeight();
//取得手机屏幕的高,宽,这里要注意,不要在构造方法中写,因为那样取到的值是0
int scWidth = this.getWidth();
int scHeight = this.getHeight();
//计算缩放率,新尺寸除原始尺寸,我这里因为高不用变,所以就是原大小的比例1
//这里一定要注意,这里的值是比例,不是值。
float scaleWidth = ((float) scWidth) / width;
float scaleHeight = 1;
//Log.v("info", "width:" + width + "height:" + height + "scWidth:" + scWidth + "scaleWidth:" + scaleWidth + "scaleHeight:" + scaleHeight);
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
//生成新的图片
newBackGround = Bitmap.createBitmap(background, 0, 0,
width, height, matrix, true);
int count = getChildCount();
//Log.v("myGridView-Count", count + "");
int top = 185;
//Log.v("getChildAt", getChildAt(0).getTop() + "");
int backgroundWidth = newBackGround.getWidth();
int backgroundHeight = newBackGround.getHeight();
for (int y = top; y<scHeight; y += 223){
//for (int x = 0; x<scWidth; x += backgroundWidth){
canvas.drawBitmap(newBackGround, 0, y, null);
//}
}
super.dispatchDraw(canvas);
}
//禁止滚动 条滚动
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
希望本文所述对大家Android程序设计有所帮助。