2015-10-20 213 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)

設置這個新的裁切圖像您已在佈局中設置的圖像視圖我希望這有助於!