Android 背景图片重复利用

本文介绍了一种利用小尺寸图片通过拉伸及固定角图片的方式拼接成大背景图的方法,以减少资源文件的大小。此方法适用于多个窗口需要使用相同背景的情况。

最近做一个项目,很多个窗口都重复用到过一个背景图片,为了减小资源文件的容量,做了这个demo,用的是google已经遗弃了的东西做的,大概的说一下我的思路:显示一个大的绝对布局,然后是9个小的绝对布局,分别设置背景为小图片,四个角的图片是固定的(图中的A,B,C,D),其他几张图片都做拉伸处理,大家有什么更好的实现方法吗?

package com.background;

	import android.app.Activity;
	import android.graphics.Bitmap;
	import android.graphics.BitmapFactory;
	import android.graphics.Matrix;
	import android.graphics.drawable.BitmapDrawable;
	import android.os.Bundle;
	import android.widget.AbsoluteLayout;

	@SuppressWarnings("deprecation")
	public class BackgroundActivity extends Activity {
	    /** Called when the activity is first created. */
	        @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);  
	                     
	        setBackground(750,350,R.id.main);
	    }
	       
	    private void setBackground(int width,int height,int id){
	        final int PHOTO_WIDTH = 21;
	        final int PHOTO_HEIGHT=25;
	        //获取父layout
	        AbsoluteLayout layout = (AbsoluteLayout)findViewById(id);               
	        //左上角
	     AbsoluteLayout layoutLeftUp = new AbsoluteLayout(this);
	        layoutLeftUp.setBackgroundResource(R.drawable.a);
	        AbsoluteLayout.LayoutParams lpLeftUp =
	                new AbsoluteLayout.LayoutParams(PHOTO_WIDTH,PHOTO_HEIGHT,0,0);
	        layout.addView(layoutLeftUp,lpLeftUp);
	        //右上角
	     AbsoluteLayout layoutRightUp = new AbsoluteLayout(this);
	        layoutRightUp.setBackgroundResource(R.drawable.b);
	        AbsoluteLayout.LayoutParams lpRightUp =
	                new AbsoluteLayout.LayoutParams(PHOTO_WIDTH,PHOTO_HEIGHT,width-PHOTO_WIDTH,0);
	        layout.addView(layoutRightUp,lpRightUp);
	        //左下角
	     AbsoluteLayout layoutLeftDown = new AbsoluteLayout(this);
	        layoutLeftDown.setBackgroundResource(R.drawable.c);
	        AbsoluteLayout.LayoutParams lpLeftDown =
	                new AbsoluteLayout.LayoutParams(PHOTO_WIDTH,PHOTO_HEIGHT,0,height-PHOTO_HEIGHT);
	        layout.addView(layoutLeftDown,lpLeftDown);
	        //右下角
	     AbsoluteLayout layoutRighttDown = new AbsoluteLayout(this);
	        layoutRighttDown.setBackgroundResource(R.drawable.d);
	        AbsoluteLayout.LayoutParams lpRightDown =
	                new AbsoluteLayout.LayoutParams(PHOTO_WIDTH,PHOTO_HEIGHT,width-PHOTO_WIDTH,height-PHOTO_HEIGHT);
	        layout.addView(layoutRighttDown,lpRightDown);
	        //上
	     AbsoluteLayout layoutUp = new AbsoluteLayout(this);
	        AbsoluteLayout.LayoutParams lpUp =
	                new AbsoluteLayout.LayoutParams(width - PHOTO_WIDTH*2,PHOTO_HEIGHT,PHOTO_WIDTH,0);    
	        Bitmap bmpOrgUp=BitmapFactory.decodeResource(getResources(),R.drawable.f);
	        int widthUp = bmpOrgUp.getWidth();  
	        int heightUp = bmpOrgUp.getHeight();
	        int newWidthUp = width - PHOTO_WIDTH*2;
	        int newHeightUp = PHOTO_HEIGHT;
	        float swUp = ((float)newWidthUp)/widthUp;
	        float shUp = ((float)newHeightUp)/heightUp;
	        Matrix matrixUp=new Matrix();         
	        matrixUp.postScale(swUp,shUp);  
	        Bitmap resizeBitmapUp=Bitmap.createBitmap(bmpOrgUp,0,0,widthUp,heightUp,matrixUp,true);
	        BitmapDrawable bmpUp=new BitmapDrawable(resizeBitmapUp);  
	        layoutUp.setBackgroundDrawable(bmpUp);
	        layout.addView(layoutUp,lpUp);      
	        //下
	     AbsoluteLayout layoutDown = new AbsoluteLayout(this);
	        AbsoluteLayout.LayoutParams lpDown =
	                new AbsoluteLayout.LayoutParams(width - PHOTO_WIDTH*2,PHOTO_HEIGHT,PHOTO_WIDTH,height-PHOTO_HEIGHT);
	        Bitmap bmpOrgDown=BitmapFactory.decodeResource(getResources(),R.drawable.h);
	        int widthDown = bmpOrgDown.getWidth();  
	        int heightDown = bmpOrgDown.getHeight();
	        int newWidthDown = width - PHOTO_WIDTH*2;
	        int newHeightDown = PHOTO_HEIGHT;
	        float swDown = ((float)newWidthDown)/widthDown;
	        float shDown = ((float)newHeightDown)/heightDown;
	        Matrix matrixDown=new Matrix();         
	        matrixDown.postScale(swDown,shDown);  
	        Bitmap resizeBitmapDown=Bitmap.createBitmap(bmpOrgDown,0,0,widthDown,heightDown,matrixDown,true);
	        BitmapDrawable bmpDown=new BitmapDrawable(resizeBitmapDown);  
	        layoutDown.setBackgroundDrawable(bmpDown);
	        layout.addView(layoutDown,lpDown);
	        //左
	     AbsoluteLayout layoutLeft = new AbsoluteLayout(this);
	        AbsoluteLayout.LayoutParams lpLeft =
	                new AbsoluteLayout.LayoutParams(PHOTO_WIDTH,height-PHOTO_HEIGHT*2,0,PHOTO_HEIGHT);
	        Bitmap bmpOrgLeft=BitmapFactory.decodeResource(getResources(),R.drawable.e);
	        int widthLeft = bmpOrgLeft.getWidth();  
	        int heightLeft = bmpOrgLeft.getHeight();
	        int newWidthLeft = PHOTO_WIDTH;
	        int newHeightLeft = height - PHOTO_HEIGHT*2;
	        float swLeft = ((float)newWidthLeft)/widthLeft;
	        float shLeft = ((float)newHeightLeft)/heightLeft;
	        Matrix matrixLeft=new Matrix();         
	        matrixLeft.postScale(swLeft,shLeft);  
	        Bitmap resizeBitmapLeft=Bitmap.createBitmap(bmpOrgLeft,0,0,widthLeft,heightLeft,matrixLeft,true);
	        BitmapDrawable bmpLeft=new BitmapDrawable(resizeBitmapLeft);  
	        layoutLeft.setBackgroundDrawable(bmpLeft);
	        layout.addView(layoutLeft,lpLeft);      
	        //右
	     AbsoluteLayout layoutRight = new AbsoluteLayout(this);
	        AbsoluteLayout.LayoutParams lpRight =
	                new AbsoluteLayout.LayoutParams(PHOTO_WIDTH,height-PHOTO_HEIGHT*2,width-PHOTO_WIDTH,PHOTO_HEIGHT);
	        Bitmap bmpOrgRight=BitmapFactory.decodeResource(getResources(),R.drawable.g);
	        int widthRight = bmpOrgRight.getWidth();  
	        int heightRight = bmpOrgRight.getHeight();
	        int newWidthRight = PHOTO_WIDTH;
	        int newHeightRight = height - PHOTO_HEIGHT*2;
	        float swRight = ((float)newWidthRight)/widthRight;
	        float shRight = ((float)newHeightRight)/heightRight;
	        Matrix matrixRight=new Matrix();         
	        matrixRight.postScale(swRight,shRight);  
	        Bitmap resizeBitmapRight=Bitmap.createBitmap(bmpOrgRight,0,0,widthRight,heightRight,matrixRight,true);
	        BitmapDrawable bmpRight=new BitmapDrawable(resizeBitmapRight);  
	        layoutRight.setBackgroundDrawable(bmpRight);
	        layout.addView(layoutRight,lpRight);       
	        //中
	     AbsoluteLayout layoutCenter = new AbsoluteLayout(this);
	        AbsoluteLayout.LayoutParams lpCenter =
	                new AbsoluteLayout.LayoutParams(width-42,height-PHOTO_HEIGHT*2,PHOTO_WIDTH,PHOTO_HEIGHT);
	        Bitmap bmpOrgCenter=BitmapFactory.decodeResource(getResources(),R.drawable.i);
	        int widthCenter = bmpOrgCenter.getWidth();  
	        int heightCenter = bmpOrgCenter.getHeight();
	        int newWidthCenter = width - PHOTO_WIDTH*2;
	        int newHeightCenter = height - PHOTO_HEIGHT*2;
	        float swCenter = ((float)newWidthCenter)/widthCenter;
	        float shCenter = ((float)newHeightCenter)/heightCenter;
	        Matrix matrixCenter=new Matrix();         
	        matrixCenter.postScale(swCenter,shCenter);  
	        Bitmap resizeBitmapCenter=Bitmap.createBitmap(bmpOrgCenter,0,0,widthCenter,heightCenter,matrixCenter,true);
	        BitmapDrawable bmpCenter=new BitmapDrawable(resizeBitmapCenter);  
	        layoutCenter.setBackgroundDrawable(bmpCenter);
	        layout.addView(layoutCenter,lpCenter);
	        }
	}
 
<?xml version="1.0" encoding="utf-8"?>
	<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:background="@drawable/main_background"
	    >
	    <TextView 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:layout_x="100px"
	        android:layout_y="100px"
	        android:text="被图片覆盖了"
	    /> 
	    <AbsoluteLayout
	        android:layout_width="fill_parent"
	        android:layout_height="fill_parent"
	        android:layout_x="50px"
	        android:layout_y="50px"
	        android:id="@+id/main">
	    </AbsoluteLayout>
	    <TextView 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:layout_x="200px"
	        android:layout_y="200px"
	        android:text="没有被图片覆盖"
	    /> 
	</AbsoluteLayout>

 

   \   


 


转载:http://www.adobex.com/android/source/details/00000430.htm

转载于:https://my.oschina.net/androidcode/blog/105100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值