您的位置:首页 > 移动开发 > Android开发

android开发壁纸的设置

2016-02-01 09:36 393 查看
壁纸的设置需要一定的权限

<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: