英文:
set image wallpaper to fit screen "
问题
item_home_image = findViewById(R.id.item_home_image);
item_home_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermission()) {
final Intent intent = getIntent();
String url = intent.getStringExtra("imageUrl");
Picasso.with(PicassoDisplayImageAdapter.this).load(url).into(new Target() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(PicassoDisplayImageAdapter.this);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException ex) {
ex.printStackTrace();
}
Toasty.normal(PicassoDisplayImageAdapter.this, "Background changed successfully", Toast.LENGTH_SHORT).show();
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Toasty.error(PicassoDisplayImageAdapter.this, "Image loading failed", Toast.LENGTH_SHORT).show();
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Toasty.normal(PicassoDisplayImageAdapter.this, "Loading...", Toast.LENGTH_SHORT).show();
}
});
}
}
});
英文:
I want to set the image to fit all screen when the user makes it as background for his device, I tried many codes but it does not work.
Here is my code
item_home_image = findViewById(R.id.item_home_image);
item_home_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermission()) {
final Intent intent = getIntent();
String url = intent.getStringExtra("imageUrl");
Picasso.with(PicassoDisplayImageAdapter.this).load(url).into(new Target() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(PicassoDisplayImageAdapter.this);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException ex) {
ex.printStackTrace();
}
Toasty.normal(PicassoDisplayImageAdapter.this, "تم تغيير الخلفية بنجاح", Toast.LENGTH_SHORT).show();
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Toasty.error(PicassoDisplayImageAdapter.this, "فشل تحميل الصورة", Toast.LENGTH_SHORT).show();
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Toasty.normal(PicassoDisplayImageAdapter.this, "جاري التحميل", Toast.LENGTH_SHORT).show();
}
});
}
}
});
答案1
得分: 1
试试这个:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
try {
wallpaperManager.setBitmap(newBitmap);
} catch (IOException e) {
e.printStackTrace();
}
英文:
Try this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
try {
wallpaperManager.setBitmap(newBitmap);
} catch (IOException e) {
e.printStackTrace();
}
</details>
# 答案2
**得分**: 1
```java
// 以下为翻译好的内容:
// 使用`wallpaperManager`设置的位图可能与**显示大小**不同,因此在传递给`wallpaperManager`之前需要对该位图进行缩放。
// 尝试以下操作:
item_home_image = findViewById(R.id.item_home_image);
item_home_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermission()) {
final Intent intent = getIntent();
String url = intent.getStringExtra("imageUrl");
Picasso.with(PicassoDisplayImageAdapter.this).load(url).into(new Target() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(PicassoDisplayImageAdapter.this);
try {
int height = getResources().getDisplayMetrics().heightPixels;
int width = getResources().getDisplayMetrics().widthPixels;
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
wallpaperManager.setBitmap(bitmap);
} catch (IOException ex) {
ex.printStackTrace();
}
Toasty.normal(PicassoDisplayImageAdapter.this, "تم تغيير الخلفية بنجاح", Toast.LENGTH_SHORT).show();
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Toasty.error(PicassoDisplayImageAdapter.this, "فشل تحميل الصورة", Toast.LENGTH_SHORT).show();
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Toasty.normal(PicassoDisplayImageAdapter.this, "جاري التحميل", Toast.LENGTH_SHORT).show();
}
});
}
}
});
英文:
The bitmap that are you setting with wallpaperManager
may not same as the Display size so, you need to scale that bitmap before passing to wallpaperManager
Try With
item_home_image = findViewById(R.id.item_home_image);
item_home_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermission()) {
final Intent intent = getIntent();
String url = intent.getStringExtra("imageUrl");
Picasso.with(PicassoDisplayImageAdapter.this).load(url).into(new Target() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(PicassoDisplayImageAdapter.this);
try {
int height = getResources().getDisplayMetrics().heightPixels;
int width = getResources().getDisplayMetrics().widthPixels;
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
wallpaperManager.setBitmap(bitmap);
} catch (IOException ex) {
ex.printStackTrace();
}
Toasty.normal(PicassoDisplayImageAdapter.this, "تم تغيير الخلفية بنجاح", Toast.LENGTH_SHORT).show();
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Toasty.error(PicassoDisplayImageAdapter.this, "فشل تحميل الصورة", Toast.LENGTH_SHORT).show();
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Toasty.normal(PicassoDisplayImageAdapter.this, "جاري التحميل", Toast.LENGTH_SHORT).show();
}
});
}
}
});
答案3
得分: 0
你可以使用这个 ImageView 属性
android:scaleType="fitXY"
更多 链接
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论