2015-10-20 183 views
1

首先要说的是我是新来的android开发,但我有经验与发展中的c + +,python,php & mysql以及HTML和CSS当然。Android背景图像裁剪

我正在玩基于在线文本的mmorpg。最近,他们增加了使用API​​密钥的能力,以便访问个人帐户信息(例如查看您的能量,冷却时间等)。所以我想出了一个想法,我会制作像“watch”这样的应用程序,这会在例如推送通知时推送给您。你的冷却时间结束了。所以你唯一需要做的就是输入你的API密钥。如果我想让这个应用程序工作(没有时间表,因为首先会用于我的使用),我当然会与其他用户分享。总之,解决问题!

我在寻找类似的问题谷歌和stackoverflow,唯一我偶然发现有用的是这样的:Android crop background,但是这不是退出解决我的问题。

我有一个大小496x60的图像。如果设备宽度小于图像宽度,并且要有100%的高度,我希望将此图像水平裁剪。这是我正在使用的图像(从官方PC应用程序,但没有应用程序的Android手机):logotorn1(链接在最后)

现在我到目前为止所做的(我有按钮,输入密钥框,和一些文本,但删除它们,所以它只是这个atm),这是(我在我的华为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,所以我不会发表它。

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

在此先感谢您提供的任何帮助。

Hector

P.S.我已将所有图片发布在一个链接中,因为我的名誉似乎只能发布2个链接。

images

+0

'scaleType'应该是您应该看的地方,以及图像的方向。 http://www.techrepublic.com/article/clear-up-ambiguity-about-android-image-view-scale-types-with-this-guide/这个链接应该有助于你的理解。我会为你的情况尝试“中心”。 – ElliotM

+0

看来这将会非常有帮助。我很感激艾略特。谢谢。 – Hector25200

回答

0

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

谢谢大家的帮助,并抱歉打扰你。

Hector

0

我没有确切的解决方案,反正以前我对与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; 
} 

这种方法裁剪图像中chunkes的一定numeber。 顺便说一句,要获得你的你必须获得你的屏幕设备的真正的宽度和高度,你可以通过覆盖onMeasure获得它。

希望它能帮助你,再见!

+0

谢谢Danilo。我的想法出现了这个想法。我已经有了onMeasure代码,因为我正在尝试使用它。我认为应该有更简单的方法,而不是在Java中编写一堆代码(我可以阅读Java,但从未开发任何内容)。再次感谢。 – Hector25200

0

尝试android:maxWidth="496px"到您的ImageView。这样,如果视图小于496像素(图像的宽度),它将只能水平缩放。另外,请查看this question,我想这两个答案可能对您有所帮助。

0

如果我沿着正确的思路思考,你可以尝试通过

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

抢在从activity.java编程像素装置宽度,然后设置一个位图的完整图像,你倒是希望通过

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

通过使用

使用和调整该位图的宽度

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

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

然后,您可以

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

设置这个新的裁切图像您已在布局中设置的图像视图我希望这有助于!