Android中使用系统桌面背景作为应用背景,支持拖动
在Android应用开发中,使用系统桌面背景作为应用的背景,需要把应用的背景设置为透明背景,然后设置窗口的属性为FLAG_SHOW_WALLPAPER即可显示背景。
修改AndroidManifest.xml文件里面activity属性:
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 7For 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
xStep | The X offset delta from one screen to the next one |
---|---|
yStep | The Y offset delta from one screen to the next one |
public void setWallpaperOffsets (IBinder windowToken, float xOffset, float yOffset)
Since: API Level 5Set 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
windowToken | The window who these offsets should be associated with, as returned by View.getWindowToken() . |
---|---|
xOffset | The offset along the X dimension, from 0 to 1. |
yOffset | The offset along the Y dimension, from 0 to 1.
|
package com.yao_guet;import android.app.WallpaperManager;import android.content.Context;import android.os.IBinder;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.VelocityTracker;import android.view.View;import android.view.ViewConfiguration;import android.view.ViewGroup;import android.widget.Scroller;/** * 仿Launcher中的WorkSapce,可以左右滑动切换屏幕的类,支持显示系统背景和滑动 * @author Yao.GUET * blog: http://blog.csdn.net/Yao_GUET * date: 2011-05-04 */public class ScrollLayout extends ViewGroup {private static final String TAG = "ScrollLayout";private Scroller mScroller;private VelocityTracker mVelocityTracker;private int mCurScreen;private int mDefaultScreen = 0;private static final int TOUCH_STATE_REST = 0;private static final int TOUCH_STATE_SCROLLING = 1;private static final int SNAP_VELOCITY = 600;private int mTouchState = TOUCH_STATE_REST;private int mTouchSlop;private float mLastMotionX;private float mLastMotionY;private WallpaperManager mWallpaperManager;private int mScrollX;public ScrollLayout(Context context, AttributeSet attrs) {this(context, attrs, 0);// TODO Auto-generated constructor stub}public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubmWallpaperManager = WallpaperManager.getInstance(context);mScroller = new Scroller(context);mCurScreen = mDefaultScreen;mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {// TODO Auto-generated method stubLog.e(TAG, "onLayout");int childLeft = 0;final int childCount = getChildCount();for (int i=0; i SNAP_VELOCITY && mCurScreen > 0) { // Fling enough to move left Log.e(TAG, "snap left"); snapToScreen(mCurScreen - 1); } else if (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;case MotionEvent.ACTION_CANCEL:mTouchState = TOUCH_STATE_REST;break;}return true;}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubLog.e(TAG, "onInterceptTouchEvent-slop:"+mTouchSlop);final int action = ev.getAction();if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {return true;}final float x = ev.getX();final float y = ev.getY();switch (action) {case MotionEvent.ACTION_MOVE:final int xDiff = (int)Math.abs(mLastMotionX-x);if (xDiff>mTouchSlop) {mTouchState = TOUCH_STATE_SCROLLING;}break;case MotionEvent.ACTION_DOWN:mLastMotionX = x;mLastMotionY = y;mTouchState = mScroller.isFinished()? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;break;case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_UP:mTouchState = TOUCH_STATE_REST;break;}return mTouchState != TOUCH_STATE_REST;}private void updateWallpaperOffset() { int scrollRange = 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); } }}
测试代码WallPaperTest.java:
package com.yao_guet;import android.app.Activity;import android.app.WallpaperManager;import android.content.Context;import android.os.Bundle;import android.view.KeyEvent;import android.view.Window;import android.view.WindowManager;public class WallPaperTest extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.wallpaper_test);getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubreturn super.onKeyDown(keyCode, event);}}
layout布局文件wallpaper_test.xml:
<?xml version="1.0" encoding="utf-8"?>
然后记得修改AndroidManifest.xml文件。。。
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent">
更多相关文章
- Android(安卓)studio3.6.3NDK环境配置
- Android对第三方类库运行时加载
- Cocos2d-x for Android(2)--编译和新建工程
- android 按键映射***.kl文件中的WAKE和WAKE_DROPPED的定义
- Android源码分析之Framework的MediaPlayer
- Android(安卓)SDK Manager:failed to install
- Android使用外部字体
- Android(安卓)Studio集成OpenCV
- android 窗口背景透明方法