簡體   English   中英

如何在設備中將圖像適合屏幕設置為牆紙?

[英]How to set Image fit to screen as wallpaper in devices?

我想將在應用程序中查看的圖像設置為牆紙。

嘗試這樣做時,圖像不適合我設備的屏幕,但可以使用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();
        }
    }

}

這是應用程序中使用的圖像

該應用程序使用的圖片

實時應用程序屏幕

運行應用程序屏幕實時查看

在Android設備中設置牆紙后

設置牆紙

如何將圖像完美地設置為XML文件中使用的牆紙(縮放至centerCrop)以將其設置為適用於所有android設備的牆紙?

采用:

android:background="@drawable/image"

代替:

app:srcCompat="@drawable/image" 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM