一个关于设置桌面背景的实例

 来自《Android应用开发揭秘》一书:

Code:
  1. package com.android.mySetWallpaper;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.   
  6. import android.app.Activity;  
  7. import android.app.AlertDialog;  
  8. import android.content.Context;  
  9. import android.content.DialogInterface;  
  10. import android.content.res.Resources;  
  11. import android.content.res.TypedArray;  
  12. import android.os.Bundle;  
  13. import android.view.View;  
  14. import android.view.ViewGroup;  
  15. import android.widget.AdapterView;  
  16. import android.widget.BaseAdapter;  
  17. import android.widget.Gallery;  
  18. import android.widget.ImageView;  
  19. import android.widget.Toast;  
  20. /* 
  21.  * 修改桌面背景的实例,主要运用了setWallpaper();方法。 
  22.  */  
  23. public class HelloSetWallpaper extends Activity {  
  24.     private ImageAdapter myImageAdapter;  
  25.     private static InputStream is;  
  26.   
  27.     /** Called when the activity is first created. */  
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.   
  33.         final Integer[] myImageIds = { R.drawable.photo1, R.drawable.photo2,  
  34.                 R.drawable.photo3, R.drawable.photo4, R.drawable.photo5,  
  35.                 R.drawable.my, R.drawable.you };  
  36.         myImageAdapter = new ImageAdapter(HelloSetWallpaper.this, myImageIds);  
  37.         Gallery g = (Gallery) findViewById(R.id.myGallery);  
  38.         g.setAdapter(myImageAdapter);  
  39.         g.setOnItemClickListener(new Gallery.OnItemClickListener() {  
  40.             public void onItemClick(AdapterView<?> parent, View v,  
  41.                     final int position, long id) {  
  42.                 new AlertDialog.Builder(HelloSetWallpaper.this).setTitle(  
  43.                         " ").setIcon(myImageIds[position])//感觉在这里添加Title不合适,但如果你取消setTitle的话,后面的setIcon也不会起作用  
  44.                         .setMessage(R.string.app_about).setPositiveButton(  
  45.                                 R.string.str_ok,  
  46.                                 new DialogInterface.OnClickListener() {  
  47.                                     public void onClick(  
  48.                                             DialogInterface dialoginterface,  
  49.                                             int i) {  
  50.                                         Resources resources = getBaseContext()  
  51.                                                 .getResources();  
  52.                                         is = resources  
  53.                                                 .openRawResource(myImageAdapter.myImageIds[position]);  
  54.                                         try {  
  55.                                             setWallpaper(is);  
  56.                                             Toast.makeText(  
  57.                                                     HelloSetWallpaper.this,  
  58.                                                     "桌面已经更换",  
  59.                                                     Toast.LENGTH_SHORT).show();  
  60.                                         } catch (Exception e) {  
  61.                                             e.printStackTrace();  
  62.                                         }  
  63.                                     }  
  64.                                 }).setNegativeButton("取消",  
  65.                                 new DialogInterface.OnClickListener() {  
  66.                                     public void onClick(  
  67.                                             DialogInterface dialoginterface,  
  68.                                             int i) {  
  69.                                         Toast.makeText(HelloSetWallpaper.this,  
  70.                                                 "已经取消了", Toast.LENGTH_SHORT)  
  71.                                                 .show();  
  72.                                     }  
  73.                                 }).show();  
  74.             }  
  75.         });  
  76.     }  
  77.   
  78.     public class ImageAdapter extends BaseAdapter {  
  79.         private Context mContext;  
  80.         private Integer[] myImageIds;  
  81.         int myGalleryItemBackground;  
  82.   
  83.         public ImageAdapter(Context c, Integer[] id) {  
  84.             mContext = c;  
  85.             myImageIds = id;  
  86.             TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery);  
  87.             myGalleryItemBackground = a.getResourceId(  
  88.                     R.styleable.Gallery_android_galleryItemBackground, 0);  
  89.             a.recycle();  
  90.         }  
  91.   
  92.         @Override  
  93.         public int getCount() {  
  94.             // TODO Auto-generated method stub  
  95.             return myImageIds.length;  
  96.         }  
  97.   
  98.         @Override  
  99.         public Object getItem(int position) {  
  100.             // TODO Auto-generated method stub  
  101.             return position;  
  102.         }  
  103.   
  104.         @Override  
  105.         public long getItemId(int position) {  
  106.             // TODO Auto-generated method stub  
  107.             return position;  
  108.         }  
  109.   
  110.         @Override  
  111.         public View getView(int position, View convertView, ViewGroup parent) {  
  112.             // TODO Auto-generated method stub  
  113.             ImageView i = new ImageView(mContext);  
  114.             i.setImageResource(myImageIds[position]);  
  115.             i.setScaleType(ImageView.ScaleType.FIT_XY);  
  116.             i.setLayoutParams(new Gallery.LayoutParams(138108));  
  117.             i.setBackgroundResource(myGalleryItemBackground);  
  118.             return i;  
  119.         }  
  120.   
  121.     }  
  122.   
  123.     public void setWallpaper(InputStream data) throws IOException {  
  124.         super.setWallpaper(data);  
  125.     }  
  126. }  

attrs.xml内容

Code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="Gallery">  
  4.         <attr name="android:galleryItemBackground" />  
  5.     </declare-styleable>  
  6. </resources>  

剩下的main.xml和value下的string.xml很简单,自己设置。

 

  1. TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery);  
  2.             myGalleryItemBackground = a.getResourceId(  
  3.                     R.styleable.Gallery_android_galleryItemBackground, 0);  
  4.             a.recycle();  
  5. //关于这块代码,一直没有理解!

 

这个是在2.1模拟器下的截图:

这个是在1.5下的截图:

我想这个应该和分辨率有关系吧!如果你想将背景设为系统默认的,1、可以点击Menu去设置2、自己写一段代码,调用clearWallpaper();方法,也可以还原桌面。这个都在书中讲到的!

 

你可能感兴趣的:(c,android,Integer,resources,menu,encoding)