手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android编程之书架效果背景图处理方法
Android编程之书架效果背景图处理方法
摘要:本文实例讲述了Android编程之书架效果背景图处理方法。分享给大家供大家参考,具体如下:在android应用中,做一个小说阅读器是很多人的...

本文实例讲述了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程序设计有所帮助。

【Android编程之书架效果背景图处理方法】相关文章:

Android实现多线程断点下载的方法

一种Android 类似电影倒计时效果

Android中使用sax解析xml文件的方法

Android控件系列之EditText使用方法

Android控件系列之ImageView使用方法

Android ViewPager相册横向移动的实现方法

Android:多线程之图文混排

Android 完全退出应用程序的解决方法

Android 异步获取网络图片并处理导致内存溢出问题解决方法

Android设置桌面背景图片的实现方法

精品推荐
分类导航