android 桌面透明

来源:互联网 发布:乐器教学软件 编辑:程序博客网 时间:2024/04/29 17:20


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

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

 

        <activity android:name=".WallPaperTest"

                  android:label="@string/app_name"

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

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

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

 

在背景拖动的时候主要是使用了WallpaperManager这个类的两个方法

 

public void setWallpaperOffsetSteps (float xStep, float yStep)

Since: API Level 7

For applications that use multiple virtual screens showing a wallpaper, specify the step size between virtual screens. For example, if the launcher has 3 virtual screens, it would specify an xStep of 0.5, since the X offset for those screens are 0.0, 0.5 and 1.0

Parameters
xStepThe X offset delta from one screen to the next oneyStepThe Y offset delta from one screen to the next one

public void setWallpaperOffsets (IBinder windowToken, float xOffset, float yOffset)

Since: API Level 5

Set the position of the current wallpaper within any larger space, when that wallpaper is visible behind the given window. The X and Y offsets are floating point numbers ranging from 0 to 1, representing where the wallpaper should be positioned within the screen space. These only make sense when the wallpaper is larger than the screen.

Parameters
windowTokenThe window who these offsets should be associated with, as returned by View.getWindowToken().xOffsetThe offset along the X dimension, from 0 to 1.yOffset

The offset along the Y dimension, from 0 to 1.

 

