安卓中改变手机壁纸

布局文件如下:
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);
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BruceCheng夏夏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值