[Android开发笔记]Android中使用系统桌面背景作为应用背景,支持拖动

一、在Android应用开发中,使用系统桌面背景作为应用的背景,需要把应用的背景设置为透明背景,然后设置窗口的属性为FLAG_SHOW_WALLPAPER即可显示背景。

1>.修改AndroidManifest.xml文件里面activity属性:

       <activity android:name=".WallPaperTest"

                 android:label="@string/app_name"

                 android:theme="@android:style/Theme.Translucent">

2>.然后在使用的时候,在onCreate里面添加一个窗口属性

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);

 

 

 

 

二、背景拖动主要是使用了WallpaperManager这个类的两个方法

public voidsetWallpaperOffsetSteps(float xStep, float yStep)

1>.修改ScrollLayout的类,让它支持显示系统背景,并且拖动的时候背景也跟着拖动.基本类文件ScrollLayout.java

[java]  view plain  copy
  1. packagecom.yao_guet;

  2. importandroid.app.WallpaperManager;

  3. importandroid.content.Context;

  4. importandroid.os.IBinder;

  5. importandroid.util.AttributeSet;

  6. importandroid.util.Log;

  7. importandroid.view.MotionEvent;

  8. importandroid.view.VelocityTracker;

  9. importandroid.view.View;

  10. importandroid.view.ViewConfiguration;

  11. importandroid.view.ViewGroup;

  12. importandroid.widget.Scroller;

  13. /**

  14. * 仿Launcher中的WorkSapce,可以左右滑动切换屏幕的类,支持显示系统背景和滑动

  15. * @author Hekang

  16. * blog: 6600s.diandian.com

  17. * date: 2012-04-22

  18. */

  19. publicclassScrollLayoutextendsViewGroup {

  20. privatestaticfinalString TAG ="ScrollLayout";

  21. privateScroller mScroller;

  22. privateVelocityTracker mVelocityTracker;

  23.  

  24. privateintmCurScreen;

  25. privateintmDefaultScreen =0;

  26.  

  27. privatestaticfinalintTOUCH_STATE_REST =0;

  28. privatestaticfinalintTOUCH_STATE_SCROLLING =1;

  29.  

  30. privatestaticfinalintSNAP_VELOCITY =600;

  31.  

  32. privateintmTouchState = TOUCH_STATE_REST;

  33. privateintmTouchSlop;

  34. privatefloatmLastMotionX;

  35. privatefloatmLastMotionY;

  36.  

  37. privateWallpaperManager mWallpaperManager;

  38. privateintmScrollX;

  39. publicScrollLayout(Context context, AttributeSet attrs) {

  40. this(context, attrs,0);

  41. // TODO Auto-generated constructor stub

  42. }

  43. publicScrollLayout(Context context, AttributeSet attrs,intdefStyle) {

  44. super(context, attrs, defStyle);

  45. // TODO Auto-generated constructor stub

  46.  

  47. mWallpaperManager = WallpaperManager.getInstance(context);

  48. mScroller =newScroller(context);

  49.  

  50. mCurScreen = mDefaultScreen;

  51. mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();

  52. }

  53. @Override

  54. protectedvoidonLayout(booleanchanged,intl,intt,intr,intb) {

  55. // TODO Auto-generated method stub

  56. Log.e(TAG,"onLayout");

  57. intchildLeft =0;

  58. finalintchildCount = getChildCount();

  59.  

  60. for(inti=0; i<childCount; i++) {

  61. finalView childView = getChildAt(i);

  62. if(childView.getVisibility() != View.GONE) {

  63. finalintchildWidth = childView.getMeasuredWidth();

  64. childView.layout(childLeft,0,

  65. childLeft+childWidth, childView.getMeasuredHeight());

  66. childLeft += childWidth;

  67. }

  68. }

  69. updateWallpaperOffset();

  70. }

  71.  

  72. @Override

  73. protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec) {

  74. Log.e(TAG,"onMeasure");

  75. super.onMeasure(widthMeasureSpec, heightMeasureSpec);

  76.  

  77. finalintwidth = MeasureSpec.getSize(widthMeasureSpec);

  78. finalintwidthMode = MeasureSpec.getMode(widthMeasureSpec);

  79. if(widthMode != MeasureSpec.EXACTLY) {

  80. thrownewIllegalStateException("ScrollLayout only canmCurScreen run at EXACTLY mode!");

  81. }

  82.  

  83. finalintheightMode = MeasureSpec.getMode(heightMeasureSpec);

  84. if(heightMode != MeasureSpec.EXACTLY) {

  85. thrownewIllegalStateException("ScrollLayout only can run at EXACTLY mode!");

  86. }

  87.  

  88. // The children are given the same width and height as the scrollLayout

  89. finalintcount = getChildCount();

  90. for(inti =0; i < count; i++) {

  91. getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);

  92. }

  93. // Log.e(TAG, "moving to screen "+mCurScreen);

  94. scrollTo(mCurScreen * width,0);

  95. }

  96.  

  97. /**

  98. * According to the position of current layout

  99. * scroll to the destination page.

  100. */

  101. publicvoidsnapToDestination() {

  102. finalintscreenWidth = getWidth();

  103. finalintdestScreen = (getScrollX()+ screenWidth/2)/screenWidth;

  104. snapToScreen(destScreen);

  105. }

  106.  

  107. publicvoidsnapToScreen(intwhichScreen) {

  108. // get the valid layout page

  109. whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));

  110. if(getScrollX() != (whichScreen*getWidth())) {

  111.  

  112. finalintdelta = whichScreen*getWidth()-getScrollX();

  113. mScroller.startScroll(getScrollX(),0,

  114. delta,0, Math.abs(delta)*2);

  115. mCurScreen = whichScreen;

  116. invalidate();// Redraw the layout

  117. }

  118. }

  119.  

  120. publicvoidsetToScreen(intwhichScreen) {

  121. whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));

  122. mCurScreen = whichScreen;

  123. scrollTo(whichScreen*getWidth(),0);

  124. }

  125.  

  126. publicintgetCurScreen() {

  127. returnmCurScreen;

  128. }

  129.  

  130. @Override

  131. publicvoidcomputeScroll() {

  132. // TODO Auto-generated method stub

  133. mScrollX = mScroller.getCurrX();

  134. if(mScroller.computeScrollOffset()) {

  135. scrollTo(mScroller.getCurrX(), mScroller.getCurrY());

  136. updateWallpaperOffset();

  137.  

  138. postInvalidate();

  139. }

  140. }

  141. @Override

  142. publicbooleanonTouchEvent(MotionEvent event) {

  143. // TODO Auto-generated method stub

  144.  

  145. if(mVelocityTracker ==null) {

  146. mVelocityTracker = VelocityTracker.obtain();

  147. }

  148. mVelocityTracker.addMovement(event);

  149.  

  150. finalintaction = event.getAction();

  151. finalfloatx = event.getX();

  152. finalfloaty = event.getY();

  153.  

  154. switch(action) {

  155. caseMotionEvent.ACTION_DOWN:

  156. Log.e(TAG,"event down!");

  157. if(!mScroller.isFinished()){

  158. mScroller.abortAnimation();

  159. }

  160. mLastMotionX = x;

  161. break;

  162.  

  163. caseMotionEvent.ACTION_MOVE:

  164. intdeltaX = (int)(mLastMotionX - x);

  165. mLastMotionX = x;

  166. scrollBy(deltaX,0);

  167. updateWallpaperOffset();

  168. break;

  169.  

  170. caseMotionEvent.ACTION_UP:

  171. Log.e(TAG,"event : up");

  172. // if (mTouchState == TOUCH_STATE_SCROLLING) {

  173. finalVelocityTracker velocityTracker = mVelocityTracker;

  174. velocityTracker.computeCurrentVelocity(1000);

  175. intvelocityX = (int) velocityTracker.getXVelocity();

  176. Log.e(TAG,"velocityX:"+velocityX);

  177.  

  178. if(velocityX > SNAP_VELOCITY && mCurScreen >0) {

  179. // Fling enough to move left

  180. Log.e(TAG,"snap left");

  181. snapToScreen(mCurScreen -1);

  182. }elseif(velocityX < -SNAP_VELOCITY

  183. && mCurScreen < getChildCount() -1) {

  184. // Fling enough to move right

  185. Log.e(TAG,"snap right");

  186. snapToScreen(mCurScreen +1);

  187. }else{

  188. snapToDestination();

  189. }

  190. if(mVelocityTracker !=null) {

  191. mVelocityTracker.recycle();

  192. mVelocityTracker =null;

  193. }

  194. // }

  195. mTouchState = TOUCH_STATE_REST;

  196. break;

  197. caseMotionEvent.ACTION_CANCEL:

  198. mTouchState = TOUCH_STATE_REST;

  199. break;

  200. }

  201.  

  202. returntrue;

  203. }

  204. @Override

  205. publicbooleanonInterceptTouchEvent(MotionEvent ev) {

  206. // TODO Auto-generated method stub

  207. Log.e(TAG,"onInterceptTouchEvent-slop:"+mTouchSlop);

  208.  

  209. finalintaction = ev.getAction();

  210. if((action == MotionEvent.ACTION_MOVE) &&

  211. (mTouchState != TOUCH_STATE_REST)) {

  212. returntrue;

  213. }

  214.  

  215. finalfloatx = ev.getX();

  216. finalfloaty = ev.getY();

  217.  

  218. switch(action) {

  219. caseMotionEvent.ACTION_MOVE:

  220. finalintxDiff = (int)Math.abs(mLastMotionX-x);

  221. if(xDiff>mTouchSlop) {

  222. mTouchState = TOUCH_STATE_SCROLLING;

  223.  

  224. }

  225. break;

  226.  

  227. caseMotionEvent.ACTION_DOWN:

  228. mLastMotionX = x;

  229. mLastMotionY = y;

  230. mTouchState = mScroller.isFinished()? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;

  231. break;

  232.  

  233. caseMotionEvent.ACTION_CANCEL:

  234. caseMotionEvent.ACTION_UP:

  235. mTouchState = TOUCH_STATE_REST;

  236. break;

  237. }

  238.  

  239. returnmTouchState != TOUCH_STATE_REST;

  240. }

  241.  

  242.  

  243. privatevoidupdateWallpaperOffset() {

  244. intscrollRange = getChildAt(getChildCount() -1).getRight() - getWidth();

  245. IBinder token = getWindowToken();

  246. if(token !=null) {

  247. mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() -1),0);

  248. mWallpaperManager.setWallpaperOffsets(getWindowToken(),

  249. Math.max(0.f, Math.min(getScrollX()/(float)scrollRange,1.f)),0);

  250. }

  251. }

  252. }

