动态壁纸上的多个画布(一个移动,一个稳定)。怎么样?

时间:2018-02-10 19:54:30

标签: android canvas bitmap live-wallpaper wallpaper

如何在动态壁纸上绘制多幅画布?

我想要一张带有背景图片(位图)的动态壁纸,并且在图片上会有一个对象,它会跟随您的手指。但我不能画多个画布。

我已经通过了Google,但我没有找到任何有用的帮助。

有没有办法产生这个?

这是我的(不工作)代码:

package i.need.some.help;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class NewMyWallpaperService extends WallpaperService {

    @Override
    public Engine onCreateEngine() {
        return new CubeEngine();
    }

    class CubeEngine extends Engine {
        private final Paint mPaint = new Paint();
        private final Paint myPaint = new Paint();
        private float mTouchX = 0;
        private float mTouchY = 0;

        Bitmap movingObject = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        final SurfaceHolder holder = getSurfaceHolder();
        int height = Resources.getSystem().getDisplayMetrics().heightPixels;
        int width = Resources.getSystem().getDisplayMetrics().widthPixels;
        Bitmap backroundIMG = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round);

        CubeEngine() {
            setBackground();
        }

        @Override
        public void onCreate (SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            // Touch event handle alert
            setTouchEventsEnabled(true);
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
        }

        @Override
        public void onTouchEvent (MotionEvent event) {
            super.onTouchEvent(event);

            if (event.getAction() == MotionEvent.ACTION_UP) {
                mTouchX = event.getX();
                mTouchY = event.getY();
            } else {
                mTouchX = event.getX();
                mTouchY = event.getY();
            }
            drawFrame();
        }

        void drawFrame() {
            Canvas c = null;
            try {
                c = holder.lockCanvas();
                if (c !=null) {
                    c.save();
                    c.drawBitmap(movingObject, mTouchX-(movingObject.getWidth()/2), mTouchY-(movingObject.getHeight()/2), mPaint);
                    c.restore();
                }
            } finally {
                if (c != null) {
                    holder.unlockCanvasAndPost(c);
                }
            }
        }
        final void setBackground() {
            Canvas ca = null;
            try {
                ca = holder.lockCanvas();
                if (ca !=null) {
                    ca.save();
                    ca.drawColor(0xffa8a8a8);
                    ca.drawBitmap(backroundIMG, height/2, width/2, myPaint);
                    ca.restore();
                }
            } finally {
                if (ca != null) {
                    holder.unlockCanvasAndPost(ca);
                }
            }
        }
    }
}

0 个答案:

没有答案