java - 如何将适合屏幕的图像设置为设备中的墙纸?
问题描述
我想将应用程序中查看的图像设置为墙纸。
虽然尝试这样做,但图像不适合我的设备屏幕,但使用 centerCrop 可以正确查看。
这是我的应用程序代码。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
app:srcCompat="@drawable/image" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@color/transparent"
android:text="Set Wallpaper"
android:textColor="#ffffff" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setWallpaper();
}
});
}
private void setWallpaper(){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
try {
manager.setBitmap(bitmap);
Toast.makeText(this, "Wallpaper Set", Toast.LENGTH_SHORT).show();
}
catch (IOException e){
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
}
这是应用程序中使用的图像
实时应用屏幕
在安卓设备中设置壁纸后
如何将图像完美设置为 XML 文件中使用的壁纸(缩放到 centerCrop),以便将其设置为任何 android 设备的完美贴合壁纸?
解决方案
利用:
android:background="@drawable/image"
代替:
app:srcCompat="@drawable/image"
推荐阅读
- ruby - 有没有办法使用 Selenium 和 Watir 确定 Chrome 浏览器中的元素是否可点击?
- c# - 安装net core后加载hostfxr.dll失败
- c# - 如何在子子文件夹 SharePoint Online C# 中创建子文件夹
- vba - 从 Excel VBA 在 word doc 中执行查找和替换
- html - html中z-index和堆叠元素的问题
- android-studio - 当字符串占用超过 1 行时,Android Studio 不会检查/突出显示 Kotlin Room DAO 查询
- swift - 自定义 UIView 中的无效上下文 0x0
- java - Android - 非 UI 线程呈现我的 UI 无响应
- sas - 参数 *pBookmark 对 CPage::Load 无效。[用于 Excel 的 SAS 插件]
- mysql - 存储过程不运行