0

我正在尝试制作一个简单的动态壁纸,当用户触摸他的屏幕时,会在该位置绘制一个位图。我面临 2 个问题,如果我在 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);
        }
    }    
}
4

1 回答 1

0

你可以试试这个示例代码

这里 imageURL 是图片位置的 http 链接

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

如果您的图像在手机内存中。

用这个

image.setImageResource(R.drawable.test2);
于 2013-04-14T12:00:19.483 回答