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

本文实例讲述了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程序设计有所帮助。

(0)

相关推荐

  • Android编程之动态壁纸实例分析

    本文实例讲述了Android编程之动态壁纸.分享给大家供大家参考,具体如下: 从android 2.1版本起引入了动态壁纸的概念,熟悉android的人一定不会陌生.这里解释一个动态壁纸是怎么形成又是怎么工作的. 首先动态桌面的动态体现出这个组件是实时变化的,也就是说有一个后台在不停的刷新这个组件.联想到后台组件首先想到的就是service,从代码角度看,果然如此.每一个动态桌面都继承自WallpaperService,其中必须实现的抽象方法onCreateEngine,返回一个Engine对象

  • Android 背景图片的缩放实现

    Android 背景图片的缩放 ONE Goal ,ONE Passion ! 我们看到一些效果,控件中的背景图片会慢慢变大,但是控件不会随着图片的放大而变大.效果如下: 分析: 想让图片变大,而且控件本身大小不能改变,那么就要改变图片自身大小,而不能改变控件大小. 实现原理: 1,首先拿到我们要放大的图片bitmap. 2,使用Bitmap.createBitmap().创建一个bitmap的副本. 3,使用matrix去改变图片副本本身大小 4,使用ValueAnimator去根据变化率将副

  • android 自定义ScrollView实现背景图片伸缩的实现代码及思路

       用过多米音乐的都市知道, 这个UI可以上下滑动,作用嘛---无聊中可以划划解解闷,这被锤子公司老罗称谓为"情怀",其实叫"情味"更合适.嘿嘿.如今挪动互联网开展这么迅速,市场上已不再是那早期随便敲个APP放上架就能具有几十万用户的阶段了.近来苹果公司,为了怕android下载量赶超苹果商店,大势宣称:(第 500 亿个下载应用的用户就能够获得 10,000 美元的 iTunes 礼品卡,除此之外,紧随第 500 亿以后的前 50 名用户也可以获得 500 美元

  • Android编程之书架效果背景图处理方法

    本文实例讲述了Android编程之书架效果背景图处理方法.分享给大家供大家参考,具体如下: 在android应用中,做一个小说阅读器是很多人的想法,大家一般都是把如何读取大文件,如果在滚动或是翻页时,让用户感觉不到做为重点.我也在做一个类似一功能,可是在做书架的时候,看了QQ阅读的书架,感觉很好看,就想做一个,前面一篇<android书架效果实现原理与代码>对此做了专门介绍,其完整实例代码可点击此处本站下载. 上面的例子很不错,可是有一个问题是他的背景图的宽必须是手机屏幕的宽,不会改变,这样感

  • Android编程之手机壁纸WallPaper设置方法示例

    本文实例讲述了Android编程之手机壁纸WallPaper设置方法.分享给大家供大家参考,具体如下: /** * Andorid设置手机屏幕的壁纸 * * @description: * @author ldm * @date 2016-5-4 下午3:08:56 */ public class SetWallpaperActivity extends Activity { // WallpaperManager类:系统壁纸管理.通过它可以获得当前壁纸以及设置指定图片作为系统壁纸. priva

  • android中实现背景图片颜色渐变方法

    常用,记录一下. 效果图: 首先新建xml文件  bg_gradient.xml 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?>  <shape xmlns:android="http://schemas.android.com/apk/res/android" >        <gradient          android:startColor="

  • Android自定义Button并设置不同背景图片的方法

    本文实例讲述了Android自定义Button并设置不同背景图片的方法.分享给大家供大家参考,具体如下: 1.自定义MyButton类 public class MyButton extends Button { //This constructormust be public MyButton(Context context, AttributeSet attrs) { super(context, attrs); } public MyButton(Context context) { su

  • android动态壁纸调用的简单实例

    调用后动态壁纸其实是显示在Activity的后面,而Activity则是透明显示,这样就可以看到下面的动态壁纸,如果Activity不是透明的则什么也看不到. 代码中有用到两个接口 IWallpaperService mService; IWallpaperEngine mEngine; 我们可以看到该目录下面有三个aidl接口,分别是 复制代码 代码如下: interface IWallpaperConnection { void attachEngine(IWallpaperEngine e

  • Android设置桌面背景图片的实现方法

    1.设置桌面背景图片的方法 复制代码 代码如下: Resources res=getResources(); BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.icon); Bitmap bmp=bmpDraw.getBitmap(); try{ setWallpaper(bmp); }catch(IOException e) { e.printStackTrace(); } 2.在manifest中增加设置桌面的

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

    本文实例讲述了Android实现手机壁纸改变的方法.分享给大家供大家参考.具体如下: main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" androi

  • Android实现手机振动设置的方法

    本文实例讲述了Android实现手机振动设置的方法.分享给大家供大家参考.具体如下: main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" androi

  • Android调用手机拍照功能的方法

    本文实例讲述了Android调用手机拍照功能的方法.分享给大家供大家参考.具体如下: 一.main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" andr

  • Android获取手机联系人列表的方法

    本文实例为大家分享了Android获取手机联系人列表的具体代码,供大家参考,具体内容如下 下面直接贴代码 1.先写一个实体类,来放名字和号码 public class PhoneDto { private String name; //联系人姓名 private String telPhone; //电话号码 public String getName() { return name; } public void setName(String name) { this.name = name;

  • 详解Android 获取手机中微信聊天记录方法

    首先我们要知道,微信的聊天记录一般是不提供给我们获取的,所以一般情况下我们手机没root的话就拿不到了.就算是root后的手机,想要获取微信的EnMicroMsg.db文件并且解密它.打开它也有点难度. 下面我们就来演示怎么从安卓设备的手机中拿到微信的数据文件吧~ 实验软件 :Android Studio实验设备:Root过的真机一部一.拿到数据库文件EnMicroMsg.db 一步步来,打开Android Studio的File Explorer:Tools –> Android –> An

  • Android获取手机通话记录的方法

    Android如何获取手机通话记录,本文为大家揭晓. 获取手机通话记录流程: 1. 获取ContentResolver; ContentResolver resolver = getContentResolver(); 2.resolver.query(*); 需要传入通话记录的URI:CallLog.Calls.CONTENT_URI 3.对查询得到的Cursor进行数据获取. 主要代码如下: MainActivity.java package com.noonecode.contentres

  • Android开发手机无线调试的方法

    是不是还在为了手机usb被占用而不能链接编译器而难过?是不是感觉无线调试遥不可及? 读完下面的几步 让你轻松掌握无线调试. 1. 首先将你的手机连接到无线网 2. 将你的手机链接到电脑上 3. Window 配置好adb Linux 安装好adb 4. 确认手机链接到无线网络需要和你的电脑在同一个无线网络内 5. 在命令端输入 $ adb tcpip 5555 (5555为端口号,可以自由指定) 然后在输如下命令 $ adb tcpip 此时你可以查看到 自己手机的ip地址 大概如下所示 10.

  • android获取手机唯一标识的方法

    复制代码 代码如下: import android.provider.Settings.Secure; private String android_id = Secure.getString(getContext().getContentResolver(),Secure.ANDROID_ID); UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hash

  • Android改变手机屏幕朝向的方法

    本文实例讲述了Android改变手机屏幕朝向的方法.分享给大家供大家参考.具体如下: 模拟当点击按钮时,使手机朝向发生改变. main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="v

随机推荐