`
杜康_酒剑仙
  • 浏览: 15041 次
  • 来自: 重庆
最近访客 更多访客>>
社区版块
存档分类
最新评论

[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">

 

分享到:
评论

相关推荐

    Android中使用系统桌面背景作为应用背景,支持拖动 (转载,原文见资源描述)

    前辈写的例子,正好做项目需要,对我帮助很大,整理成了代码工程。 原文地址: http://blog.csdn.net/yao_guet/article/details/6572739 如有侵犯您的权益,请告知我。

    Android类似桌面应用拖动的GridView

    Android上类似桌面应用的一个GridView,可以拖动位置,支持多页,可以拖动子项到另一页。

    Android-Android支持拖拽排序的流式标签布局

    Android 支持拖拽排序的流式标签布局 

    Android开发应用实战详解源代码

    4.2 将背景图片作为按钮 4.3 用toast实现提示 4.4 用checkbox实现一个简单的物品清单 4.5 实现同意条款效果 4.6 radiogroup选择 4.7 imageview相框 4.8 spinner选择处理 4.9 gallery相簿 4.10 用.iava.io.file实现...

    android开发揭秘PDF

    第10章 Android应用开发 实例 10.1 情境模式 10.2 文件管理器 10.3 通讯录 10.4 音乐播放器 10.5 天气预报 10.6 个人地图 10.7 Widget日历 10.8 小结 第11 章Android游戏开发实例 11.1 手机游戏开发简介 11.2 游戏...

    《Android应用开发揭秘》附带光盘代码.

    《Android应用开发揭秘》全部实例源代码,配合《Android应用开发揭秘》使用 前言  第一部分 准备篇  第1章 Android开发简介  1.1 Android基本概念  1.1.1 Android简介  1.1.2 Android的系统构架  1.1.3 ...

    android桌面拖拽效果

    android桌面拖拽效果

    《Android应用开发揭秘》源码

     杨丰盛,Android应用开发先驱,对Android有深入研究,实战经验极其丰富。精通Java、C、C++等语言,专注于移动通信软件开发,在机顶盒软件开发和MTK平台软件开发方面有非常深厚的积累。2007年获得中国软件行业协会...

    安卓Launcher桌面相关-android桌面拖拽效果.rar

    android桌面拖拽效果.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

    Android开发与应用——张荣,原书配套课件

    这是Android开发与应用,原书配套的课件,作者张荣,目录如下。 第1章 Android简介 1.1 手机操作系统 1.2 Android起源 1.3 Android特征 1.4 Android体系结构 1.4.1 应用层 1.4.2 应用框架层 1.4.3 ...

    android桌面悬浮窗

    android下的桌面悬浮窗,可自由拖动,并监控在运行非桌面app的时候隐藏它。

    Android应用开发揭秘pdf高清版

    《Android应用开发揭秘》内容全面,不仅详细讲解了Android框架、Android组件、用户界面开发、游戏开发、数据存储、多媒体开发和网络开发等基础知识,而且还深入阐述了传感器、语音识别、桌面组件开发、Android游戏...

    Android-app.rar

    100多个Android的实用开源小应用,包含Android -- 引导页面的实现 Android APP引导页大全 Android Push it单机版事件管理APP Android RecycleView+任意头尾布局+拖拽换位+拖拽删除demo Android sql练习合集 Android ...

    android开发资料大全

    Android中利用画图类和线程画出闪烁的心形,送给亲爱的他 android自带的示例程序 BluetoothChat 变蓝牙串口助手(内含DIY蓝牙遥控车附源码实例教程) Android高手过招 FAQ 网友收集的android开发书籍(可下载哦)...

    安卓Launcher桌面相关-Android实现图标拖拽.rar

    Android实现图标拖拽.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

    android开发实例大全_王东华

    本书以Android应用程序的开发为主题,并结合真实的案例向读者详细介绍了Android的基本组件的使用及应用程序开发的整个流程。本书的讲述由浅入深,实例全面并典型,几乎囊括了所有和Android应用相关的项目。全书分为...

    仿iphone桌面拖动排序

    这是android仿ios桌面的应用,支持拖动排序。 高仿iphone桌面,排列个数支持自适应设备哦。 由于是demo,若设备上程序安装过多,需要等长一点时间界面才能刷新出来。 这一版本支持文件夹操作。慢慢拖哦,手机性能高...

    Android入门到精通源代码.

    第3章 Android中的Activity 3.1 Activity的作用 3.2 单Activity的Android应用 3.2.1 Activity的生命周期 3.2.2 Activity类的结构 3.3 Activity的两种界面设计方式 3.3.1 基于XML的界面设计 3.3.2 基于代码的界面设计...

    android开发入门与实战(下)

    11.2 应用开发步骤 11.2.1 界面设计始终是第一位——实现UI 11.2.2 必备的动力源泉——数据操作和存储 11.2.3 华丽转身——实现多页面跳转 11.2.4 始终为用户做好服务——增加Service 11.2.5 细节决定成败——完善...

Global site tag (gtag.js) - Google Analytics