Android的动态壁纸视差滚动效果 [英] android live wallpapers parallax-scrolling effect

查看:744
本文介绍了Android的动态壁纸视差滚动效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们滚动,主屏幕的前景(图标,窗口小部件等),移动到左边或右边的全屏幕宽度,但背景图像(或动态壁纸)仅移动由宽的一小部分。 我的问题是如何让这种效果。 到现在已经做到了这一点。

when we scroll, the foreground of the home screen (icons, widgets, etc.) moves to the left or right by the full screen width, but the background image (or live wallpaper) only moves by a fraction of that width. My question is how get this effect. till now have done this.

    SurfaceHolder holder = getSurfaceHolder();
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            if (canvas != null) {

                drawCircles(canvas);
            }
        } finally {
            if (canvas != null)
                holder.unlockCanvasAndPost(canvas);
        }

绘制函数是

{
    private void draw(Canvas canvas) {
        Paint paint = new Paint();
        DisplayMetrics metdisplayMatrics = new DisplayMetrics();
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        display.getMetrics(metdisplayMatrics);

            canvas.save();
            canvas.drawColor(0xff000000);
            mRecscreenRectangleFrame = new Rect(0, 0,  (int) (metdisplayMatrics.widthPixels*2.0), metdisplayMatrics.heightPixels);
            photo1= BitmapFactory.decodeResource(getResources(), R.drawable.img1);
            canvas.drawBitmap(photo1, null,mRecscreenRectangleFrame, paint);
            photo1.recycle();
            System.gc();
}               

现在如何把动态壁纸视差滚动效果。

Now how to put live wallpapers parallax-scrolling effect.

    @Override
    public void onOffsetsChanged(float xOffset, float yOffset,
            float xOffsetStep, float yOffsetStep, int xPixelOffset,
            int yPixelOffset) {

        super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
                xPixelOffset, yPixelOffset);

        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        View view=new View(getBaseContext());

        myWallpaperManager.setWallpaperOffsets(view.getWindowToken(),xOffset, 0f);

    }

还没有成型............

Not working yet.................

推荐答案

呼叫 WallpaperManager.setWallpaperOffsets 指示壁纸滚动。

<一个href="http://developer.android.com/reference/android/app/WallpaperManager.html#setWallpaperOffsets%28android.os.IBinder,%20float,%20float%29"相对=nofollow称号=文档>文档

因此​​,这应该居中壁纸:

So this should center the wallpaper:

WallpaperManager.setWallpaperOffsets(getWindowToken(), 0.5f, 0f);

这应该是滚动到一边:

WallpaperManager.setWallpaperOffsets(getWindowToken(), 0f, 0f);

这应该是滚动到另一侧:

This should scroll it to the other side:

WallpaperManager.setWallpaperOffsets(getWindowToken(), 1f, 0f);

如果你要做到这一点,你应该确保你知道壁纸其实是可以滚动,或用户已经要求您启用滚动。许多设备都配置了壁纸是大小相同的画面,并且不滚动。

If you're going to do this, you ought to ensure that you know that the wallpaper can actually be scrolled, or that the user has asked you to enable scrolling. Many devices are configured with wallpaper that is the same size as the screen and does not scroll.

这篇关于Android的动态壁纸视差滚动效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