android 设置壁纸页面,Android实现手机壁纸改变的方法

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

main.xml布局文件:

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="恢复默认墙纸" />

android:layout_width="100px"

android:layout_height="150px"

android:layout_gravity="center_horizontal" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="获取当前墙纸" />

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="设置为当前墙纸" />

清单文件:

package="com.ljq.activity"

android:versionCode="1"

android:versionName="1.0">

android:label="@string/app_name">

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;

}

}

};

}

运行结果:

1abb4d41f19406521e695baafe30f6a0.png

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设置动态壁纸,在Android开发中需要进行以下步骤: 1. 首先,在res文件夹下创建一个新的文件夹,命名为xml,用于存放动态壁纸的配置文件。 2. 在xml文件夹内,创建一个名为wallpaper.xml的配置文件。在该文件中,定义动态壁纸的属性,例如壁纸资源、壁纸的位置等。 3. 在res文件夹下创建一个新的文件夹,命名为drawable,并在其中添加壁纸资源的图片文件。 4. 在AndroidManifest.xml文件中声明壁纸服务。在<application>标签内添加以下代码: ``` java <service android:name=".WallpaperService" android:enabled="true" android:permission="android.permission.BIND_WALLPAPER" > <intent-filter> <action android:name="android.service.wallpaper.WallpaperService" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <meta-data android:name="android.service.wallpaper" android:resource="@xml/wallpaper" /> </service> ``` 5. 创建一个名为WallpaperService的Java类,继承自WallpaperService。在该类中,重写onCreateEngine()方法,并返回一个继承自Engine的内部类对象。在该内部类中,实现动态壁纸的逻辑,在onCreate()方法中初始化壁纸资源,在onSurfaceCreated()方法中绘制壁纸,在onSurfaceDestroyed()方法中释放资源。 6. 编译、运行项目,并选择该动态壁纸作为手机的壁纸。 以上是设置动态壁纸的基本步骤,当然还可以通过对壁纸的绘制、交互等进行更复杂的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值