【问题标题】:Android background image croppingAndroid背景图片裁剪
【发布时间】:2016-01-19 08:23:19
【问题描述】:

首先要说我是 android 开发的新手,但我有使用 c++、python、php&mysql 以及 html 和 css 开发的经验。

我正在玩基于文本的在线多人在线游戏。最近他们增加了使用您的 API 密钥来访问个人帐户信息的功能(例如查看您的能量、冷却时间等)。所以我开始想到我会制作像“手表”这样的应用程序,它会在例如你的冷却时间结束了。所以你唯一要做的就是输入你的 api 密钥。如果我想让这个应用程序正常工作(没有时间框架,因为首先将用于我的使用)我当然会与其他用户分享它。总之,解决问题!

我在 google 和 stackoverflow 上搜索类似的问题,我偶然发现的唯一有用的是:Android crop background,但这并不能完全解决我的问题。

我的图片尺寸为 496x60。如果设备宽度小于图像宽度,我希望此图像水平裁剪,并具有 100% 的高度。这是我正在使用的图片(来自官方电脑应用,但没有适用于安卓手机的应用):logotorn1(链接在末尾)

现在我到目前为止所做的(我有按钮、输入键框和一些文本但删除了它们,所以只有这个 atm),是这个(我在我的 huawei y330 手机上运行该应用程序): 1

这张图片看起来有点挤,所以我决定手动裁剪第一个并用当前图片替换它。这是第二张图片:logotorn2

这是结果:2

这是我目前的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
    android:layout_height="fill_parent"  tools:context=".MainActivity"
    android:foregroundGravity="top"
    android:gravity="top"
    android:background="@drawable/bgimg">


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/imageView"
            android:background="@drawable/lel"
            android:layout_gravity="left|top"
            android:scaleType="centerCrop"/>
    </FrameLayout>
</RelativeLayout>

MainActivity.java 代码无关(?)atm,所以我不会发布它。

所以,问题是,我忽略了什么或者我做错了什么?

提前感谢您提供的任何帮助。

赫克托

附:我已经在一个链接中发布了所有图片,因为我的声誉似乎只能发布 2 个链接。

images

【问题讨论】:

  • scaleType 将是您应该查看的位置以及图像的方向。 techrepublic.com/article/…这个链接应该可以帮助你理解。我会根据您的情况尝试center
  • 看来这真的很有帮助。我很感激艾略特。谢谢。

标签: android


【解决方案1】:

解决方案比我想象的要简单得多。问题出在android:background 而不是android:src。我确实将 java 代码与位图变量和 OnMeasure 一起实现了,但是在仔细研究之后,不需要任何 java 代码。因此,如果您在背景裁剪方面遇到问题,请首先检查您的图像是否包含 android:src="@drawable/name_of_your_image" 而不是 android:background="@drawable/name_of_your_image"

感谢大家的帮助,抱歉打扰了。

赫克托

【讨论】:

    【解决方案2】:

    如果我的想法是正确的,您可以尝试以编程方式从您的 activity.java 中获取设备宽度(以像素为单位)

    Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x;

    然后将位图设置为您希望使用的完整图像

    Bitmap b = (Bitmap)findViewById(R.drawable.YOUR_IMAGE)

    并使用

    调整此位图的宽度

    b = Bitmap.createBitmap(b, 0, 0, width, ORIGINAL_HEIGHT);

    其中“width”是先前获得的设备宽度(以像素为单位),“ORIGINAL_HEIGHT”是图像的原始高度(以像素为单位)。

    然后您可以将这个新的裁剪图像设置为您在布局中设置的图像视图

    ImageView iv = (ImageView)findViewById(R.layout.YOUR_IMAGE_VIEW); iv.setImageBitmap(b)

    我希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      在您的 ImageView 中尝试 android:maxWidth="496px"。这样,只有当视图小于 496 像素(图像的宽度)时,它才会水平缩放。另外,请查看this question,我认为这两个答案都可能对您有所帮助。

      【讨论】:

        【解决方案4】:

        反正我没有确切的解决方案,我之前一直在用 Android 裁剪图像:

         public static ArrayList<Bitmap> splitImage(Context context, Bitmap bitmap, int rows, int cols) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
        
            int chunkNumbers = rows * cols;
            ArrayList<Bitmap> chunkedImages = new ArrayList<>(chunkNumbers);
            int chunkHeight = bitmap.getHeight() / rows;
            int chunkWidth = bitmap.getWidth() / cols;
            Log.d("CHUNK",chunkHeight + " " + chunkWidth);
            int xCoord;
            int yCoord = 0;
            for (int x = 0; x < rows; x++) {
                xCoord = 0;
                for (int y = 0; y < cols; y++) {
                    chunkedImages.add(Bitmap.createBitmap(bitmap, xCoord, yCoord, chunkWidth, chunkHeight));
                    xCoord += chunkWidth;
                }
                yCoord += chunkHeight;
            }
            bitmap.recycle();
            System.gc();
            return chunkedImages;
        }
        

        此方法以一定数量的块裁剪图像。 顺便说一句,您必须获取屏幕设备的真实宽度和高度,您可以通过覆盖 onMeasure 来获取它。

        希望对你有帮助,再见!

        【讨论】:

        • 也谢谢达尼洛。这个想法出现在我的脑海中。我已经有了 onMeasure 代码,因为我试图用它做点什么。我认为应该有更简单的方法,而不是用 Java 编写一堆代码(我可以阅读 Java,但从未在其中开发过任何东西)。再次感谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-02
        • 2013-12-03
        • 1970-01-01
        • 2014-03-14
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        相关资源
        最近更新 更多