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)用。首先來看我的布局代碼:
<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類:
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)的代碼如下:
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)文章
Android MQTT與WebSocket協(xié)議詳細(xì)講解
MQTT(消息隊(duì)列遙測(cè)傳輸)是ISO 標(biāo)準(zhǔn)(ISO/IEC PRF 20922)下基于發(fā)布/訂閱范式的消息協(xié)議。它工作在TCP/IP協(xié)議族上,是為硬件性能低下的遠(yuǎn)程設(shè)備以及網(wǎng)絡(luò)狀況糟糕的情況下而設(shè)計(jì)的發(fā)布/訂閱型消息協(xié)議2022-11-11Android中FlowLayout組件實(shí)現(xiàn)瀑布流效果
大家好,本篇文章主要講的是Android中FlowLayout組件實(shí)現(xiàn)瀑布流效果,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01Android自定義View app更新動(dòng)畫詳解
這篇文章給大家分享了Android自定義View app更新動(dòng)畫的相關(guān)代碼以及知識(shí)點(diǎn)內(nèi)容,有興趣的朋友參考學(xué)習(xí)下。2018-07-07Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android TextView字體顏色設(shè)置方法小結(jié)
這篇文章主要介紹了Android TextView字體顏色設(shè)置方法,結(jié)合實(shí)例形式總結(jié)分析了Android開發(fā)中TextView設(shè)置字體顏色的常用技巧,需要的朋友可以參考下2016-02-02Android實(shí)現(xiàn)價(jià)格走勢(shì)自定義曲線圖
本篇文章主要介紹了Android實(shí)現(xiàn)價(jià)格走勢(shì)曲線圖,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04Android實(shí)現(xiàn)定制返回按鈕動(dòng)畫效果的方法
這篇文章主要介紹了Android實(shí)現(xiàn)定制返回按鈕動(dòng)畫效果的方法,涉及Android控件及動(dòng)畫的相關(guān)操作技巧,需要的朋友可以參考下2016-02-02Android View.onMeasure方法詳解及實(shí)例
這篇文章主要介紹了Android View.onMeasure方法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05