Settings imageview to phone wallpaper
guys working with Android Gallery and just got it working, however I'm trying to figure out how to get the selected image in the gallery to be set as the phone's wallpaper by the click of a button. Any help is appreciated. Here's the code;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.gallery);
imgView = (ImageView) findViewById(R.id.ImageView01);
imgView.setBackgroundResource(Imgid[0]);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
imgView.setBackgroundResour开发者_如何学运维ce(Imgid[position]);
}
});
}
Button btn = (Button) findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(
R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
}
Use the wallpaper manager:
try {
// Set background from a resource
WallpaperManager.getInstance(this).setResource(R.drawable.bg);
// or set background from a bitmap
WallpaperManager.getInstance(this).setBitmap(mBitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Will need this permission in your manifest:
<uses-permission android:name="android.permission.SET_WALLPAPER" />
精彩评论