Android实现手机壁纸改变的方法

所属分类: 软件编程 / Android 阅读数: 1679
收藏 0 赞 0 分享

本文实例讲述了Android实现手机壁纸改变的方法。分享给大家供大家参考。具体如下:

main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <Button android:id="@+id/clearWall"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="恢复默认墙纸" />
  <ImageView android:id="@+id/currWall" 
    android:layout_width="100px"
    android:layout_height="150px"
    android:layout_gravity="center_horizontal" />
  <Button android:id="@+id/getWall" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="获取当前墙纸" />
  <Gallery android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
  <Button android:id="@+id/setWall" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="设置为当前墙纸" />
</LinearLayout>

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.ljq.activity"
   android:versionCode="1"
   android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".WallActivity"
         android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
  <uses-sdk android:minSdkVersion="7" />
  <!-- 设置手机墙纸权限 -->
  <uses-permission android:name="android.permission.SET_WALLPAPER" />
</manifest>

WallAdapter自定义适配器:

package com.ljq.activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class WallAdapter extends BaseAdapter {
  private int[] imgIds = null;
  private Context context = null;
  public WallAdapter(int[] imgIds, Context context) {
    super();
    this.imgIds = imgIds;
    this.context = context;
  }
  public int getCount() {
    return imgIds.length;
  }
  public Object getItem(int position) {
    //return imgIds[position];
    return imgIds[position%imgIds.length];//可循环
  }
  public long getItemId(int position) {
    return position;
  }
  public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(context);
    imageView.setBackgroundResource(imgIds[position]);// 设置ImageView的背景图片
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
    return imageView;
  }
}

WallActivity类:

package com.ljq.activity;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
public class WallActivity extends Activity {
  private int[] imgIds={R.drawable.w1, R.drawable.w2, R.drawable.w3, R.drawable.w4};
  private int selectIndex=-1;//被选中的图片在id数组中的索引
  private ImageView currWall=null;
  private Gallery gallery=null;
  private Button clearWall=null;
  private Button getWall=null;
  private Button setWall=null;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    gallery=(Gallery)findViewById(R.id.gallery);
    gallery.setAdapter(new WallAdapter(imgIds, WallActivity.this));
    gallery.setSpacing(5);
    gallery.setOnItemSelectedListener(new OnItemSelectedListener(){
      public void onItemSelected(AdapterView<?> parent, View view,
          int position, long id) {
        selectIndex = position;//记录被选中的图片索引
      }
      public void onNothingSelected(AdapterView<?> parent) {
      }
    });
    currWall=(ImageView)findViewById(R.id.currWall);
    clearWall=(Button)findViewById(R.id.clearWall);
    getWall=(Button)findViewById(R.id.getWall);
    setWall=(Button)findViewById(R.id.setWall);
    clearWall.setOnClickListener(listener);
    getWall.setOnClickListener(listener);
    setWall.setOnClickListener(listener);
  }
  View.OnClickListener listener=new View.OnClickListener(){
    public void onClick(View v) {
      Button btn=(Button)v;
      switch (btn.getId()) {
      case R.id.clearWall://还原手机壁纸
        try {
          WallActivity.this.clearWallpaper();
        } catch (IOException e) {
          e.printStackTrace();
        }
        break;
      case R.id.getWall://设置ImageView显示的内容为当前墙纸
        currWall.setBackgroundDrawable(getWallpaper());
        break;
      case R.id.setWall://设置墙纸
        InputStream in=WallActivity.this.getResources().openRawResource(imgIds[selectIndex]);
        try {
          setWallpaper(in);
        } catch (IOException e) {
          e.printStackTrace();
        }
        break;
      }
    }
  };
}

运行结果:

希望本文所述对大家的Android程序设计有所帮助。

更多精彩内容其他人还在看

老生常谈Android HapticFeedback(震动反馈)

下面小编就为大家带来一篇老生常谈Android HapticFeedback(震动反馈)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详谈OnTouchListener与OnGestureListener的区别

下面小编就为大家带来一篇详谈OnTouchListener与OnGestureListener的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿知乎悬浮功能按钮FloatingActionButton效果

前段时间在看属性动画,恰巧这个按钮的效果可以用属性动画实现,下面通过本文给大家分享adroid仿知乎悬浮功能按钮FloatingActionButton效果,需要的朋友参考下吧
收藏 0 赞 0 分享

解决Android V7后自定义Toolbar、ActionBar左侧有空白问题

这篇文章主要介绍的Android V7后自定义Toolbar、ActionBar左侧有空白问题的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android常见控件使用详解

这篇文章主要为大家详细介绍了Android常见控件的使用方法,包括ProgressBar进度条控件、AlertDialog对话框控件等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简洁的APP更新dialog数字进度条

这篇文章主要为大家详细介绍了Android实现简洁的APP更新dialog数字进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 判断当前语言环境是否是中文环境

本文主要介绍了Android 判断当前语言环境是否是中文环境的方法。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详谈Android中Matrix的set、pre、post的区别

下面小编就为大家带来一篇详谈Android中Matrix的set、pre、post的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现登录界面记住密码的存储

这篇文章主要为大家详细介绍了Android SharedPreferrences实现登录界面记住密码的存储,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 使用SharedPreferrences储存密码登录界面记住密码功能

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入,下面通过实例代码给大家讲解下,需要的朋友参考下吧
收藏 0 赞 0 分享
查看更多