Android实现手机壁纸改变的方法
作者:Ruthless 发布时间:2022-06-03 07:41:46
标签: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程序设计有所帮助。


猜你喜欢
- 本文实例讲述了Android使用ActionBar和ViewPager切换页面,分享给大家供大家参考。具体如下:运行效果截图如下:项目布局如
- 本文实例为大家分享了Android实现界面跳转的具体代码,供大家参考,具体内容如下布局<?xml version="1.0&
- 本文实例讲述了Java实现的简单网页截屏功能。分享给大家供大家参考,具体如下:package awtDemo;import java.awt
- 本文实例讲述了C#使用GDI绘制直线的方法。分享给大家供大家参考。具体实现方法如下:Point p1=new Point(200,200);
- 一、项目简述功能:登录,门诊划价,收费,报表,药品管理等等功能。二、项目运行运行环境: Jdk1.8 + Tomcats . 5 + mys
- 项目需求:根据年级下拉框的变化使得科目下拉框绑定次年级下对应有的值我们用三层架构的模式来实现1.我们想和数据库交互,我们首先得来先解决DAL
- 本文实例讲述了Android中显示GIF动画的实现代码。分享给大家供大家参考,具体如下:gif图动画在android中还是比较常用的,比如像
- Activity设置全屏和无标题栏,要用到andorid.view.Window和Android.view.WindowManager。 W
- 一、类加载流程类加载的流程可以简单分为三步:加载连接初始化而其中的连接又可以细分为三步:验证准备解析下面会分别对各个流程进行介绍。1.1 类
- 前言在这个 Spring Security 教程中,我很乐意与您分享如何通过在 Java Web 应用程序中为用户添加角色来实现授权&
- Escape: public static string Escape(string str) { StringBuilder sb = n
- 在 WinForms 中,有时要执行耗时的操作,在该操作未完成之前操作用户界面,会导致用户界面停止响应。解决的方法就是新开一个线程,把耗时的
- Spring JPA 增加字段执行异常用Spring jpa Entity里面增加了几个字段,但启动报错,提示column Unable t
- 什么是Java类库在编写程序的时候,通常有很多功能是通用的,或者是很基础的,可以用这些功能来组成更发杂的功能代码。比如文件操作,不同程序对文
- 引言设计: 嗯? 这个图片点击跳转进详情再返回图片怎么变白闪一下呢?产品: 是啊是啊! 一定是个bug开发: 囧囧囧在开发过程中, 也许你也
- 效果展示如下所示:实时监控redis环境信息和日志列表Redis配置在windows下安装的redis,在安装目录找到redis.windo
- /**Bitmap放大的方法*/ private static Bitmap big(Bitmap bitmap) { Matrix mat
- 使用linq把多列的List转化为只有指定列的List1、方式一2、方式二
- 本文实例讲述了C#实现异步GET的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Coll
- 最近要做一个java web项目,因为页面不是很多,所以就没有前后端分离,前后端写在一起,这时候就用到thymeleaf了,以下是不动脑式的