2>.测试代码WallPaperTest.java:

 

 

[java]  view plain  copy
  1. packagecom.yao_guet;

  2. importandroid.app.Activity;

  3. importandroid.app.WallpaperManager;

  4. importandroid.content.Context;

  5. importandroid.os.Bundle;

  6. importandroid.view.KeyEvent;

  7. importandroid.view.Window;

  8. importandroid.view.WindowManager;

  9. publicclassWallPaperTestextendsActivity {

  10. @Override

  11. protectedvoidonCreate(Bundle savedInstanceState) {

  12. // TODO Auto-generated method stub

  13. super.onCreate(savedInstanceState);

  14. this.requestWindowFeature(Window.FEATURE_NO_TITLE);

  15. setContentView(R.layout.wallpaper_test);

  16. getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);

  17. }

  18. @Override

  19. protectedvoidonDestroy() {

  20. // TODO Auto-generated method stub

  21. super.onDestroy();

  22. }

  23. @Override

  24. publicbooleanonKeyDown(intkeyCode, KeyEvent event) {

  25. // TODO Auto-generated method stub

  26. returnsuper.onKeyDown(keyCode, event);

  27. }

  28. }


3>.layout布局文件wallpaper_test.xml:

[html]  view plain  copy
  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <com.sf.test.ScrollLayout

  3. xmlns:android="http://schemas.android.com/apk/res/android"

  4. android:id="@+id/WallPaperTest"

  5. android:layout_width="fill_parent"

  6. android:layout_height="fill_parent">

  7. <LinearLayout

  8. android:layout_width="fill_parent"

  9. android:layout_height="fill_parent">

  10. <TextView

  11. android:layout_width="fill_parent"

  12. android:layout_height="wrap_content"

  13. android:text="This is page 1"/>

  14. </LinearLayout>

  15.  

  16. <LinearLayout

  17. android:layout_width="fill_parent"

  18. android:layout_height="fill_parent">

  19. <TextView

  20. android:layout_width="fill_parent"

  21. android:layout_height="wrap_content"

  22. android:text="This is page 2"/>

  23. </LinearLayout>

  24.  

  25. <LinearLayout

  26. android:layout_width="fill_parent"

  27. android:layout_height="fill_parent">

  28. <TextView

  29. android:layout_width="fill_parent"

  30. android:layout_height="wrap_content"

  31. android:text="This is page 3"/>

  32. </LinearLayout>

  33.  

  34. <LinearLayout

  35. android:layout_width="fill_parent"

  36. android:layout_height="fill_parent">

  37. <TextView

  38. android:layout_width="fill_parent"

  39. android:layout_height="wrap_content"

  40. android:text="This is page 4"/>

  41. </LinearLayout>

  42. <LinearLayout

  43. android:layout_width="fill_parent"

  44. android:layout_height="fill_parent">

  45. <TextView

  46. android:layout_width="fill_parent"

  47. android:layout_height="wrap_content"

  48. android:text="This is page 5"/>

  49. </LinearLayout>

  50. <LinearLayout

  51. android:layout_width="fill_parent"

  52. android:layout_height="fill_parent">

  53. <TextView

  54. android:layout_width="fill_parent"

  55. android:layout_height="wrap_content"

  56. android:text="This is page 6"/>

  57. </LinearLayout>

  58.  

  59. <LinearLayout

  60. android:layout_width="fill_parent"

  61. android:layout_height="fill_parent">

  62. <TextView

  63. android:layout_width="fill_parent"

  64. android:layout_height="wrap_content"

  65. android:text="This is page 7"/>

  66. </LinearLayout>

  67. </com.sf.test.ScrollLayout>

4>.最后记得修改AndroidManifest.xml文件

       <activity android:name=".WallPaperTest"

                 android:label="@string/app_name"

                 android:theme="@android:style/Theme.Translucent">

猜你喜欢

转载自6600s.iteye.com/blog/1843046