我正在创建一个与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 :(得分: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();
}
});