2012-05-15 197 views
3

Android: 嘿,我想要回顧屏幕的寬度,並將其除以3.我想在LayoutParams中使用此長度,但它不起作用。我究竟做錯了什麼 ?該應用在// //崩潰。Android獲取屏幕寬度

public class LoaderImageView extends LinearLayout { 

private Context mContext; 
private Drawable mDrawable; 
private ProgressBar mSpinner; 
private ImageView mImage; 
Context ctx; 
Display display; 

public LoaderImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context); 
} 

public LoaderImageView(Context context) { 
    super(context); 
    init(context); 
} 

/** 
* First time loading of the LoaderImageView 
* Sets up the LayoutParams of the view, you can change these to 
* get the required effects you want 
*/ 
private void init(final Context context) { 
    mContext = context; 

// 1。 //這是應用程序崩潰,從這裏

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

//到這裏

mImage = new ImageView(mContext); 
    mImage.setLayoutParams(new LayoutParams((width/3), 150)); 
    mImage.setVisibility(View.GONE); 
    mImage.setBackgroundColor(Color.WHITE); 
    mImage.setPadding(3, 3, 3, 3); 



    mSpinner = new ProgressBar(mContext); 
    mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    mSpinner.setIndeterminate(true); 

    addView(mSpinner); 
    addView(mImage); 


} 

/** 
* Set's the view's drawable, this uses the internet to retrieve the image 
* don't forget to add the correct permissions to your manifest 
* 
* @param imageUrl the url of the image you wish to load 
*/ 
public void setImageDrawable(final String imageUrl) { 
    mDrawable = null; 
    mSpinner.setVisibility(View.VISIBLE); 
    mImage.setVisibility(View.GONE); 
    new Thread() { 
     public void run() { 
      try { 
       mDrawable = getDrawableFromUrl(imageUrl); 
       imageLoadedHandler.sendEmptyMessage(RESULT_OK); 
      } catch (MalformedURLException e) { 
       imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED); 
      } catch (IOException e) { 
       imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED); 
      } 
     }; 
    }.start(); 
} 

/** 
* Callback that is received once the image has been downloaded 
*/ 
private final Handler imageLoadedHandler = new Handler(new Callback() { 

    public boolean handleMessage(Message msg) { 
     switch (msg.what) { 
     case RESULT_OK: 
      mImage.setImageDrawable(mDrawable); 
      mImage.setVisibility(View.VISIBLE); 
      mSpinner.setVisibility(View.GONE); 
      break; 
     case RESULT_CANCELED: 
     default: 
      // Could change image here to a 'failed' image 
      // otherwise will just keep on spinning 
      break; 
     } 
     return true; 
    } 
}); 

/** 
* Pass in an image url to get a drawable object 
* 
* @return a drawable object 
* @throws IOException 
* @throws MalformedURLException 
*/ 
private static Drawable getDrawableFromUrl(final String url) throws IOException, MalformedURLException { 
    return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()), "name"); 
} 

} 
+0

是否有不使用XML標記此任務一個很好的理由? – snowCrabs

回答

2

在視圖中創建了點,但尚未連接到佈局或窗口。嘗試將該代碼移至onAttachedToWindow

+0

很高興知道。你每天學習新的東西。 – Gophermofur

0

我相信getWindowManager()需要在Activity上調用,而不是Layout類。嘗試檢索活動中的寬度/高度,然後將這些值(寬度/高度)傳遞到擴展的線性佈局類中並使用它們。

2

儘管getWindowManager()是活動的方法: http://developer.android.com/reference/android/app/Activity.html#getWindowManager()

而且getSize()方法僅適用起始API livel 13

http://developer.android.com/reference/android/view/Display.html#getSize(android.graphics.Point

也許這是不好的,覆蓋所有的Android API版本使用

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); // deprecated 
int height = display.getHeight(); // deprecated 
0

如果y ou're不是在一個活動,您可以通過WINDOW_SERVICE得到默認顯示:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
Display display = wm.getDefaultDisplay(); 

details here..