我有多个图像,如果我使用WallpaperManager
,那么它会将壁纸设置为将壁纸设置为。我如何使用toPhone和WallpaperManager?
Java文件(如果需要):
package hd.wallpaper.abs;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Main extends Activity implements View.OnClickListener {
ImageView display;
int toPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hd__wallpapers);
toPhone = R.drawable.eye;
display = (ImageView) findViewById(R.id.IVdisplay);
ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
ImageView image6 = (ImageView) findViewById(R.id.IVimage7);
Button setWall = (Button) findViewById(R.id.setwallpaperbutton);
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
image6.setOnClickListener(this);
setWall.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.IVimage1:
display.setImageResource(R.drawable.i1);
toPhone = R.drawable.i1;
break;
case R.id.IVimage2:
display.setImageResource(R.drawable.i2);
toPhone = R.drawable.i2;
break;
case R.id.IVimage3:
display.setImageResource(R.drawable.i3);
toPhone =R.drawable.i3;
break;
case R.id.IVimage4:
display.setImageResource(R.drawable.i3);
toPhone =R.drawable.i3;
break;
case R.id.IVimage5:
display.setImageResource(R.drawable.i3);
toPhone =R.drawable.i3;
break;
case R.id.IVimage7:
display.setImageResource(R.drawable.i3);
toPhone =R.drawable.solar;
break;
case R.id.setwallpaperbutton:
final WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
final int fullWidth = myWallpaperManager.getDesiredMinimumWidth();
final int fullHeight = myWallpaperManager.getDesiredMinimumHeight();
try {
myWallpaperManager.setResource(R.drawable.i1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
答案 0 :(得分:0)
case R.id.BsetWallpaper:
InputStream first = getResources().openRawResource(toPhone);
Bitmap close= BitmapFactory.decodeStream(first);
try{
getApplicationContext().setWallpaper(close);
}catch (IOException e){
e.printStackTrace();
}
希望能帮到你!
答案 1 :(得分:0)
代码
final WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
final int fullWidth = myWallpaperManager.getDesiredMinimumWidth();
final int fullHeight = myWallpaperManager.getDesiredMinimumHeight();
try {
myWallpaperManager.setResource(R.drawable.i1);
每次将 R.drawable.i1 设置为壁纸时,只需使用 toPhone 进行更改,它就可以解决您的问题。