第三章: 设置手机背景图片

该应用程序使用Gallery组件展示一组图片,并允许用户选择图片作为壁纸。通过点击事件弹出对话框确认是否设置壁纸,确认后更改设备壁纸并给出提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果:

[img]http://dl.iteye.com/upload/attachment/403688/205bab8b-e3ad-3cc8-8e71-0a2f9dc18623.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/403691/2d716091-4ee1-3702-bc27-c13a70253358.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/403695/4aa9fd09-df8b-3f16-9124-92c3662ab476.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/403700/b2e111bd-d8ba-31a1-9a77-0c5f7112c3af.jpg[/img]


main.xml



<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<Gallery
android:id="@+id/gall"
android:layout_width="320px"
android:layout_height="429px"
android:layout_x="0px"
android:layout_y="2px"
>
</Gallery>
</AbsoluteLayout>





AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wallpaper.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".WallpaperTest"
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-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>

</manifest>





package wallpaper.test;

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

public class WallpaperTest extends Activity {
protected static InputStream io;
private ImageAdapter myImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** 载入main.xml文件 */
setContentView(R.layout.main);
/** 设置一组图片*/
Integer[] myImageIde={
R.drawable.pic01,
R.drawable.pic02,
R.drawable.pic03,
R.drawable.pic04,
R.drawable.pic05
};

myImage=new ImageAdapter(WallpaperTest.this,myImageIde);
/** 设置图为Gallery的显示方式*/
Gallery g=(Gallery)findViewById(R.id.gall);
g.setAdapter((SpinnerAdapter) myImage);
g.setOnItemClickListener(new Gallery.OnItemClickListener(){
/** 设置ITEM的点击事件*/
@Override
public void onItemClick(AdapterView<?> parent, View v, final int position,
long id) {
/**设置 消息框*/
new AlertDialog.Builder(WallpaperTest.this)
/** 设置消息框的标题*/
.setTitle(R.string.alert_title)
/** 设置标题框的图片*/
.setIcon(myImage.myImageIds[position])
/** 设置消息框体的文本*/
.setMessage(R.string.alert_message)
/** 设置点击确定按钮的事件*/
.setPositiveButton(R.string.alert_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

Resources resources=getBaseContext().getResources();
io=resources.openRawResource(myImage.myImageIds[position]);
try {
/** 更换桌面*/
setWallpaper(io);
/** 设置Toast的消息*/
Toast.makeText(WallpaperTest.this,getString(R.string.alert_isgllery), Toast.LENGTH_LONG).show();

} catch (Exception e) {
e.printStackTrace();
}
}
/** 设置点击取消按钮的事件*/
}).setNegativeButton(R.string.alert_cancle, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/** 设置Toast的消息*/
Toast.makeText(WallpaperTest.this, getString(R.string.alert_no), Toast.LENGTH_LONG).show();
}
}).show();
}
});
}
@Override
public void setWallpaper(InputStream data) throws IOException {
super.setWallpaper(data);
}
}






package wallpaper.test;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter{
int mGalleryBackground;
private Context context;
protected Integer[] myImageIds;
public ImageAdapter(Context c,Integer[] aid){
context=c;
myImageIds=aid;
/** 设置背景图片*/
TypedArray a=c.obtainStyledAttributes(R.styleable.Gallery);
mGalleryBackground=a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
a.recycle();
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return myImageIds.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i =new ImageView(context);
/** 设置背景图片给ImageView*/
i.setImageResource(myImageIds[position]);
/**图片的高和宽*/
i.setScaleType(ImageView.ScaleType.FIT_XY);
/**设置Gallery的背景图*/
i.setBackgroundResource(mGalleryBackground);
return i;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值