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

效果:

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



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



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

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


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




你可能感兴趣的:(C++,c,xml,android,C#)