亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙?jiān)敿?xì)介紹與實(shí)例

 更新時(shí)間:2013年12月06日 15:45:32   作者:  
這篇文章主要介紹了Android開發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙?jiān)敿?xì)介紹與實(shí)例,有需要的朋友可以參考一下

今天和大家分享的是關(guān)于在Android中設(shè)置壁紙的方法,在Android中設(shè)置壁紙的方法有三種,分別是:

1、使用WallpaperManager的setResource(int ResourceID)方法

2、使用WallpaperManager的setBitmap(Bitmap bitmap)方法

3、重寫ContextWrapper 類中提供的setWallpaper()

除此之外,我們還需要在應(yīng)用程序中加入下列權(quán)限: <uses-permission android:name="android.permission.SET_WALLPAPER"/>

下面我們以此為基本方法,來實(shí)現(xiàn)Android中自帶的壁紙應(yīng)用。首先來看我的布局代碼:

復(fù)制代碼 代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#000000"
    tools:context=".MainActivity" >
    <ImageSwitcher
        android:id="@+id/ImageSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="370dp">
    </ImageSwitcher>
    <Gallery
        android:id="@+id/Gallery"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_below="@+id/ImageSwitcher"  />
    <Button
        android:id="@+id/BtnGo"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_below="@+id/Gallery"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/BtnGo" />
</RelativeLayout>

 在這里我們使用Gallery來實(shí)現(xiàn)一個(gè)可以供用戶選擇的縮略圖列表,當(dāng)用戶選擇列表中的圖像時(shí),會(huì)在ImageSwitcher控件中顯示出當(dāng)前圖像,當(dāng)點(diǎn)擊Button時(shí),當(dāng)前圖片將被設(shè)置為壁紙。其實(shí)這里的ImageSwitcher完全可以替換為ImageView,考慮到ImageSwitcher可以提供較好的動(dòng)畫效果,所以我們?cè)谶@里選擇了ImageSwitcher。同樣地,我們繼續(xù)使用Android開發(fā)學(xué)習(xí)之Gallery中的那個(gè)ImageAdapter類:

 

復(fù)制代碼 代碼如下:

 package com.android.gallery2switcher;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter{

 //類成員myContext為context父類
 private Context myContext;
 private int[] myImages;

 //構(gòu)造函數(shù),有兩個(gè)參數(shù),即要存儲(chǔ)的Context和Images數(shù)組
 public ImageAdapter(Context c,int[] Images)
 {
  // TODO Auto-generated constructor stub
  this.myContext=c;
  this.myImages=Images;
 }

 //返回所有的圖片總數(shù)量
 @Override
 public int getCount()
 {

  return this.myImages.length;
 }

 //利用getItem方法,取得目前容器中圖像的數(shù)組ID
 @Override
 public Object getItem(int position)
 {
  return position;
 }


 @Override
 public long getItemId(int position)
 {
  return position;
 }

 //取得目前欲顯示的圖像的VIEW,傳入數(shù)組ID值使之讀取與成像
 @Override
 public View getView(int position, View convertView, ViewGroup parent)
 {
  ImageView image=new ImageView(this.myContext);
  image.setImageResource(this.myImages[position]);
  image.setScaleType(ImageView.ScaleType.FIT_XY);
  image.setAdjustViewBounds(true);
  return image;
 }

}
 

 現(xiàn)在,我們就可以開始編寫程序了,后臺(tái)的代碼如下:
 

復(fù)制代碼 代碼如下:

 package com.android.gallery2switcher;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.app.WallpaperManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {

 Gallery mGallery;
 ImageSwitcher mSwitcher;
 Button BtnGo;
 int[] Resources=new int[]{R.drawable.image0,R.drawable.image1,R.drawable.image2,R.drawable.image3,
   R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8};
 int index;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //不顯示標(biāo)題欄
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_main);
  mGallery=(Gallery)findViewById(R.id.Gallery);
  mSwitcher=(ImageSwitcher)findViewById(R.id.ImageSwitcher);
  //實(shí)現(xiàn)ImageSwitcher的工廠接口
    mSwitcher.setFactory(new ViewFactory()
    {
     @Override
     public View makeView()
     {
        ImageView i = new ImageView(MainActivity.this);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        return i;
     }
    });
     //設(shè)置資源
     mSwitcher.setImageResource(Resources[0]);
     //設(shè)置動(dòng)畫
     mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
     mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
  BtnGo=(Button)findViewById(R.id.BtnGo);
  BtnGo.setOnClickListener(new OnClickListener()
  {
   @Override
   public void onClick(View arg0)
   {
    SetWallPaper();
   }
  });
  ImageAdapter mAdapter=new ImageAdapter(this,Resources);
  mGallery.setAdapter(mAdapter);
  mGallery.setOnItemSelectedListener(new OnItemSelectedListener()
  {
   @Override
   public void onItemSelected(AdapterView<?> Adapter, View view,int position, long id)
   {
    //設(shè)置圖片
    mSwitcher.setImageResource(Resources[position]);
    //獲取當(dāng)前圖片索引
    index=position;
   }
   @Override
   public void onNothingSelected(AdapterView<?> arg0)
   {

   }

  });

      
 }
 //設(shè)置壁紙
    public void SetWallPaper()
    {
     WallpaperManager mWallManager=WallpaperManager.getInstance(this);
     try
     {
   mWallManager.setResource(Resources[index]);
  }
     catch (IOException e)
     {
   e.printStackTrace();
  }
    }

 @Override
 public boolean onCreateOptionsMenu(Menu menu)
 {
  return true;
 }

}
 

 可以看到,在使用ImageSwitcher的時(shí)候,我們需要實(shí)現(xiàn)它的工廠接口,并且這里的makeView()方法和BaseAdapter里的getView()方法是一樣的,即返回一個(gè)View視圖。我們ImageSwitcher給使用了系統(tǒng)默認(rèn)的動(dòng)畫效果。最終運(yùn)行效果如下:

相關(guān)文章

最新評(píng)論