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

[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"是指一个特殊定制的GridView,它模仿了Android桌面应用的交互方式,允许用户通过拖放操作来改变GridView中各个元素的位置。这种功能在创建自定义启动器、文件管理器或个性化布局...

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

    在Android中,Drag and Drop(拖放)是一种交互方式,让用户可以通过触摸屏或物理按键将一个对象(如图标、文本或图片)从一处移动到另一处。在Launcher中,这个功能主要应用于移动应用快捷方式、小部件或文件夹的...

    android桌面拖拽效果

    在Android系统中,桌面拖拽效果是用户交互的重要组成部分,它极大地提升了用户的操作体验。本文将深入探讨Android桌面拖拽效果的实现原理、关键技术和优化方法。 首先,我们需要理解Android桌面的基本架构。Android...

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

    在Android系统中,Launcher是用户与设备交互的主要入口,它是一个桌面应用,负责显示应用程序的图标、快捷方式以及小部件等。"安卓Launcher桌面相关-Android实现图标拖拽"这个压缩包文件似乎包含了一些关于如何在...

    可视化ANDROID开发系统

    总的来说,“可视化Android开发系统”借助BASIC语言的易用性,降低了Android应用开发的复杂度,使得更多人有机会参与到移动应用的创新之中。它不仅适合初学者入门,也对有经验的开发者提供了一种快速原型开发的途径...

    Android悬浮笔记应用

    在Android平台上,开发一款悬浮笔记应用是一项有趣且实用的任务,它允许用户在任何其他应用程序之上进行笔记记录,提高工作效率和学习便利性。本教程将详细讲解如何实现这样的功能,主要聚焦于Android悬浮框的使用。...

    android桌面悬浮窗

    在Android系统中,桌面悬浮窗是一种非常实用的功能,它可以在用户使用其他应用程序的同时显示重要的信息或者提供便捷的操作入口。本文将深入探讨如何实现一个具备自由拖动和智能隐藏功能的Android桌面悬浮窗。 首先...

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

    而支持拖拽排序的流式标签布局则进一步提升了用户体验,用户可以通过直接拖动标签来改变它们的顺序,使得应用的交互更加直观和便捷。 实现这样的功能通常涉及到以下几个关键技术点: 1. **自定义ViewGroup**:首先...

    Android悬浮笔记应用+源码

    在Android中,可以使用`EditText`作为输入控件,用户在此输入文字。为了保存这些笔记,开发者通常会利用SQLite数据库进行本地存储,或者使用云同步服务如Google Drive进行远程备份。笔记数据结构的设计包括标题、...

    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开发中,创建一个类似安卓系统桌面的用户体验,其中包括长按拖拽功能,是提升应用交互性的重要一环。这个项目"类似安卓系统桌面(长按拖拽效果)"显然是一个实现这一功能的示例代码,适用于Eclipse开发环境...

    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入门到精通源代码.

    第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 自定义Seekbar,包括图片,背景颜色

    本教程将详细介绍如何在Android中自定义Seekbar,包括设置自定义图片和背景颜色。 首先,我们需要创建一个新的XML布局文件来定义自定义Seekbar。这个文件通常会放在res/layout目录下,例如命名为`custom_seekbar....

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

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

    ANDROID 开发文档.pdf

    - **概述**:深入讲解Android中的Service组件及其使用方法。 - **应用场景**:实现后台任务和服务。 **33. Android下添加耳机插拔提示** - **概述**:实现耳机插拔时的通知提示。 - **应用场景**:提高用户体验。 ...

    android控件在界面上自由拖动位置显示

    Android中的坐标系统可能与用户预期的屏幕坐标不一致,因此需要进行坐标转换。使用`view.getLocationOnScreen(int[])`或`view.getX()`和`view.getY()`获取控件在屏幕上的绝对位置,再根据ACTION_MOVE事件中的偏移量...

    Android-Android图片选择预览九宫格图片控件拖拽排序九宫格图片控件

    在Android应用开发中,图片的选择、预览以及有效的展示方式是不可或缺的部分,特别是在社交媒体或个性化设置的应用场景下。本文将详细讲解如何实现一个Android图片选择、预览功能,并且介绍如何设计一个支持拖拽排序...

    Android应用源码之(鼠标关节拖拽Body).zip

    在Android应用开发中,"Android应用源码之(鼠标关节拖拽Body)"的主题涉及到了一些高级的用户交互技术。这个源码可能包含了如何实现通过鼠标或触摸设备的关节拖动功能,让应用中的对象(例如游戏中的角色或者界面元素...

Global site tag (gtag.js) - Google Analytics