Создание приложения для обоев в формате Kotlin. Не удается подобрать обои под экран пользователя - PullRequest
0 голосов
/ 28 мая 2020

Я делаю приложение для обоев. Я беру изображения из своей базы данных firebase. Но когда я нажимаю кнопку «Установить в качестве обоев» в своем приложении, обои оставляют пустое черное пространство как вверху, так и внизу. Кажется, не удается заставить его работать даже после того, как попробовали разные похожие ответы на stackoverflow.

Моя активность по установке обоев:

import android.app.WallpaperManager
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Point
import android.graphics.Rect
import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.drawToBitmap
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_set_wallpaper.*
import java.io.IOException


class SetWallpaperActivity : AppCompatActivity() {
        var image: String?=null
    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_set_wallpaper)

        val intent=intent
        image=intent.getStringExtra("image")
        Picasso.get().load(intent.getStringExtra("image")).into(image_set_wallpaper)
        val wm = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = wm.defaultDisplay
        val size = Point()
        display.getSize(size)

        val height: Int = size.y
        val width: Int = size.x

        set_wallpaper.setOnClickListener {
            val result:Bitmap=rl_iv.drawToBitmap()

            val wallpaperManager = WallpaperManager.getInstance(this)

            try {
                wallpaperManager.setBitmap(result)
                }
            catch (ex: IOException) {
                ex.printStackTrace()

            }

        }
        set_lock_wallpaper.setOnClickListener {
            val wpManager = WallpaperManager.getInstance(this)
            val myBitmap: Bitmap = rl_iv.drawToBitmap()
            try {
                wpManager.setBitmap(myBitmap, null, true, WallpaperManager.FLAG_LOCK)
            }
            catch (ex: IOException) {
                ex.printStackTrace()

            }
        }

    }
}

Его 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=".SetWallpaperActivity">

    <RelativeLayout
        android:id="@+id/rl_iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image_set_wallpaper"
        android:layout_width="match_parent"
        android:scaleType="fitXY"
        android:layout_height="match_parent"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"/>

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal">

    <Button
        android:id="@+id/set_wallpaper"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="SET as HOME SCREEN WALLPAPER"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:background="@drawable/shape_button_rounded"
        android:focusableInTouchMode="true"
        android:gravity="center"/>

    <Button
        android:id="@+id/set_lock_wallpaper"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="SET as LOCK SCREEN WALLPAPER"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/set_wallpaper"
        android:background="@drawable/shape_button_rounded"
        android:focusableInTouchMode="true"
        android:layout_marginBottom="20dp"
        android:gravity="center"/>

    </LinearLayout>

</RelativeLayout>

Пожалуйста, предложите, что мне следует отредактировать в моем коде, чтобы он заработал. Я новичок в этом. Пожалуйста, используйте Kotlin, чтобы объяснить

1 Ответ

1 голос
/ 28 мая 2020

Вы получаете черное пространство из-за полей в файле XML.

 <ImageView
        android:id="@+id/image_set_wallpaper"
        android:layout_width="match_parent"
        android:scaleType="fitXY"
        android:layout_height="match_parent"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"/>

Удалите layout_margin-xx- в атрибуте ImageView, чтобы получить желаемый результат.

Ваш ImageView в файле XML должен быть таким:

 <ImageView
        android:id="@+id/image_set_wallpaper"
        android:layout_width="match_parent"
        android:scaleType="fitXY"
        android:layout_height="match_parent"/>

Это должно решить вашу проблему.

...