壁紙的設置需要一定的權限
<uses-permission android:name="android.permission.SET_WALLPAPER" /> 獲取壁紙管理器需要的權限
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" /> suggestDesiredDimensions方法需要加權限
設置壁紙
一、將整張圖片設置爲壁紙
相應的代碼
獲得壁紙管理器
WallpaperManager wallpaperManager =
WallpaperManager.getInstance(getApplicationContext();
得到壁紙管理器後設置壁紙 只需一行代碼
wallpaper.setBitmap(Bitmap bitmap);
setResource(int
resid) setStream(InputStream
data)
二、截取一定的圖片大小設置爲壁紙
截取手機屏幕大小的尺寸設置爲壁紙
中間一個主要的方法就是
wallpaperManager.suggestDesiredDimensions(deviceSize[0],deviceSize[1]);
其中deviceSize[0]和deviceSize[1]分別是手機屏幕的寬和高
獲取方法爲 public static int[] getWidthHeight(Context context) {
int w_screen = 0;
int h_screen = 0;
try {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
w_screen = dm.widthPixels;
h_screen = dm.heightPixels;
} catch (Exception e) {
MyLog.printLog(e);
}
return new int[] { w_screen, h_screen };
}
整體
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
wallpaperManager.suggestDesiredDimensions(deviceSize[0],deviceSize[1]);
Bitmap bitmap = captureScreen(WallPagerActivity.this);
wallpaperManager.setBitmap(bitmap);//根據規定設置 得到bitmap對象 然後進行設置壁紙的操作
captureScreen()方法
private Bitmap captureScreen(Activity
activity) {//這是截屏的操作
activity.getWindow().getDecorView().setDrawingCacheEnabled(true);
Bitmap bitmap = activity.getWindow().getDecorView().getDrawingCache();
return bitmap;
}
在對壁紙進行截取的界面 還需要對防止壁紙圖片的Imageview控件 進行一定的設置 因爲可以滑動 應將ImageView放在一個HorizontalScrollView中
相應的對ImageView的設置爲
private View getImageView() {
ImageView imgView = new ImageView(getApplicationContext());
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(WallPagerActivity.this);
Bitmap bitmap = DownLoadImage.decodeSampledBitmapFromResource(filePath,
deviceSize[0], deviceSize[1]);
if (bitmap != null) {
double ratio = bitmap.getHeight() / (deviceSize[1] * 1.0);
int scaleWidth = (int) (bitmap.getWidth() / ratio);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
scaleWidth, deviceSize[1]);
imgView.setLayoutParams(layoutParams);
imgView.setImageBitmap(bitmap);
bitmap = null;
}
imgView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
return imgView;
}
-------------------------------------------------------------------------------------------------------------------------------------
全部代碼
package com.cn.girlshbb;
import java.io.IOException;
import com.cn.girlshbb.I.ICallBack;
import com.cn.girlshbb.bean.SexyDataBean;
import com.cn.girlshbb.db.DBSQLiteManager;
import com.cn.girlshbb.db.DBUtils;
import com.cn.girlshbb.imageutils.DownLoadImage;
import com.cn.girlshbb.imageutils.SDCardHelper;
import com.cn.girlshbb.utils.ApplicationUtils;
import com.umeng.analytics.MobclickAgent;
import android.os.Bundle;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class WallPagerActivity extends Activity {
private String filePath;
private LinearLayout linearLayout;
private int[] deviceSize;
private String imgUrl;
private SexyDataBean sexyDataBean;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wall_pager);
deviceSize = ApplicationUtils.getWidthHeight(getApplicationContext());
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
int currentIndex = bundle.getInt("currentIndex", 0);
filePath = bundle.getString("filePath");
imgUrl = bundle.getString("imgUrl");
sexyDataBean = (SexyDataBean) bundle.getSerializable("databean");
}
initView();
}
private void initView() {
linearLayout = (LinearLayout) findViewById(R.id.activity_wallPager_horizontalScrollView_linerLayout);
linearLayout.addView(getImageView());
Button button = (Button) findViewById(R.id.activity_wallPager_downBtn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View arg0) {
// 統計setting點擊次數
MobclickAgent.onEvent(WallPagerActivity.this,
ApplicationData.click_setting);
setWallpaper();
/*if (!getSharedPreferences(ApplicationData.sharedPreferencesTAG,
Context.MODE_PRIVATE).getBoolean("hasGo", false)) {
// TODO 彈出計費窗口
int apiLever = ApplicationUtils.getVersionSdk();
if (apiLever>=19) {
ApplicationUtils.showAlertView(WallPagerActivity.this,
new ICallBack() {
@Override
public void result() {
arg0.setVisibility(View.GONE);
// TODO 同意收費
setWallpaper();
}
});
}else{
String status = getSharedPreferences(ApplicationData.sharedPreferencesTAG, Context.MODE_PRIVATE).getString("status", null);
if ("0".equals(status)) {
ApplicationUtils.showAlertView(WallPagerActivity.this,
new ICallBack() {
@Override
public void result() {
arg0.setVisibility(View.GONE);
// TODO 同意收費
setWallpaper();
}
});
}else if("1".equals(status)){
ApplicationUtils.paySdkMethod(getApplicationContext());
}
}
} else {
arg0.setVisibility(View.GONE);
setWallpaper();
}*/
}
/**
* 設置壁紙
*/
public void setWallpaper() {
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
wallpaperManager.suggestDesiredDimensions(deviceSize[0],
deviceSize[1]);
Bitmap bitmap = captureScreen(WallPagerActivity.this);
DBSQLiteManager manager = DBSQLiteManager
.getIstance(getApplicationContext());
// 判斷數據庫 選擇性插入數據
if (manager.isNotContains(DBUtils.TABLE_DOWNlOAD, imgUrl)) {
manager.insert(DBUtils.TABLE_DOWNlOAD, sexyDataBean);
}
if (manager.isNotContains(DBUtils.TABLE_USED, imgUrl)) {
manager.insert(DBUtils.TABLE_USED, sexyDataBean);
}
try {
DownLoadImage downLoadImage = new DownLoadImage(WallPagerActivity.this);
if (imgUrl!=null) {
try {
String fileName = imgUrl.substring(imgUrl.lastIndexOf("/")+1);
if (!downLoadImage.isSavedFile(ApplicationData.rootPath+fileName)) {
byte []data = SDCardHelper.loadFileFromSDCard(ApplicationData.jumpLoadPath, fileName);
SDCardHelper.saveFileToSDCard(data, ApplicationData.rootPath, fileName);
}
} catch (Exception e) {
// TODO: handle exception
}
}
finish();
wallpaperManager.setBitmap(bitmap);
bitmap = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Bitmap captureScreen(Activity activity) {
activity.getWindow().getDecorView()
.setDrawingCacheEnabled(true);
Bitmap bitmap = activity.getWindow().getDecorView()
.getDrawingCache();
return bitmap;
}
});
}
// /檢查getImageView
private View getImageView() {
ImageView imgView = new ImageView(getApplicationContext());
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(WallPagerActivity.this);
Bitmap bitmap = DownLoadImage.decodeSampledBitmapFromResource(filePath,
deviceSize[0], deviceSize[1]);
if (bitmap != null) {
double ratio = bitmap.getHeight() / (deviceSize[1] * 1.0);
int scaleWidth = (int) (bitmap.getWidth() / ratio);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
scaleWidth, deviceSize[1]);
imgView.setLayoutParams(layoutParams);
imgView.setImageBitmap(bitmap);
bitmap = null;
}
imgView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
return imgView;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
MobclickAgent.onResume(this);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
MobclickAgent.onPause(this);
}
}
xml 文件爲
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WallPagerActivity"
android:background="@color/white" >
<HorizontalScrollView
android:id="@+id/activity_wallPager_horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
<LinearLayout
android:id="@+id/activity_wallPager_horizontalScrollView_linerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/activity_wallPager_downBtn"
android:text="@string/sheWEIBiZhi"
android:layout_alignParentBottom="true"
android:alpha="0.3" />
</LinearLayout>
</RelativeLayout>