一、在Android应用开发中,使用系统桌面背景作为应用的背景,需要把应用的背景设置为透明背景,然后设置窗口的属性为FLAG_SHOW_WALLPAPER即可显示背景。
1>.修改AndroidManifest.xml文件里面activity属性:
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 packagecom.yao_guet; importandroid.app.WallpaperManager; importandroid.content.Context; importandroid.os.IBinder; importandroid.util.AttributeSet; importandroid.util.Log; importandroid.view.MotionEvent; importandroid.view.VelocityTracker; importandroid.view.View; importandroid.view.ViewConfiguration; importandroid.view.ViewGroup; importandroid.widget.Scroller; /** * 仿Launcher中的WorkSapce,可以左右滑动切换屏幕的类,支持显示系统背景和滑动 * @author Hekang * blog: 6600s.diandian.com * date: 2012-04-22 */ publicclassScrollLayoutextendsViewGroup { privatestaticfinalString TAG ="ScrollLayout"; privateScroller mScroller; privateVelocityTracker mVelocityTracker; privateintmCurScreen; privateintmDefaultScreen =0; privatestaticfinalintTOUCH_STATE_REST =0; privatestaticfinalintTOUCH_STATE_SCROLLING =1; privatestaticfinalintSNAP_VELOCITY =600; privateintmTouchState = TOUCH_STATE_REST; privateintmTouchSlop; privatefloatmLastMotionX; privatefloatmLastMotionY; privateWallpaperManager mWallpaperManager; privateintmScrollX; publicScrollLayout(Context context, AttributeSet attrs) { this(context, attrs,0); // TODO Auto-generated constructor stub } publicScrollLayout(Context context, AttributeSet attrs,intdefStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub mWallpaperManager = WallpaperManager.getInstance(context); mScroller =newScroller(context); mCurScreen = mDefaultScreen; mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); } @Override protectedvoidonLayout(booleanchanged,intl,intt,intr,intb) { // TODO Auto-generated method stub Log.e(TAG,"onLayout"); intchildLeft =0; finalintchildCount = getChildCount(); for(inti=0; i finalView childView = getChildAt(i); if(childView.getVisibility() != View.GONE) { finalintchildWidth = childView.getMeasuredWidth(); childView.layout(childLeft,0, childLeft+childWidth, childView.getMeasuredHeight()); childLeft += childWidth; } } updateWallpaperOffset(); } @Override protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec) { Log.e(TAG,"onMeasure"); super.onMeasure(widthMeasureSpec, heightMeasureSpec); finalintwidth = MeasureSpec.getSize(widthMeasureSpec); finalintwidthMode = MeasureSpec.getMode(widthMeasureSpec); if(widthMode != MeasureSpec.EXACTLY) { thrownewIllegalStateException("ScrollLayout only canmCurScreen run at EXACTLY mode!"); } finalintheightMode = MeasureSpec.getMode(heightMeasureSpec); if(heightMode != MeasureSpec.EXACTLY) { thrownewIllegalStateException("ScrollLayout only can run at EXACTLY mode!"); } // The children are given the same width and height as the scrollLayout finalintcount = getChildCount(); for(inti =0; i < count; i++) { getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); } // Log.e(TAG, "moving to screen "+mCurScreen); scrollTo(mCurScreen * width,0); } /** * According to the position of current layout * scroll to the destination page. */ publicvoidsnapToDestination() { finalintscreenWidth = getWidth(); finalintdestScreen = (getScrollX()+ screenWidth/2)/screenWidth; snapToScreen(destScreen); } publicvoidsnapToScreen(intwhichScreen) { // get the valid layout page whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1)); if(getScrollX() != (whichScreen*getWidth())) { finalintdelta = whichScreen*getWidth()-getScrollX(); mScroller.startScroll(getScrollX(),0, delta,0, Math.abs(delta)*2); mCurScreen = whichScreen; invalidate();// Redraw the layout } } publicvoidsetToScreen(intwhichScreen) { whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1)); mCurScreen = whichScreen; scrollTo(whichScreen*getWidth(),0); } publicintgetCurScreen() { returnmCurScreen; } @Override publicvoidcomputeScroll() { // TODO Auto-generated method stub mScrollX = mScroller.getCurrX(); if(mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); updateWallpaperOffset(); postInvalidate(); } } @Override publicbooleanonTouchEvent(MotionEvent event) { // TODO Auto-generated method stub if(mVelocityTracker ==null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); finalintaction = event.getAction(); finalfloatx = event.getX(); finalfloaty = event.getY(); switch(action) { caseMotionEvent.ACTION_DOWN: Log.e(TAG,"event down!"); if(!mScroller.isFinished()){ mScroller.abortAnimation(); } mLastMotionX = x; break; caseMotionEvent.ACTION_MOVE: intdeltaX = (int)(mLastMotionX - x); mLastMotionX = x; scrollBy(deltaX,0); updateWallpaperOffset(); break; caseMotionEvent.ACTION_UP: Log.e(TAG,"event : up"); // if (mTouchState == TOUCH_STATE_SCROLLING) { finalVelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000); intvelocityX = (int) velocityTracker.getXVelocity(); Log.e(TAG,"velocityX:"+velocityX); if(velocityX > SNAP_VELOCITY && mCurScreen >0) { // Fling enough to move left Log.e(TAG,"snap left"); snapToScreen(mCurScreen -1); }elseif(velocityX < -SNAP_VELOCITY && mCurScreen < getChildCount() -1) { // Fling enough to move right Log.e(TAG,"snap right"); snapToScreen(mCurScreen +1); }else{ snapToDestination(); } if(mVelocityTracker !=null) { mVelocityTracker.recycle(); mVelocityTracker =null; } // } mTouchState = TOUCH_STATE_REST; break; caseMotionEvent.ACTION_CANCEL: mTouchState = TOUCH_STATE_REST; break; } returntrue; } @Override publicbooleanonInterceptTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub Log.e(TAG,"onInterceptTouchEvent-slop:"+mTouchSlop); finalintaction = ev.getAction(); if((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) { returntrue; } finalfloatx = ev.getX(); finalfloaty = ev.getY(); switch(action) { caseMotionEvent.ACTION_MOVE: finalintxDiff = (int)Math.abs(mLastMotionX-x); if(xDiff>mTouchSlop) { mTouchState = TOUCH_STATE_SCROLLING; } break; caseMotionEvent.ACTION_DOWN: mLastMotionX = x; mLastMotionY = y; mTouchState = mScroller.isFinished()? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; break; caseMotionEvent.ACTION_CANCEL: caseMotionEvent.ACTION_UP: mTouchState = TOUCH_STATE_REST; break; } returnmTouchState != TOUCH_STATE_REST; } privatevoidupdateWallpaperOffset() { intscrollRange = getChildAt(getChildCount() -1).getRight() - getWidth(); IBinder token = getWindowToken(); if(token !=null) { mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() -1),0); mWallpaperManager.setWallpaperOffsets(getWindowToken(), Math.max(0.f, Math.min(getScrollX()/(float)scrollRange,1.f)),0); } } } 2>.测试代码WallPaperTest.java: packagecom.yao_guet; importandroid.app.Activity; importandroid.app.WallpaperManager; importandroid.content.Context; importandroid.os.Bundle; importandroid.view.KeyEvent; importandroid.view.Window; importandroid.view.WindowManager; publicclassWallPaperTestextendsActivity { @Override protectedvoidonCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.wallpaper_test); getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER); } @Override protectedvoidonDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override publicbooleanonKeyDown(intkeyCode, KeyEvent event) { // TODO Auto-generated method stub returnsuper.onKeyDown(keyCode, event); } } xmlversion="1.0"encoding="utf-8"?> <com.sf.test.ScrollLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/WallPaperTest" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is page 1"/> LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is page 2"/> LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is page 3"/> LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is page 4"/> LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is page 5"/> LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is page 6"/> LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is page 7"/> LinearLayout> com.sf.test.ScrollLayout> 4>.最后记得修改AndroidManifest.xml文件 android:label="@string/app_name" android:theme="@android:style/Theme.Translucent">
[java] view plain copy
3>.layout布局文件wallpaper_test.xml:
[html] view plain copy