服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现手机壁纸改变的方法

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

2021-04-01 15:35Ruthless Android

这篇文章主要介绍了Android实现手机壁纸改变的方法,以完整实例形式分析了Android手机壁纸改变的方法,包括页面布局及属性设置的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

main.xml布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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>

清单文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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自定义适配器:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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程序设计有所帮助。

延伸 · 阅读

精彩推荐
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7442021-03-11
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4642021-03-09
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4662021-01-03
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28