图像背景根据屏幕的大小而不使用九个贴片



我有一个像squareup的第一个图像的图像背景:https://market.android.com/details?id=com.squareup.cardcase&hl=fr

我需要在屏幕上显示这个图像背景(fill_parent)在各种尺寸的屏幕。

我该怎么办?由于图像的几何问题,我不能使用9个补丁。我是否需要将图像设置为所有尺寸?

谢谢!

在代码中(而不是通过布局XML),您可以在将位图缩放到足够大后裁剪图像,从而创建具有正确长宽比(设备高度x宽度)的位图。你需要确保图像可以放大/缩小(最好是缩小)而不会失去清晰度。您还需要确保在使用不同的长宽比裁剪图像时不会丢失重要信息。

一旦你有了结果位图,然后把它作为ImageView的内容放在显示器上。

我发现最好将徽标的大小与底层图像分开,并将图像视图层叠在一起,以便文本保持清晰。

我创建了ImageView类的一个子类来封装调整大小和裁剪。唯一有价值的方法是被覆盖的onMeasure()方法:

/**
 * Override the onMeasure method to resize the Bitmap as needed
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    Drawable currentDrawable = this.getDrawable();
    BitmapDrawable theBitmapDrawable;
    if (BitmapDrawable.class.isInstance(currentDrawable)){
        // We have a bitmap to work with
        theBitmapDrawable = (BitmapDrawable) currentDrawable;
        Bitmap currentBitmap = theBitmapDrawable.getBitmap();
        Bitmap resizedBitmap = null;
        if (currentBitmap != null) {
            int currentHeight = currentBitmap.getHeight();
            int currentWidth = currentBitmap.getWidth();
            int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
            int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
            if ((currentHeight != parentHeight) || (currentWidth != parentWidth)) {
                // The bitmap needs to be resized, and/or cropped to fit
                if ((currentHeight < parentHeight) || (currentWidth < parentWidth)) {
                    // Need to make the bitmap larger
                    float heightFactor = (float) parentHeight / (float) currentHeight;
                    float widthFactor = (float) parentWidth / (float) currentWidth;
                    float scaleFactor;
                    // Choose the largest factor
                    if (Float.compare(heightFactor, widthFactor) < 0) {
                        scaleFactor = widthFactor;
                    } else {
                        scaleFactor = heightFactor;
                    }
                    int dstWidth = (int) (currentWidth * scaleFactor);
                    int dstHeight = (int) (currentHeight * scaleFactor);
                    if (dstWidth < parentWidth) dstWidth = parentWidth;     // Deal with off by one rounding errors
                    if (dstHeight < parentHeight) dstHeight = parentHeight; // Deal with off by one rounding errors
                    resizedBitmap = Bitmap.createScaledBitmap(currentBitmap, dstWidth, dstHeight, true);
                    currentBitmap.recycle();
                } else if ((currentHeight > parentHeight) && (currentWidth > parentWidth)){
                    // Need to make the splash screen bitmap smaller
                    float heightFactor = (float) parentHeight / (float) currentHeight;
                    float widthFactor = (float) parentWidth / (float) currentWidth;
                    float scaleFactor;
                    // Choose the largest factor
                    if (Float.compare(heightFactor, widthFactor) < 0) {
                        scaleFactor = widthFactor;
                    } else {
                        scaleFactor = heightFactor;
                    }
                    int dstWidth = (int) (currentWidth * scaleFactor);
                    int dstHeight = (int) (currentHeight * scaleFactor);
                    if (dstWidth < parentWidth) dstWidth = parentWidth;     // Deal with off by one rounding errors
                    if (dstHeight < parentHeight) dstHeight = parentHeight; // Deal with off by one rounding errors
                    resizedBitmap = Bitmap.createScaledBitmap(currentBitmap, dstWidth, dstHeight, true);
                    currentBitmap.recycle();
                } else {
                    // No need to resize the image - we'll just need to crop it
                    resizedBitmap = currentBitmap;
                }
                // Now crop the image so that it fits the aspect ratio of the screen
                currentHeight = resizedBitmap.getHeight();
                currentWidth = resizedBitmap.getWidth();
                Bitmap newBitmap;
                if ((currentHeight != parentHeight) || (currentWidth != parentWidth)) {
                    // Crop the image to fit exactly
                    int startX = (currentWidth - parentWidth)/2;
                    if (startX < 0) startX = 0; // Hmm!
                    int startY = (currentHeight - parentHeight)/2;
                    if (startY < 0) startY = 0; // Hmm! again
                    newBitmap = Bitmap.createBitmap(resizedBitmap, startX, startY, parentWidth, parentHeight);
                    resizedBitmap.recycle();
                } else {
                    // The resized image is the exact right size
                    newBitmap = resizedBitmap;
                }
                this.setImageBitmap(newBitmap);
            }
        }
    }
}

最新更新