随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

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;
}
}

};

}
复制代码

运行结果

posted on   Ruthless  阅读(1586)  评论(1编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
< 2011年2月 >
30 31 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 1 2 3 4 5
6 7 8 9 10 11 12

点击右上角即可分享
微信分享提示