我看过其他有关将墙纸适合设备屏幕尺寸的文章,并且我尝试了每种方法,但在某些设备上仍然无法将墙纸设置为设备的正确屏幕尺寸,我想知道是否有人可以帮忙。
这是壁纸应用程序中的图片...
这是在将其设置为墙纸之后。
这是我的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"));