gpt4 book ai didi

android - 显示指标未将墙纸缩放到屏幕尺寸

转载 作者:行者123 更新时间:2023-11-29 00:18:26 25 4
gpt4 key购买 nike

我已经查看了其他每篇关于将墙纸适合设备屏幕尺寸的帖子,并且我已经尝试了他们的每一种方法,但它仍然无法在一些设备上将墙纸设置为正确的屏幕尺寸设备,我想知道是否有人可以提供帮助。

这是墙纸应用程序中的图片...

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com