2017-04-20 59 views
0

我正在製作一個功能類似於Google's new wallpaper app.的壁紙應用程序。底部有一個水平滾動視圖,其中包含一個圖像按鈕列表,點擊時會在應用程序中顯示牆紙預覽。如何告訴應用程序設置正在預覽的壁紙?如何根據所選圖像設置牆紙?

我的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> 

我的Java:

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(); 

    } 
} 
} 

感謝您的任何和提前的所有幫助!

回答

0

首先,您需要設置所需的權限

<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(); 
    } 
}); 
+0

這只是將它設置不同的牆紙,當第一個預覽。 – tycemang