Android获取屏幕宽度

Android获取屏幕宽度,第1张

概述Android:嘿,我想检索屏幕宽度并将其除以3.我想在LayoutParams中使用此长度,但是它不起作用.我究竟做错了什么?该应用程序在//1时崩溃.publicclassLoaderImageViewextendsLinearLayout{privateContextmContext;privateDrawablemDrawable;privateProgressBarmSpi

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

解决方法:

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

总结

以上是内存溢出为你收集整理的Android获取屏幕宽度全部内容,希望文章能够帮你解决Android获取屏幕宽度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1085307.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存