【问题标题】:How to set Image fit to screen as wallpaper in devices?如何将适合屏幕的图像设置为设备中的墙纸?
【发布时间】:2018-05-20 11:00:49
【问题描述】:

我想将在应用中查看的图像设置为墙纸。

虽然尝试这样做,但图像不适合我设备的屏幕,但使用 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 设备的完美壁纸?

【问题讨论】:

  • 设置匹配父项
  • 尝试使用但没有结果

标签: java android screen android-wallpaper


【解决方案1】:

用途:

android:background="@drawable/image"

代替:

app:srcCompat="@drawable/image" 

【讨论】:

  • 我想将该图像设置为我的智能手机背景(壁纸)
猜你喜欢
  • 2014-05-10
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多