布局文件如下:
XML文件如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<Button
android:id="@+id/btn_reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/returns" />
<ImageView
android:id="@+id/iv_current"
android:layout_width="80dp"
android:layout_height="130dp"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/action_settings" />
<Button
android:id="@+id/btn_current"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/getcurrent" />
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn_setwall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/setcurretn" />
</LinearLayout>
安卓手机壁纸的改变<1>要实现这一功能需要添加一个权限
<!-- 设置手机墙纸权限 -->
<uses-permission android:name="android.permission.SET_WALLPAPER" />
代码实现如下所示:
一、需要创建一个适配器
package com.brucecheng.waller;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
@SuppressWarnings("deprecation")
public class WallPaper extends BaseAdapter {
//创建资源数组
private int[] imageIds;
private Context context;
//重写构造方法
public WallPaper(Context context,int[] imageIds){
this.imageIds=imageIds;
this.context=context;
}
@Override
public int getCount() {
return imageIds.length;
}
@Override
public Object getItem(int position) {
return imageIds[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//创建一个ImageView用于显示壁纸图片
ImageView iv=new ImageView(context);
//设置参数
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setLayoutParams(new Gallery.LayoutParams(120, 120));
iv.setImageResource(imageIds[position]);
return iv;
}
二、Activity的代码实现如下所示:
package com.brucecheng.waller;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
@SuppressWarnings("deprecation")
public class WallerActivity extends Activity {
//创建资源数组
private int[] imageIds={R.drawable.bing11,
R.drawable.bing12,
R.drawable.bing5,
R.drawable.bing6,
R.drawable.bing7,
R.drawable.album,
};
//创建一个整型值表示当前的壁纸
int currentIndex=-1;
//寻找控件
Gallery gallery;
Button btn_reset,btn_current,btn_setwall;
ImageView iv_current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//调用方法实例化控件
findViewById();
//为gallery绑定适配器
gallery.setAdapter(new WallPaper(this, imageIds));
//设置监听事件
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//记录被选中的图片索引
currentIndex=position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//为其他的控件设置监听事件
btn_current.setOnClickListener(listener);
btn_setwall.setOnClickListener(listener);
btn_reset.setOnClickListener(listener);
}
//重写监听事件
OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
//获取当前壁纸
case R.id.btn_current:
iv_current.setBackgroundDrawable(getWallpaper());
break;
//设置壁纸
case R.id.btn_setwall:
InputStream in=WallerActivity.this.getResources().openRawResource(imageIds[currentIndex]);
//设为当前壁纸
try {
setWallpaper(in);
} catch (IOException e1) {
e1.printStackTrace();
}
break;
//恢复默认壁纸
case R.id.btn_reset:
try {
WallerActivity.this.clearWallpaper();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
};
//自定义方法实例化控件
public void findViewById(){
btn_current=(Button) findViewById(R.id.btn_current);
btn_reset=(Button) findViewById(R.id.btn_reset);
btn_setwall=(Button) findViewById(R.id.btn_setwall);
iv_current=(ImageView) findViewById(R.id.iv_current);
gallery=(Gallery) findViewById(R.id.gallery);
}
}