Как установить обои на основе выбранного изображения?

Я делаю приложение для обоев, аналогичное по функциональности Новое приложение для обоев от Google. Внизу есть горизонтальная прокрутка со списком кнопок изображения, при нажатии на которые будет отображаться предварительный просмотр обоев в приложении. Как указать приложению установить обои из просматриваемого?

Мой xml:

<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:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
tools:context="biz.bigtooth.wallpapers.MainActivity">

<Button
    android:id="@+id/set"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:padding="16dp"
    style="@style/Base.Widget.AppCompat.Button.Borderless" 
    android:background="@color/button"
    android:gravity="start|center_vertical"
    android:textColor="@android:color/white"
    android:text="Set Wallpaper" />

<HorizontalScrollView
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/bottom">

        <ImageButton android:id="@+id/dsc18"
            android:layout_width="120dp"
            android:layout_height="150dp"
            android:layout_margin="8dp"
            android:focusable="true"
            android:src="@drawable/_dsc0018" />

        <ImageButton android:id="@+id/dsc65"
            android:layout_width="120dp"
            android:layout_height="150dp"
            android:layout_margin="8dp"
            android:focusable="true"
            android:src="@drawable/_dsc0065" />

        <ImageButton android:id="@+id/dsc131"
            android:layout_width="120dp"
            android:layout_height="150dp"
            android:layout_margin="8dp"
            android:focusable="true"
            android:src="@drawable/_dsc0131" />

        <ImageButton android:id="@+id/dsc175"
            android:layout_width="120dp"
            android:layout_height="150dp"
            android:layout_margin="8dp"
            android:focusable="true"
            android:src="@drawable/_dsc0175" />

        <ImageButton android:id="@+id/dsc246"
            android:layout_width="120dp"
            android:layout_height="150dp"
            android:layout_margin="8dp"
            android:focusable="true"
            android:src="@drawable/_dsc0246" />

        <ImageButton android:id="@+id/dsc274"
            android:layout_width="120dp"
            android:layout_height="150dp"
            android:layout_margin="8dp"
            android:focusable="true"
            android:src="@drawable/_dsc0274" />

    </LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

Моя Ява:

import android.app.WallpaperManager;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.FocusFinder;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

RelativeLayout layout;

HorizontalScrollView view;

Button set;

ImageButton dsc18;
ImageButton dsc65;
ImageButton dsc131;
ImageButton dsc175;
ImageButton dsc246;
ImageButton dsc274;

int oneeight = R.drawable._dsc0018pr;
int sixfive = R.drawable._dsc0065pr;
int onethreeone = R.drawable._dsc0131pr;
int onesevenfive = R.drawable._dsc0175pr;
int twofoursix = R.drawable._dsc0246pr;
int twosevenfour = R.drawable._dsc0274pr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layout = (RelativeLayout)findViewById(R.id.layout_main);
    layout.setBackgroundResource(oneeight);
    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.setVisibility(view.isShown()
                    ? View.GONE
                    : View.VISIBLE );
        }
    });

    view = (HorizontalScrollView)findViewById(R.id.view);

    set = (Button)findViewById(R.id.set);
    set.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setWallpaper(onesevenfive);
        }
    });

    dsc18 = (ImageButton)findViewById(R.id.dsc18);
    dsc65 = (ImageButton)findViewById(R.id.dsc65);
    dsc131 = (ImageButton)findViewById(R.id.dsc131);
    dsc175 = (ImageButton)findViewById(R.id.dsc175);
    dsc246 = (ImageButton)findViewById(R.id.dsc246);
    dsc274 = (ImageButton)findViewById(R.id.dsc274);

    dsc18.hasFocus();

    dsc18.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layout.setBackgroundResource(oneeight);
            dsc18.hasFocus();
        }
    });

    dsc65.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layout.setBackgroundResource(sixfive);
            dsc65.hasFocus();
        }
    });

    dsc131.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layout.setBackgroundResource(onethreeone);
            dsc131.hasFocus();
        }
    });

    dsc175.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layout.setBackgroundResource(onesevenfive);
            dsc175.hasFocus();
        }
    });

    dsc246.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layout.setBackgroundResource(twofoursix);
            dsc246.hasFocus();
        }
    });

    dsc274.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layout.setBackgroundResource(twosevenfour);
            dsc274.hasFocus();
        }
    });
}

public void setWallpaper(int id) {
    int wall = getBackground;

    WallpaperManager myWallpaperManager
            = WallpaperManager.getInstance(getApplicationContext());
    try {
        myWallpaperManager.setResource(wall);
    } catch (IOException e) {
        e.printStackTrace();

    }
}
}

Заранее спасибо за любую помощь!


person tycemang    schedule 20.04.2017    source источник


Ответы (1)


Во-первых, вам нужно установить необходимое разрешение

<uses-permission android:name="android.permission.SET_WALLPAPER"/>

а потом :

dsc18.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(onethreeone);
            } catch (IOException e) {
                e.printStackTrace();
            }
        dsc18.hasFocus();
    }
});
person Arash Hatami    schedule 20.04.2017
comment
Это просто настраивает его на установку других обоев при предварительном просмотре первого. - person tycemang; 21.04.2017