Android 編程下背景圖片適配工具類

package cn.sunzn.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

/**
 * 處理背景圖片的一些工具,如放大 縮小...
 */
public class BgUtil {

    /**
     * 使背景圖片適應手機屏幕分辨率
     * 
     * @param context        上下文環境
     * @param screenWidth    手機屏幕寬度
     * @param screenHeight   手機屏幕高度
     * @param resId          背景圖片資源ID
     * @return               返回一個新的適應手機屏幕分辨率的圖片
     */
    public static Bitmap getSuitableBg(Context context, int screenWidth, int screenHeight, int resId) {
        Bitmap bg = BitmapFactory.decodeResource(context.getResources(), resId);
        int bgWidth = bg.getWidth();
        int bgHeight = bg.getHeight();
        float scaleWidth = (float) screenWidth / bgWidth;
        float scaleHeight = (float) screenHeight / bgHeight;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newBg = Bitmap.createBitmap(bg, 0, 0, bgWidth, bgHeight, matrix, true);
        return newBg;
    }
}
相關文章
相關標籤/搜索