将壁纸设置到每个HomeScreen

时间:2012-07-03 06:06:01

标签: android wallpaper homescreen

我是Android开发新手。我使用以下代码将壁纸设置为每个主屏幕。在此代码中,首先它将要求用户输入主屏幕可用性编号。

用户输入他们有3个主屏幕意味着它会要求他们从sdcard中选择三个壁纸,我得到三个图像并将其更改为设备默认屏幕尺寸并组合该位图图像并将其设置为主屏幕墙纸。

  public void onCreate(Bundle savedInstanceState) {
         Display display = getWindowManager().getDefaultDisplay();
    dwidth = display.getWidth();
    dheight = display.getHeight();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);          
    Log.i("WALLPAPER", "" + dwidth);
    Log.i("WALLPAPER", "" + dheight);
    width1 = dwidth;
    height1 = dheight;
    scno = (EditText) findViewById(R.id.screenno);
    image = (ImageView) findViewById(R.id.imageview1);
    wallpaper=(Button) findViewById(R.id.setwallpaper);
    selectimage = (Button) findViewById(R.id.selectimg);
    selectimage.setVisibility(View.VISIBLE);
    selectimage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(scno.getText().toString().length()<=0){
        Toast.makeText(getApplicationContext(),"Enter The number Of Screen",Toast.LENGTH_LONG).show();
            }
            else{
            nmscreen = scno.getText().toString();               
            noofscreen = Integer.parseInt(nmscreen);            
            Log.i("WALLPAPERDEMO", "" + noofscreen);                
            Intent intent1=new Intent();
            intent1.setType("image/*");
            intent1.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent1, "Complete action using"), PICK_FROM_FILE);             
            }
        }
    });

    wallpaper.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(scno.getText().toString().length()<=0){
                Toast.makeText(getApplicationContext(),"Enter The Number Of Screen",Toast.LENGTH_LONG).show();
            }
            else{
            WallpaperManager mywallpapermanager=WallpaperManager.getInstance(getApplicationContext());

            try{
                mywallpapermanager.setBitmap(change);
                selectimage.setEnabled(true);

            }catch (IOException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            image.setImageBitmap(null);
            scno.setText("");
        }
        }

    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK)return;
    Log.d("WALLPAPERDEMO", "Count: " + count);
    switch (requestCode) {
        case PICK_FROM_FILE:
            mImageCaptureUri = data.getData();
            Log.i("WALLPAPERDEMO","Calling doCrop() "+mImageCaptureUri.toString());
            doCrop();
            break;
        case CROP_FROM_CAMERA:          
            Bundle extras = data.getExtras();
            if (extras != null) {               
                photo = extras.getParcelable("data");               
                Log.d("WALLPAPERDEMO",""+photo.getWidth());                 
                Log.d("WALLPAPERDEMO",""+photo.getWidth());
                bitmapArray.add(photo);
                 count++;
                 Log.d("WALLPAPERDEMO","Count"+count);
                 if(count<noofscreen){
                        Log.d("WALLPAPERDEMO","Outside Switch"+count);
                        Intent intent1=new Intent();
                        intent1.setType("image/*");
                    intent1.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent1, "Complete action using"), PICK_FROM_FILE);
                 }else{
                     firstimage = new Bitmap[count];
                        for (i = 0; i <count; i++) {
                            firstimage[i] = bitmapArray.get(i);
                        }
                        setImage(firstimage);
                        selectimage.setEnabled(false);
                 }
                File f = new File(mImageCaptureUri.getPath());            
                if (f.exists()) f.delete();
                break;
        }           
    }               
}       

private void setImage(Bitmap[] firstimage) {

    change = Bitmap.createScaledBitmap(firstimage[0], width1, height1, true);
    for(int i=1;i<firstimage.length;i++){
            Log.d("WALLPAPERDEMO", "" + firstimage[i].getWidth());
            Log.d("WALLPAPERDEMO", "change " + change.getWidth());
            change1 = Bitmap.createScaledBitmap(firstimage[i], width1, height1, true);
            Log.d("WALLPAPERDEMO", "change1 " + change1.getWidth());
            change = combineImages(change, change1);
            Log.d("WALLPAPERDEMO", ""+change.getWidth());
    }

    image.setImageBitmap(change);

}

public Bitmap combineImages(Bitmap change1, Bitmap change) {
    Bitmap cs = null;
    int width, height = 0;

    if (change1.getWidth() > change.getWidth()) {
        width = change1.getWidth() + change.getWidth();
        height = change1.getHeight();
    } else {
        width = change.getWidth() + change1.getWidth();
        height = change.getHeight();
    }
    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas comboImage = new Canvas(cs);

    comboImage.drawBitmap(change, 0f, 0f, null);
    comboImage.drawBitmap(change1, change.getWidth(), 0f, null);
    return cs;

}

我正在运行此应用程序并使用真实设备检查它是否能完美地运行某些设备。但在某些设备中,屏幕尺寸并不合适。我的代码中有什么问题。我正在获取设备的默认屏幕尺寸,并且对于组合图像,我将选定的位图图像更改为默认屏幕尺寸,然后仅组合图像并将其设置为壁纸。

请帮我设置每个主屏幕的壁纸。提前谢谢。

1 个答案:

答案 0 :(得分:0)

xlarge屏幕至少为960dp x 720dp 大屏幕至少640dp x 480dp 正常屏幕至少为470dp x 320dp 小屏幕至少为426dp x 320dp

将资源保存在此文件夹中。