livewallpaper Double Tap开启活动

时间:2015-12-13 07:55:55

标签: android live-wallpaper

我是android开发的新手。 我想在我的壁纸中实现双击事件,这样当我双击壁纸时,它会打开应用程序。

这是我绘制壁纸图片的代码:

public class AbcService extends WallpaperService
{
    public void onCreate()
    {
        super.onCreate();
    }

    public void onDestroy()
    {
        super.onDestroy();
    }

    public Engine onCreateEngine()
    {
        return new wallpaperEngine();
    }

    class wallpaperEngine extends Engine
    {
        public Bitmap image1;

        wallpaperEngine()
        {
            image1 = BitmapFactory.decodeResource(getResources(), R.drawable.img_3);
        }

        public void onCreate(SurfaceHolder surfaceHolder)
        {
            super.onCreate(surfaceHolder);
        }

        public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels)
        {
            drawFrame();
        }

        void drawFrame()
        {

        final SurfaceHolder holder = getSurfaceHolder();

        Canvas c = null;
        try
        {
            c = holder.lockCanvas();
            if (c != null)
            {
                c.drawBitmap(image1, 0, 0, null);

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

现在该怎么办? 我在正确的轨道上吗?

提前感谢你..

1 个答案:

答案 0 :(得分:0)

对于动态壁纸中的双击操作,您可以将GestureDetector.SimpleOnGestureListener子类化为:

class wallpaperEngine extends Engine {
    ...
    ...
    private GestureDetector gestureListener;

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDoubleTap(MotionEvent e) {

            //start intent/application here

            return super.onDoubleTap(e);
        }
    }

然后创建该类的实例,f.e。在你的onCreate(SurfaceHolder surfaceHolder)方法中启用动态壁纸中的触摸事件:

   public void onCreate(SurfaceHolder surfaceHolder) {
        super.onCreate(surfaceHolder);

        gestureListener = new GestureDetector(getApplicationContext(), new GestureListener());
        setTouchEventsEnabled(true);

    }

然后@Override onTouchEvent:

    @Override
    public void onTouchEvent(MotionEvent event) {

       gestureListener.onTouchEvent(event);

    }

这是我能想到的最简单的方法。