将图像壁纸设置为适应屏幕”

huangapple go评论102阅读模式
英文:

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(&quot;imageUrl&quot;);
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, &quot;تم تغيير الخلفية بنجاح&quot;, Toast.LENGTH_SHORT).show();
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Toasty.error(PicassoDisplayImageAdapter.this, &quot;فشل تحميل الصورة&quot;, Toast.LENGTH_SHORT).show();
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Toasty.normal(PicassoDisplayImageAdapter.this, &quot;جاري التحميل&quot;, Toast.LENGTH_SHORT).show();
}
});
}
}
});

答案3

得分: 0

你可以使用这个 ImageView 属性

android:scaleType="fitXY"

更多 链接

英文:

You can use this ImageView attributes

  android:scaleType=&quot;fitXY&quot;

more link

huangapple
  • 本文由 发表于 2020年5月30日 11:39:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/62097505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定