在动态壁纸中绘制位图

时间:2013-04-14 11:54:03

标签: android live-wallpaper

我正在尝试制作一个简单的动态壁纸,当用户触摸他的屏幕时,会在该位置绘制位图。我面临着2个问题,如果我在Runnable中绘制它然后它会不断创建位图,如果我在外面绘制它然后它会不断闪烁。我注意到,如果我改变延迟时间为可运行闪烁也会改变,如果我完全删除了runnable,那么它正在绘制,但是位置变得很奇怪。

这是我的代码。 这一个连续闪烁。

package com.example.live_wallpaper_p1;

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;



public class DemoWallpaperService extends WallpaperService {

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

    private class DemoWallpaperEngine extends Engine{
        private boolean mVisible = false;
        private final Handler mHandler = new Handler();
        private Bitmap imm;
        private float xcor;
        private float ycor;
        BitmapFactory.Options option;       
        DemoWallpaperEngine()
        {
        imm = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon, option));
        }

        private final Runnable mUpdateDisplay = new Runnable() {
        @Override
        public void run() {
            draw();
        }};

        @Override
        public Bundle onCommand(String action, int x, int y, int z, Bundle extras, boolean resultRequested) {
            if (action.equals(WallpaperManager.COMMAND_TAP)) {
                xcor=x;
                ycor=y;
                final SurfaceHolder holder = getSurfaceHolder();

                Canvas c = null;
                try {
                    c = holder.lockCanvas();
                    if (c != null) {
                    c.drawBitmap(imm, xcor-(imm.getWidth()/2), ycor-(imm.getHeight()/2),null);
                        //drawCube(c);
                    }
                } finally {
                    if (c != null)
                        holder.unlockCanvasAndPost(c);
                }

            }
            return null;
        }



        private void draw() {
           SurfaceHolder holder = getSurfaceHolder();
           Canvas c = null;
           try {
              c = holder.lockCanvas();
              if (c != null) {
                  //do something
              }
           } finally {
              if (c != null)
                 holder.unlockCanvasAndPost(c);
           }
           mHandler.removeCallbacks(mUpdateDisplay); 
      if (mVisible) {
              mHandler.postDelayed(mUpdateDisplay, 10); 

           } 
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            mVisible = visible;
            if (visible) {
                draw();
            } else {
                mHandler.removeCallbacks(mUpdateDisplay);
            }
        }

         @Override
          public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            draw();
          }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            mVisible = false;
            mHandler.removeCallbacks(mUpdateDisplay);
        }

        @Override
        public void onDestroy() {
             super.onDestroy();
             mVisible = false;
             mHandler.removeCallbacks(mUpdateDisplay);
        }
    }    
}

1 个答案:

答案 0 :(得分:0)

您可以尝试此示例代码

此处imageURL是指向图像位置的http链接

 Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
    holder.itemImage.setImageBitmap(bitmap); 

如果您的图像在手机记忆库中。

使用此

image.setImageResource(R.drawable.test2);