2014-01-22 100 views
0

我需要您的幫助。設備旋轉後可更改的Android設備大小

在我的活動我有一個TextView在那裏我加載HTML內容:

textview.setText(Html.fromHtml(body, p, null)); 

因爲有可能是在這個內容一些圖片,我使用

URLImageParser p = new URLImageParser(textview, this); 

來接他們並動態添加。問題是,如果圖像小於屏幕寬度,它會自動對齊左側。所以我想通居中圖像,我會創造更廣泛的位圖,附上我的圖像中的特定位置,根據顯示的寬度和圖像:

public Drawable fetchDrawable(String urlString) 
{ 
    try 
    { 
     InputStream is = fetch(urlString); 
     Drawable drawable; 
     Bitmap bitmap = BitmapFactory.decodeStream(is); 

     widthBM=bitmap.getWidth(); 
     heightBM=bitmap.getHeight(); 
     display.getSize(size); 
     int width = size.x; 

     int w = (width - widthBM)/2+widthBM; 
     Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
     Bitmap b = Bitmap.createBitmap(w, heightBM, conf); 
     Canvas comboCanvas = new Canvas(b); 
     comboCanvas.drawBitmap(bitmap, (width - widthBM)/2, 0, null); 

     drawable = new BitmapDrawable(getResources(), b); 
     drawable.setBounds(0, 0, 0+w, 0+heightBM); 

     return drawable; 
    } 
    catch (Exception e) 
    { 
     return null; 
    } 
} 

而且在活動開始它的作品完美 - 這開始時是在縱向:

portrait

,這是在景觀啓動時:

landscape

問題始於設備的旋轉。我想,既然我沒有使用像

android:configChanges="keyboardHidden|orientation|screenSize" 
本次活動

任何東西,我不重寫onConfigurationChanged(配置NEWCONFIG),整個活動應該被重新加載,應該的onCreate再次打來電話,我可繪製的大小應根據新的屏幕寬度重新計算。但是,從縱向到橫向切換的時候,而不是得到什麼對第二張照片,我得到這樣的:

enter image description here

當我檢查繪製它是正確的大小(假設295x80px在肖像和455x80px的風景,其中屏幕是480x800px(或800x480px當在橫向,顯然)和圖像120x80px)。我錯過了什麼?

public class URLImageParser implements ImageGetter 
{ 
    Context c; 
    TextView container; 

    public URLImageParser(TextView t, Context c) 
    { 
     this.c = c; 
     this.container = t; 
    } 

    public Drawable getDrawable(String source) 
    { 
     URLDrawable urlDrawable = new URLDrawable(); 
     ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable); 
     asyncTask.execute(source); 
     return urlDrawable; 
    } 

    public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> 
    { 
     URLDrawable urlDrawable; 
     Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 

     public ImageGetterAsyncTask(URLDrawable d) 
     { 
      this.urlDrawable = d; 
     } 

     @Override 
     protected Drawable doInBackground(String... params) 
     { 
      String source = params[0]; 
      return fetchDrawable(source); 
     } 

     @Override 
     protected void onPostExecute(Drawable result) 
     { 

      urlDrawable.setBounds(0, 0, 0+(width - widthBM)/2 + widthBM, 0+heightBM); 

      urlDrawable.drawable = result; 
      URLImageParser.this.container.invalidate(); 

      URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() + heightBM)); 

      URLImageParser.this.container.setEllipsize(null); 

     } 
    } 
} 
+0

比較'widthBM'和'heightBM'(如果我理解正確,它們是「座標」,告訴你放置圖像)。如果這些不相同,則可能需要在另一個事件中獲取座標。讓我知道,我曾經做過類似的事情。 但是,我認爲爲每個方向提供不同的佈局會容易得多。 – RominaV

+0

感謝您的回答。 WidthBM和heightBM是位圖的寬度和高度。此位圖左上角的座標是(width - widthBM)/ 2,0(位於內部的位圖b),它們在設備旋轉後重新計算並且是正確的。你的佈局是什麼意思?在這種情況下,在這個活動使用的佈局中只有一個TextView。 – Adam

+0

也許你想以編程的方式來做,所以我說的不適合你。否則,您可以爲縱向模式指定佈局,爲橫向指定另一個佈局:http://developer.android.com/training/basics/supporting-devices/screens.html。那麼,你說這兩種情況下的變量是相同的?如果是這樣,我會盡力實施它。 – RominaV

回答

1

固定它。原來它與drawables無關。正確的答案是:不要成爲一個白癡。如果由於某種原因,你決定像我一樣,不要把

android:freezesText="true" 

在你的textview屬性。