android书架效果背景图处理

       在android应用中,做一个小说阅读器是很多人的想法,大家一般都是把如何读取大文件,如果在滚动或是翻页时,让用户感觉不到做为重点。我也在做一个类似一功能,可是在做书架的时候,看了QQ阅读的书架,感觉很好看,就想做一个,上网查了一下,真有例子http://blog.csdn.net/wangkuifeng0118/article/details/7944215

       上面的例子很不错,可是有一个问题是他的背景图的宽必须是手机屏幕的宽,不会改变,这样感觉对于手机的适配不太好。就自己动手改了一下。

       不多说,直接说一下原理上代 码。

       在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);
		} 

}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值