图像背景根据屏幕大小而定,无需使用九个补丁

发布于 2024-12-19 05:48:50 字数 312 浏览 4 评论 0原文

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

我需要在每种尺寸的屏幕中在屏幕(fill_parent)中显示此图像背景。

我该怎么办?由于图像的几何问题,我无法使用九个补丁。我需要制作所有尺寸的图像吗?

谢谢!

I have an image background like the first image of squareup image: https://market.android.com/details?id=com.squareup.cardcase&hl=fr

I need to display this image background in the screen (fill_parent) in every size of screen.

How can I do? I can't use nine patch due to geometric issue of the image. Do I need to make the image in all the size?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2024-12-26 05:48:50

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

获得生成的位图后,将其作为 ImageView 的内容放置在显示器上。

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

我创建了 ImageView 类的子类来封装调整大小和裁剪。 value 的唯一方法是重写的 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);
            }
        }
    }

}

In code (rather than via the layout XML) you can create a bitmap of the right aspect ratio (device height x width) by cropping the image after scaling the bitmap to be big enough to crop. You need to make sure that the image can be scaled up/down (preferably down) without losing sharpness. You also need to be sure that important information will not be lost when the image is cropped with different aspect ratios.

Once you have the resulting bitmap then place it on the display as an ImageView's content.

I find that it is better to size logos separately from underlying images and layer image views on on top of each other so that the text remains crisp.

I created a subclass of the ImageView class to encapsulate the resizing and cropping. The only method of value is the overridden onMeasure() method:

/**
 * 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);
            }
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文