显示指标未将壁纸缩放到屏幕尺寸

时间:2014-07-16 05:35:37

标签: android wallpaper

我已经查看了关于在设备屏幕尺寸上安装壁纸的其他帖子,我已经尝试了他们的每一种方法但它仍然在一些设备上失败,将壁纸设置为正确的屏幕大小设备,我想知道是否有人可以提供帮助。

这是壁纸应用中的图片......

Picture in app

这是在它被设置为壁纸之后......

after set as wallaper

这是我的java类

private static final String LOG_TAG = "Home";

private static final Integer[] THUMB_IDS = {
        R.drawable.icarus_thumb,
        R.drawable.koneko_thumb,
        R.drawable.ic_launcher,
};

private static final Integer[] IMAGE_IDS = {
        R.drawable.icarus,
        R.drawable.koneko,
        R.drawable.ic_launcher,
};

private Gallery mGallery;
private boolean mIsWallpaperSet;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.wallpaper);

    mGallery = (Gallery) findViewById(R.id.gallery);
    mGallery.setAdapter(new ImageAdapter(this));
    mGallery.setOnItemSelectedListener(this);
    mGallery.setOnItemClickListener(this);
}

@Override
protected void onResume() {
    super.onResume();
    mIsWallpaperSet = false;
}

public void onItemSelected(AdapterView parent, View v, int position, long id) {
    getWindow().setBackgroundDrawableResource(IMAGE_IDS[position]);
}

public void onItemClick(AdapterView parent, View v, int position, long id) {

    Bitmap bitmap = BitmapFactory.decodeStream(getResources().openRawResource(IMAGE_IDS[position]));

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int height = metrics.heightPixels;
    int width = metrics.widthPixels;


    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
     try {

         wallpaperManager.setBitmap(bitmap);

        wallpaperManager.suggestDesiredDimensions(width, height); 




     } catch (IOException e) {
      e.printStackTrace();
     }  
}

/*
 * When using touch if you tap an image it triggers both the onItemClick and
 * the onTouchEvent causing the wallpaper to be set twice. Synchronize this
 * method and ensure we only set the wallpaper once.
 */
private synchronized void selectWallpaper(int position) {
    if (mIsWallpaperSet) {
        return;
    }
    mIsWallpaperSet = true;
    try {
        Bitmap bmap2 = BitmapFactory.decodeStream(getResources().openRawResource(IMAGE_IDS[position]));
        DisplayMetrics metrics = new DisplayMetrics(); 
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int height = metrics.heightPixels; 
        int width = metrics.widthPixels;
        Bitmap bitmap = Bitmap.createScaledBitmap(bmap2, width, height, true); 






        WallpaperManager wallpaperManager = WallpaperManager.getInstance(Wallpaper.this);

        wallpaperManager.setBitmap(bitmap);
        setResult(RESULT_OK);
        finish();
    } catch (IOException e) {
        Log.e(LOG_TAG, "Failed to set wallpaper " + e);
    }
}

public void onNothingSelected(AdapterView parent) {
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    selectWallpaper(mGallery.getSelectedItemPosition());
    return true;
}

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return THUMB_IDS.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageResource(THUMB_IDS[position]);
        i.setAdjustViewBounds(true);
        i.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        i.setBackgroundResource(android.R.drawable.picture_frame);
        return i;
    }

}

非常感谢。希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:2)

使用默认图像裁剪器进行设置。它工作正常。

Intent setAsWallpaperIntent = new Intent(Intent.ACTION_ATTACH_DATA);
setAsWallpaperIntent.setDataAndType(uriToImage, "image/*");
startActivity(Intent.createChooser(setAsWallpaperIntent, "Set Image As"));
相关问题