2011-02-23 246 views
3

即時通訊新的Android和即時通訊面臨一些問題...Android - SurfaceView(寬度,高度)問題

即時通訊使用SurfaceView擴展我的課。 在我的班級,我不能得到寬度和高度的SurfaceView。 它始終爲0。我試着輕彈整個屏幕(surfaceView)上的圖像,但即時通訊無法獲得它的寬度和高度。

有什麼建議嗎?

下面是main.xml中的SurfaceView代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <test.MyClass android:id="@+id/SurfaceView01" 
     android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    </test.MyClass> 
</LinearLayout> 

回答

4

我從上面你也需要它來獲取高度和寬度試過類似的事情,並張貼在這個線程Android: UI elements over canvas layer

除了

public void onDraw(Canvas canvas) 
{ 
    this.canvas = canvas; 

    if (!oneTime) 
    { 
     oneTime = true; 
     screenWidth = getWidth(); 
     screenHeight = getHeight(); 
     Bitmap red = BitmapFactory.decodeResource(getResources(), R.drawable.image); 
     red = Bitmap.createScaledBitmap(red, screenWidth , screenHeight , true); 
    } 
} 

如果你不想拉OnDraw的代碼,你可以試試下面的代碼

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
int screenHeight = metrics.heightPixels; 
int screenWidth = metrics.widthPixels; 

但是這會給你整個可見光區域

+0

這就是升技爲時已晚。在進入onDraw方法之前創建一個圖像。我需要創建寬度和高度的圖像。從那以後,我的東西呈現在圖像的圖形和onDraw有我只是做canvas.drawBitmap(image.getBitmap(),0,0,NULL); – no9 2011-02-23 08:25:31

+0

你爲什麼不能創建的onDraw – ingsaurabh 2011-02-23 08:28:54

+0

圖像必須從J2ME的OnPaint一個習慣中......它的不一樣呢? :) – no9 2011-02-23 08:30:07

3

我找到了解決這個工作對我來說的寬度高度是使用的getHeight()和的getWidth()我SurafaceCreated方法裏面,之後我就開始我的線程。這允許我在任何更新或繪圖發生之前具有表面視圖的高度和寬度。

2

使用surfaceView.getHolder().getSurfaceFrame()得到Rect物體大小