android - 显示指标未将壁纸缩放至屏幕尺寸

标签 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;
    }

}

好的,谢谢。希望有人能帮忙。

最佳答案

使用默认的图像裁剪器来设置它。效果很好。

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

关于android - 显示指标未将壁纸缩放至屏幕尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24772905/

相关文章:

android - 如何在 Galaxy Tab 上以编程方式安装 apk 文件

android - 如何在服务中使用 strings.xml 文件?

android - 系统壁纸应该通过服务改变

android - 启动默认 Android 壁纸选择器

windows - 是否可以以编程方式更改用户的屏幕保护程序和/或桌面背景?

android壁纸图像路径

java - 尝试将 String 转换为 LocalDate 时出现 "... is not public in org.bp.threeten.format.DateTimeFormatter"错误

python - REST/JSON/XML-RPC/SOAP

android - 从邮件附件打开 x509 证书 (.crt) 时接收 Intent

android - 在 Android 上设置壁纸刷新