EX07_07.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:5k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex07_07;
  2. import java.io.InputStream;
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.res.Resources;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.view.Window;
  12. import android.view.animation.AnimationUtils;
  13. import android.widget.AdapterView;
  14. import android.widget.BaseAdapter;
  15. import android.widget.Gallery;
  16. import android.widget.ImageSwitcher;
  17. import android.widget.ImageView;
  18. import android.widget.Toast;
  19. import android.widget.ViewSwitcher;
  20. import android.widget.Gallery.LayoutParams;
  21. public class EX07_07 extends Activity implements 
  22.   AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory
  23. {
  24.   protected static InputStream is;
  25.   private ImageSwitcher mSwitcher;
  26.   private Context mContext;
  27.   
  28.   /*设置在Gallery的图档*/
  29.   private Integer[] mThumbIds =
  30.   {
  31.       R.drawable.sample_p1, R.drawable.sample_p2,
  32.       R.drawable.sample_p3, R.drawable.sample_p4,
  33.       R.drawable.sample_p5, R.drawable.sample_p6,
  34.       R.drawable.sample_p7, R.drawable.sample_p8
  35.   };
  36.   /*设置在Switcher的图档*/
  37.   private Integer[] mImageIds = 
  38.   {
  39.     R.drawable.p1, R.drawable.p2, R.drawable.p3,
  40.     R.drawable.p4, R.drawable.p5, R.drawable.p6,
  41.     R.drawable.p7, R.drawable.p8
  42.   };
  43.   
  44.   /** Called when the activity is first created. */
  45.   @Override
  46.   public void onCreate(Bundle savedInstanceState)
  47.   {
  48.     super.onCreate(savedInstanceState);
  49.     requestWindowFeature(Window.FEATURE_NO_TITLE);
  50.     setContentView(R.layout.main);
  51.     /*设置Switcher*/
  52.     mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
  53.     mSwitcher.setFactory(this);
  54.     /*设置加载Switcher的模式*/
  55.     mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
  56.             android.R.anim.fade_in));
  57.     /*设置输出Switcher的模式*/
  58.     mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
  59.             android.R.anim.fade_out));
  60.     Gallery g = (Gallery) findViewById(R.id.gallery);
  61.     g.setAdapter(new ImageAdapter(this));
  62.     g.setOnItemSelectedListener(this);
  63.   
  64.     /*设置点击Gallery的图片事件*/
  65.     g.setOnItemClickListener(new Gallery.OnItemClickListener()
  66.     {
  67.       @Override
  68.       public void onItemClick(AdapterView<?> parent, View v,
  69.           final int position, long id)
  70.       {
  71.         // TODO Auto-generated method stub
  72.         new AlertDialog.Builder(EX07_07.this)
  73.         .setTitle(R.string.app_about)
  74.         /*设置弹出窗口的图式*/
  75.         .setIcon(mImageIds[position]) 
  76.         /*设置弹出窗口的信息*/
  77.         .setMessage(R.string.app_about_msg)
  78.         /*确认窗口*/
  79.         .setPositiveButton(R.string.str_ok,
  80.         new DialogInterface.OnClickListener()
  81.        {
  82.          public void onClick(DialogInterface dialoginterface,
  83.              int i)
  84.          {           
  85.           Resources resources = getBaseContext().getResources();
  86.           is = resources.openRawResource(mImageIds[position]);
  87.            try
  88.            {
  89.              /*更换桌布*/
  90.              setWallpaper(is); 
  91.              /*用Toast来显示桌布已更换*/
  92.              Toast.makeText(EX07_07.this, getString
  93.              (R.string.my_text_pre),Toast.LENGTH_SHORT).show();
  94.            }
  95.            catch (Exception e)
  96.            {            
  97.              e.printStackTrace();
  98.            };
  99.          }
  100.        })
  101.         /*设置跳出窗口的返回事件*/
  102.         .setNegativeButton(R.string.str_no,
  103.          new DialogInterface.OnClickListener()
  104.         {
  105.           public void onClick(DialogInterface dialoginterface,int i)
  106.          {
  107.             /*用Toast来显示桌布已取消*/
  108.             Toast.makeText(EX07_07.this, getString(R.string.
  109.                 my_text_no),Toast.LENGTH_SHORT).show();
  110.          }
  111.         })
  112.         .show();
  113.       }     
  114.     });
  115.   }  
  116.   
  117.   /*设置加载Switcher的图档*/
  118.   public void onItemSelected(AdapterView parent, View v,
  119.       int position, long id) 
  120.   {
  121.     mSwitcher.setImageResource(mImageIds[position]);
  122.   }
  123.   public void onNothingSelected(AdapterView parent)
  124.   {
  125.     
  126.   }
  127.   /*呈现Switcher的模式*/
  128.   public View makeView() 
  129.   {
  130.     ImageView i = new ImageView(this);
  131.     i.setBackgroundColor(0xFF000000);
  132.     i.setScaleType(ImageView.ScaleType.FIT_CENTER);
  133.     i.setLayoutParams(new ImageSwitcher.LayoutParams(
  134.         LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
  135.     return i;
  136.   }
  137.   public class ImageAdapter extends BaseAdapter
  138.   {
  139.     public ImageAdapter(Context c)
  140.     {
  141.      mContext = c;
  142.     }
  143.     public int getCount() 
  144.     {
  145.       return mImageIds.length;
  146.     }
  147.     public Object getItem(int position)
  148.     {
  149.       return position;
  150.     }
  151.     public long getItemId(int position)
  152.     {
  153.       return position;
  154.     }
  155.     /*显示Gallery里的模式*/
  156.     public View getView(int position, View convertView,
  157.                           ViewGroup parent)
  158.     {
  159.       ImageView i = new ImageView(mContext);
  160.       i.setImageResource(mThumbIds[position]);
  161.       i.setAdjustViewBounds(true);
  162.       i.setLayoutParams(new Gallery.LayoutParams(
  163.             LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  164.       i.setBackgroundResource(R.drawable.picture_frame);
  165.       return i;
  166.     }
  167.   }
  168. }