栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android编程之书架效果背景图处理方法

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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> 2,
    MeasureSpec.AT_MOST);
   super.onMeasure(widthMeasureSpec, expandSpec);
  }
}

希望本文所述对大家Android程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/163698.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号