android2.3 Launcher墙纸设置代码追踪

    case MENU_WALLPAPER_SETTINGS:              
         startWallpaper();//点击壁纸设置菜单,会调用startWallpaper()  

  
    private void startWallpaper() {
        closeAllApps(true);
        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
        Intent chooser = Intent.createChooser(pickWallpaper,
        getText(R.string.chooser_wallpaper));
        startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
    }  //createChooser 会弹出一个对话框,把能处理Intent.ACTION_SET_WALLPAPER   
       //的activity都列出来供用户选择

选择“壁纸”后会启动Activity:WallpaperChooser

界面由三个View组成:ImageView 、Gallery、Button,

点击Button后,设置壁纸:

    private void selectWallpaper(int position) {
        if (mIsWallpaperSet) {
            return;
        }

        mIsWallpaperSet = true;
        try {
            WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
            wpm.setResource(mImages.get(position));
            setResult(RESULT_OK);
            finish();
        } catch (IOException e) {
            Log.e(TAG, "Failed to set wallpaper: " + e);
        }
    }


Gallery的item被选择后,ImageView会把相对应的大的图片显示出来,

此处通过异步处理类asynctask来完成的:

    public void onItemSelected(AdapterView parent, View v, int position, long id) {
        if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
            mLoader.cancel();
        }
        mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
    }

其中execute的参数position就是AsyncTask的doInBackground后面的参数,

doInBackground  return的值,会作为onPostExecute的参数

   

 class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
        BitmapFactory.Options mOptions;

        WallpaperLoader() {
            mOptions = new BitmapFactory.Options();
            mOptions.inDither = false;
            mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;            
        }
        
        protected Bitmap doInBackground(Integer... params)//此处params为position
 {
            if (isCancelled()) return null;
            try {
                return BitmapFactory.decodeResource(getResources(),
                        mImages.get(params[0]), mOptions);
            } catch (OutOfMemoryError e) {
                Log.w(TAG, "Home no memory load current wallpaper", e);
                return null;
            }
        }

        @Override
        protected void onPostExecute(Bitmap b) //此处的b为doInBackGround return的值
  {
            if (b == null) return;

            if (!isCancelled() && !mOptions.mCancel) {
                // Help the GC
                if (mBitmap != null) {
                    mBitmap.recycle();
                }
    
                final ImageView view = mImageView;
                view.setImageBitmap(b);
    
                mBitmap = b;
    
                final Drawable drawable = view.getDrawable();
                drawable.setFilterBitmap(true);
          //用来对Bitmap进行滤波处理,有抗锯齿的效果 
                drawable.setDither(true);//防抖动
                view.postInvalidate();

                mLoader = null;
            } else {
               b.recycle(); 
            }
        }

        void cancel() {
            mOptions.requestCancelDecode();
            super.cancel(true);
        }
    }
}




 


 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章