android開發設定案頭背景圖適應解析度

來源:互聯網
上載者:User

android 設定案頭背景圖片適應螢幕大小

今天設定案頭每天自動更新背景,但是案頭的背景都是被系統裁剪過的圖片,後來找了這個過時的方法,改進後果然有用了!

              Bitmap bmp=BitmapFactory.decodeResource
              (getResources(), bg[DailyBg]);
              try
              {
              //  super.setWallpaper(bmp);
                  WallpaperManager instance = WallpaperManager.getInstance(ChangeBgImage.this);
//                  int desiredMinimumWidth = getWindowManager().getDefaultDisplay().getHeight(); //方法已經過時
//                  int desiredMinimumHeight = getWindowManager().getDefaultDisplay().getHeight();//方法已經過時
                  DisplayMetrics dm = new DisplayMetrics();
                  getWindowManager().getDefaultDisplay().getMetrics(dm);
                  int desiredMinimumWidth = dm.widthPixels;
                  int desiredMinimumHeight = dm.heightPixels;
                  Log.v("ss",""+desiredMinimumWidth);
                  Log.v("ss",""+desiredMinimumHeight);
                  instance.suggestDesiredDimensions(desiredMinimumWidth, desiredMinimumHeight);
                  instance.setBitmap(bmp);
                  Toast.makeText(ChangeBgImage.this,"壁紙設定成功",Toast.LENGTH_SHORT).show();
              }
              catch (IOException e)
              {
                e.printStackTrace();
              }
            }

  就是在擷取螢幕大小的地方老方法已經被注釋了,用了新的方法擷取了螢幕的大小。

對了還要在資訊清單檔裡將許可權加上,這點很重要,我找了半天才發現,suggestDesiredDimensions方法需要加許可權

    <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>

  好了 ,菜鳥大功告成


Android設定案頭背景圖片的方法


1.設定案頭背景圖片的方法


    Resources res=getResources();     
    BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.icon);      
    Bitmap bmp=bmpDraw.getBitmap();   
    try{  
        setWallpaper(bmp);  
    }catch(IOException e) {  
        e.printStackTrace();  
    }  

 

2.在manifest中增加設定案頭的許可權

[xhtml] view plaincopy

    <uses-permission android:name="android.permission.SET_WALLPAPER" />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.