umbrellall1 发表于 2013-2-4 19:31:07

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

效果:

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



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



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

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


main.xml


<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><Galleryandroid: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的点击事件*/@Overridepublic void onItemClick(AdapterView<?> parent, View v, final int position,long id) {/**设置 消息框*/new AlertDialog.Builder(WallpaperTest.this)/** 设置消息框的标题*/.setTitle(R.string.alert_title)/** 设置标题框的图片*/.setIcon(myImage.myImageIds)/** 设置消息框体的文本*/.setMessage(R.string.alert_message)/** 设置点击确定按钮的事件*/.setPositiveButton(R.string.alert_ok, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Resources resources=getBaseContext().getResources();io=resources.openRawResource(myImage.myImageIds);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() {@Overridepublic 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();}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn myImageIds.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView i =new ImageView(context);/** 设置背景图片给ImageView*/i.setImageResource(myImageIds);/**图片的高和宽*/i.setScaleType(ImageView.ScaleType.FIT_XY);/**设置Gallery的背景图*/i.setBackgroundResource(mGalleryBackground);return i;}}
页: [1]
查看完整版本: 第三章: 设置手机背景图片