3

Android:嘿,我想检索屏幕的宽度并将其除以 3。我想在 LayoutParams 中使用这个长度,但它不起作用。我究竟做错了什么 ?应用程序在 // 1 崩溃。

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");
}

}
4

4 回答 4

2

在创建视图时,它尚未附加到布局或窗口。尝试将该代码移至onAttachedToWindow.

于 2012-05-15T19:31:41.643 回答
2

尽管getWindowManager()是Activity的方法: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
于 2012-05-15T20:20:58.720 回答
0

我相信 getWindowManager() 需要在 Activity 上调用,而不是在 Layout 类上。尝试在您的活动中检索宽度/高度,然后将这些值(宽度/高度)传递到您的扩展线性布局类并使用它们。

于 2012-05-15T19:33:20.507 回答
0

如果您不在 Activity 中,您可以通过 WINDOW_SERVICE 获得默认显示:

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

详情在这里..

于 2014-06-12T11:30:03.637 回答