修改了之前ScrollLayout的类,让它支持显示系统背景,并且拖动的时候背景也跟着拖动,跟Launcher中的效果一致。。。
基本类文件ScrollLayout.java
[java] view plaincopyprint?
  1. package com.yao_guet;  
  2. import android.app.WallpaperManager;  
  3. import android.content.Context;  
  4. import android.os.IBinder;  
  5. import android.util.AttributeSet;  
  6. import android.util.Log;  
  7. import android.view.MotionEvent;  
  8. import android.view.VelocityTracker;  
  9. import android.view.View;  
  10. import android.view.ViewConfiguration;  
  11. import android.view.ViewGroup;  
  12. import android.widget.Scroller;  
  13. /** 
  14.  * 仿Launcher中的WorkSapce,可以左右滑动切换屏幕的类,支持显示系统背景和滑动 
  15.  * @author Yao.GUET 
  16.  * blog: http://blog.csdn.net/Yao_GUET 
  17.  * date: 2011-05-04 
  18.  */  
  19. public class ScrollLayout extends ViewGroup {  
  20.     private static final String TAG = "ScrollLayout";  
  21.     private Scroller mScroller;  
  22.     private VelocityTracker mVelocityTracker;  
  23.       
  24.     private int mCurScreen;  
  25.     private int mDefaultScreen = 0;  
  26.       
  27.     private static final int TOUCH_STATE_REST = 0;  
  28.     private static final int TOUCH_STATE_SCROLLING = 1;  
  29.       
  30.     private static final int SNAP_VELOCITY = 600;  
  31.       
  32.     private int mTouchState = TOUCH_STATE_REST;  
  33.     private int mTouchSlop;  
  34.     private float mLastMotionX;  
  35.     private float mLastMotionY;  
  36.       
  37.     private WallpaperManager mWallpaperManager;  
  38.     private int mScrollX;  
  39.     public ScrollLayout(Context context, AttributeSet attrs) {  
  40.         this(context, attrs, 0);  
  41.         // TODO Auto-generated constructor stub  
  42.     }  
  43.     public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {  
  44.         super(context, attrs, defStyle);  
  45.         // TODO Auto-generated constructor stub  
  46.           
  47.         mWallpaperManager = WallpaperManager.getInstance(context);  
  48.         mScroller = new Scroller(context);  
  49.           
  50.         mCurScreen = mDefaultScreen;  
  51.         mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();  
  52.     }  
  53.     @Override  
  54.     protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  55.         // TODO Auto-generated method stub  
  56.         Log.e(TAG, "onLayout");  
  57.         int childLeft = 0;  
  58.         final int childCount = getChildCount();  
  59.           
  60.         for (int i=0; i<childCount; i++) {  
  61.             final View childView = getChildAt(i);  
  62.             if (childView.getVisibility() != View.GONE) {  
  63.                 final int childWidth = childView.getMeasuredWidth();  
  64.                 childView.layout(childLeft, 0,   
  65.                         childLeft+childWidth, childView.getMeasuredHeight());  
  66.                 childLeft += childWidth;  
  67.             }  
  68.         }  
  69.         updateWallpaperOffset();  
  70.     }  
  71.   
  72.     @Override    
  73.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     
  74.         Log.e(TAG, "onMeasure");  
  75.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);     
  76.     
  77.         final int width = MeasureSpec.getSize(widthMeasureSpec);     
  78.         final int widthMode = MeasureSpec.getMode(widthMeasureSpec);     
  79.         if (widthMode != MeasureSpec.EXACTLY) {     
  80.             throw new IllegalStateException("ScrollLayout only canmCurScreen run at EXACTLY mode!");   
  81.         }     
  82.     
  83.         final int heightMode = MeasureSpec.getMode(heightMeasureSpec);     
  84.         if (heightMode != MeasureSpec.EXACTLY) {     
  85.             throw new IllegalStateException("ScrollLayout only can run at EXACTLY mode!");  
  86.         }     
  87.     
  88.         // The children are given the same width and height as the scrollLayout     
  89.         final int count = getChildCount();     
  90.         for (int i = 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.     public void snapToDestination() {  
  102.         final int screenWidth = getWidth();  
  103.         final int destScreen = (getScrollX()+ screenWidth/2)/screenWidth;  
  104.         snapToScreen(destScreen);  
  105.     }  
  106.       
  107.     public void snapToScreen(int whichScreen) {  
  108.         // get the valid layout page  
  109.         whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));  
  110.         if (getScrollX() != (whichScreen*getWidth())) {  
  111.               
  112.             final int delta = 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.     public void setToScreen(int whichScreen) {  
  121.         whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));  
  122.         mCurScreen = whichScreen;  
  123.         scrollTo(whichScreen*getWidth(), 0);  
  124.     }  
  125.       
  126.     public int getCurScreen() {  
  127.         return mCurScreen;  
  128.     }  
  129.       
  130.     @Override  
  131.     public void computeScroll() {  
  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.     public boolean onTouchEvent(MotionEvent event) {  
  143.         // TODO Auto-generated method stub  
  144.           
  145.         if (mVelocityTracker == null) {  
  146.             mVelocityTracker = VelocityTracker.obtain();  
  147.         }  
  148.         mVelocityTracker.addMovement(event);  
  149.           
  150.         final int action = event.getAction();  
  151.         final float x = event.getX();  
  152.         final float y = event.getY();  
  153.           
  154.         switch (action) {  
  155.         case MotionEvent.ACTION_DOWN:  
  156.             Log.e(TAG, "event down!");  
  157.             if (!mScroller.isFinished()){  
  158.                 mScroller.abortAnimation();  
  159.             }  
  160.             mLastMotionX = x;  
  161.             break;  
  162.               
  163.         case MotionEvent.ACTION_MOVE:  
  164.             int deltaX = (int)(mLastMotionX - x);  
  165.             mLastMotionX = x;  
  166.             scrollBy(deltaX, 0);  
  167.             updateWallpaperOffset();  
  168.             break;  
  169.               
  170.         case MotionEvent.ACTION_UP:  
  171.             Log.e(TAG, "event : up");     
  172.             // if (mTouchState == TOUCH_STATE_SCROLLING) {     
  173.             final VelocityTracker velocityTracker = mVelocityTracker;     
  174.             velocityTracker.computeCurrentVelocity(1000);     
  175.             int velocityX = (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.             } else if (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.         case MotionEvent.ACTION_CANCEL:  
  198.             mTouchState = TOUCH_STATE_REST;  
  199.             break;  
  200.         }  
  201.           
  202.         return true;  
  203.     }  
  204.     @Override  
  205.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  206.         // TODO Auto-generated method stub  
  207.         Log.e(TAG, "onInterceptTouchEvent-slop:"+mTouchSlop);  
  208.           
  209.         final int action = ev.getAction();  
  210.         if ((action == MotionEvent.ACTION_MOVE) &&   
  211.                 (mTouchState != TOUCH_STATE_REST)) {  
  212.             return true;  
  213.         }  
  214.           
  215.         final float x = ev.getX();  
  216.         final float y = ev.getY();  
  217.           
  218.         switch (action) {  
  219.         case MotionEvent.ACTION_MOVE:  
  220.             final int xDiff = (int)Math.abs(mLastMotionX-x);  
  221.             if (xDiff>mTouchSlop) {  
  222.                 mTouchState = TOUCH_STATE_SCROLLING;  
  223.                   
  224.             }  
  225.             break;  
  226.               
  227.         case MotionEvent.ACTION_DOWN:  
  228.             mLastMotionX = x;  
  229.             mLastMotionY = y;  
  230.             mTouchState = mScroller.isFinished()? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;  
  231.             break;  
  232.               
  233.         case MotionEvent.ACTION_CANCEL:  
  234.         case MotionEvent.ACTION_UP:  
  235.             mTouchState = TOUCH_STATE_REST;  
  236.             break;  
  237.         }  
  238.           
  239.         return mTouchState != TOUCH_STATE_REST;  
  240.     }  
  241.       
  242.       
  243.     private void updateWallpaperOffset() {  
  244.         int scrollRange = 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. }  


测试代码WallPaperTest.java:
[java] view plaincopyprint?
  1. package com.yao_guet;  
  2. import android.app.Activity;  
  3. import android.app.WallpaperManager;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.KeyEvent;  
  7. import android.view.Window;  
  8. import android.view.WindowManager;  
  9. public class WallPaperTest extends Activity {  
  10.     @Override  
  11.     protected void onCreate(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.     protected void onDestroy() {  
  20.         // TODO Auto-generated method stub  
  21.         super.onDestroy();  
  22.     }  
  23.     @Override  
  24.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  25.         // TODO Auto-generated method stub  
  26.         return super.onKeyDown(keyCode, event);  
  27.     }  
  28. }  


layout布局文件wallpaper_test.xml:
[html] view plaincopyprint?
  1. <?xml version="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>  


然后记得修改AndroidManifest.xml文件。。。

        <activity android:name=".WallPaperTest"

                  android:label="@string/app_name"

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

0 0