2013-10-07 111 views
1

好了,所以我問這個問題之前,我已經看了:Android的 - 底欄重疊SurfaceView

How to remove unwanted overlapping in android

Tabs at bottom overlapping with the list view

Android bottom navigation bar overlapping Spinner. Set Spinner dropdown height/margin

Bottom button bar overlaps the last element of Listview!

但是我還沒有看到我認爲可以解決我的特殊情況。 所以這裏是我的問題:我正在寫一個自定義的SurfaceView,它將在屏幕上顯示圖像。在該SurfaceView中,我正在將圖像繪製到右下角。下面是代碼:

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.Display; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.WindowManager; 

public class demosf extends Activity { 

    OurView v; 
    int Measuredwidth; 
    int Measuredheight; 
    WindowManager w; 
    Bitmap whatever; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Measuredwidth = 0; 
     Measuredheight = 0; 
     getScreenWidthAndHeight(); 
     whatever = BitmapFactory.decodeResource(getResources(), R.raw.dragarrow); 
     v = new OurView(this); 
     setContentView(v); 
    } 

    public class OurView extends SurfaceView implements Runnable { 

     Thread t = null; 
     SurfaceHolder holder; 
     boolean isItOK; 
     public OurView(Context context) { 
      super(context); 
      holder = getHolder(); 
     } 

     @Override 
     public void run() { 
      while (isItOK) { 
       try { 
        Thread.sleep((long) 50); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       if (!holder.getSurface().isValid()) { 
        continue; 
       } 
       Canvas c = holder.lockCanvas(); 
       c.drawARGB(255, 0, 0, 0); 
       c.drawBitmap(whatever, ((float) Measuredwidth - (float) whatever.getWidth()), ((float) Measuredheight - (float) whatever.getHeight()), null); 
       holder.unlockCanvasAndPost(c); 
      } 
     } 
     public void pause() { 
      isItOK = false; 
      while (true) { 
       try { 
        t.join(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       break; 
      } 
      t = null; 
     } 

     public void resume() { 
      isItOK = true; 
      t = new Thread(this); 
      t.start(); 
     } 
    } 
    @Override 
    protected void onPause() { 
     super.onPause(); 
     v.pause(); 
    } 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     v.resume(); 
    } 

    @SuppressLint("NewApi") 
    @SuppressWarnings("deprecation") 
    public void getScreenWidthAndHeight() { 
     Point size = new Point(); 
     w = getWindowManager(); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 
      w.getDefaultDisplay().getSize(size); 

      Measuredwidth = size.x; 
      Measuredheight = size.y; 
     } else { 
      Display d = w.getDefaultDisplay(); 
      Measuredwidth = d.getWidth(); 
      Measuredheight = d.getHeight(); 

     } 
    } 
} 

,只是要徹底的,這裏的清單太:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.something" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.something.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name="com.example.something.demosf" 
      android:label="@string/app_name" 
      android:screenOrientation="landscape" 
      android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
      > 
      <intent-filter> 
       <action android:name="com.example.something.DEMO" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

    </application> 

</manifest> 

現在的問題我似乎有,是對我的Nexus 7這正是作品出來的我期望。然而,在我的朋友東芝Thrive上,圖像的底部被底部的條切斷。以下是比較: problem Nexus7在右側,Thrive在左側。所以我的問題是:究竟發生了什麼,我該如何解決這個問題? (最好適用於所有Android版本) 哦,側面問題:底部酒吧甚至叫什麼?大聲笑

編輯:我知道這不是最好的代碼,我只是使用此代碼來演示發生了什麼事情,也許找到了一種不同的方法來解決這個奇怪的重疊在我的「getScreenWidthAndHeight()」方法設備它發生在

+0

這就是所謂的系統欄,僅供參考。 –

+0

很高興知道,非常感謝=)任何想法如何解釋呢?我在http:// stackoverflow找到了一個「getStatusBarHeight()」方法。com/questions/3407256/android-height-of-status-bar-in-android但是由於我的Nexus 7似乎沒有考慮到這個問題,但我仍然希望有人能夠有某種解決方案 – codingNewb

+0

狀態欄與系統欄不同;系統欄由大多數設備上的觸摸按鈕組成(通常位於底部)。狀態欄是包含您的通知,電池壽命等的(通常在頂部)欄。 –

回答

1

相反的:

c.drawBitmap(whatever, ((float) Measuredwidth - (float) whatever.getWidth()), 
      ((float) Measuredheight - (float) whatever.getHeight()), null); 

使用:

c.drawBitmap(whatever, ((float) this.getWidth() - (float) whatever.getWidth()), 
      ((float) this.getHeight() - (float) whatever.getHeight()), null); 

原因:

您定位位圖的方法假定底部不存在任何屏幕裝飾。實際上,它根本不考慮裝飾視圖。您應該使用活動視圖的維度來處理定位。

編輯:

在您的活動的情況下,下面將讓你的觀點的維度:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Measuredwidth = 0; 
    Measuredheight = 0; 
    getScreenWidthAndHeight(); 
    whatever = BitmapFactory.decodeResource(getResources(), R.raw.dragarrow); 
    v = new OurView(this); 

    v.post(new Runnable() { 

     @Override 
     public void run() { 
      Measuredwidth = v.getWidth(); 
      Measuredheight = v.getHeight();  
     } 
    }); 

    setContentView(v); 
} 

編輯2:

LinearLayout llMain; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Measuredwidth = 0; 
    Measuredheight = 0; 
    //getScreenWidthAndHeight(); 
    whatever = BitmapFactory.decodeResource(getResources(), R.raw.dragarrow); 

    llMain = new LinearLayout(this); 

    setContentView(llMain); 

    llMain.post(new Runnable() { 

     @Override 
     public void run() { 

      Measuredwidth = llMain.getWidth(); 
      Measuredheight = llMain.getHeight();  

      scaleAndTrimImages(); 

      v = new OurView(demosf.this); 

      llMain.addView(v);     
     } 
    }); 

} 

public void scaleAndTrimImages() { 

    // Use Measuredwidth and Measuredheight 
    // Since you are calling this method from onCreate(Bundle), 
    // it runs only once. 

} 
+1

Ooo看起來很有希望,我會試試看^ _^ – codingNewb

+0

那麼我怎麼會在方法本身中獲取大小呢?實際上,我需要在onCreate方法中發現這些度量值,與我的示例中使用getScreenWidthAndHeight()方法的同一點。你說使用活動視圖,它被設置爲「v」,但是當我使用v.getWidth()來設置我得到的寬度0 – codingNewb

+0

@codingNewb謝謝。定位子視圖的一種可靠方法是使用父視圖的邊界矩形。創建一個'Rect'變量:'Rect r = new Rect();'。獲取父視圖的邊界:'this.getGlobalVisibleRect(r);'。現在,使用:'c.drawBitmap(whatever,((float)r.right - (float)whatever.getWidth()),((float)r.bottom - (float)whatever.getHeight()),null); '。祝你好運! – Vikram