Android Canvas错误 - 错误排队缓冲区到SurfaceTexture

时间:2015-01-28 10:30:03

标签: android canvas draw live-wallpaper forceclose

有没有人知道会导致以下错误的原因?

queueBuffer: error queuing buffer to SurfaceTexture, -32

我在我的应用中使用SurfaceTexture。当我尝试设置为时出现上述错误 活壁纸

以下是代码:

public class ClockWallpaperService extends WallpaperService {

    private WallpaperEngine myEngine;

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

    @Override
    public Engine onCreateEngine() {

        System.out.println("Service: onCreateEngine");
        this.myEngine = new WallpaperEngine();
        return myEngine;
    }

    public void onDestroy() {
        this.myEngine = null;
        super.onDestroy();
    }

    private class WallpaperEngine extends Engine implements OnGestureListener,
            OnSharedPreferenceChangeListener {

        public Bitmap image1, backgroundImage;

        private ArrayList<Leaf> leafList;
        private Bitmap bitmap1;
        private Bitmap bitmap2;
        private Bitmap bitmap3;

        private Bitmap currentBackgroundBitmap;
        private Paint paint;
        private int count;
        private int heightOfCanvas;
        private int widthOfCanvas;
        private float touchX;
        private float touchY;
        private int interval;
        private int amount;
        private boolean fallingDown;
        private float bgX = 0;

        private String colorFlag;
        private String backgroundFlag;
        private Random rand;
        private GestureDetector detector;

        private static final int DRAW_MSG = 0;
        private static final int MAX_SIZE = 101;

        private Handler mHandler = new Handler() {

            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                case DRAW_MSG:
                    drawPaper();
                    break;
                }

            }

        };

        /** hands colors for hour, min, sec */
        private int[] colors = { 0xFFFF0000, 0xFF0000FF, 0xFFA2BC13 };
        // private int bgColor;
        private int width;
        private int height;
        private boolean visible = true;
        private boolean displayHandSec;
        private AnalogClock clock;

        private SharedPreferences prefs;

        WallpaperEngine() {
            // get the fish and background image references

            backgroundImage = BitmapFactory.decodeResource(getResources(),
                    R.drawable.bg1);

            SharedPreferences sp = getSharedPreferences("back_position",
                    Activity.MODE_PRIVATE);
            int position = sp.getInt("back_position", 1);

            Global.backgroundDial = BitmapFactory.decodeResource(
                    getResources(), Global.BackgroundId[position]);

            prefs = PreferenceManager
                    .getDefaultSharedPreferences(ClockWallpaperService.this);
            prefs.registerOnSharedPreferenceChangeListener(this);
            displayHandSec = prefs.getBoolean(
                    SettingsActivity.DISPLAY_HAND_SEC_KEY, true);
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(5);
            // bgColor = Color.parseColor("#C0C0C0");
            clock = new AnalogClock(getApplicationContext());

        }

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            // TODO Auto-generated method stub
            super.onCreate(surfaceHolder);
            System.out.println("Engine: onCreate");

            this.leafList = new ArrayList<Leaf>();
            this.bitmap1 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.flower1);
            this.bitmap2 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.flower2);
            this.bitmap3 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.flower3);
            this.paint = new Paint();
            this.paint.setAntiAlias(true);
            this.count = -1;
            this.rand = new Random();
            this.detector = new GestureDetector(this);
            this.touchX = -1.0f;
            this.touchY = -1.0f;

            SharedPreferences pref = PreferenceManager
                    .getDefaultSharedPreferences(ClockWallpaperService.this);
            pref.registerOnSharedPreferenceChangeListener(this);
            String speedStr = pref.getString("leaf_falling_speed", "20");
            String amountStr = pref.getString("leaf_number", "50");
            this.interval = Integer.parseInt(speedStr);
            this.amount = Integer.parseInt(amountStr);
            this.colorFlag = pref.getString("leaf_color", "0");
            this.backgroundFlag = pref.getString("paper_background", "0");
            String directionFlag = pref.getString("leaf_moving_direction", "0");
            if (directionFlag.equals("0")) {
                this.fallingDown = true;
            } else {
                this.fallingDown = false;
            }

            this.setTouchEventsEnabled(true);

        }

        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            System.out.println("Engine: onDestroy");
            this.mHandler.removeMessages(DRAW_MSG);

            PreferenceManager.getDefaultSharedPreferences(
                    ClockWallpaperService.this)
                    .unregisterOnSharedPreferenceChangeListener(this);

            super.onDestroy();
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
            // TODO Auto-generated method stub
            this.width = width;
            this.height = height;
            super.onSurfaceChanged(holder, format, width, height);
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            super.onSurfaceCreated(holder);
            System.out.println("Engine: onSurfaceCreate");

            Canvas canvas = holder.lockCanvas();
            this.heightOfCanvas = canvas.getHeight();
            this.widthOfCanvas = canvas.getWidth();
            System.out.println("Width = " + widthOfCanvas + ", Height = "
                    + heightOfCanvas);

            holder.unlockCanvasAndPost(canvas);

            this.mHandler.sendEmptyMessage(DRAW_MSG);
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            System.out.println("Engine: onSurfaceDestroyed");
            this.mHandler.removeMessages(DRAW_MSG);

            if (this.currentBackgroundBitmap != null) {
                this.currentBackgroundBitmap.recycle();
                this.currentBackgroundBitmap = null;
            }
            if (this.bitmap1 != null) {
                this.bitmap1.recycle();
                this.bitmap1 = null;
            }
            if (this.bitmap2 != null) {
                this.bitmap2.recycle();
                this.bitmap2 = null;
            }
            if (this.bitmap3 != null) {
                this.bitmap3.recycle();
                this.bitmap3 = null;
            }

            super.onSurfaceDestroyed(holder);
        }

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

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

            System.out.println("xPixelOffset: " + xPixelOffset
                    + ", yPixelOffset: " + yPixelOffset);

            this.bgX = xPixelOffset;

        }

        private void drawPaper() {
            count++;
            if (count >= 10000) {
                count = 0;
            }
            if (count % 10 == 0) {
                if (this.leafList.size() < MAX_SIZE) {
                    Leaf l = null;
                    Bitmap temp = bitmap1;
                    if (colorFlag.equals("0")) {
                        int index = rand.nextInt(3) + 1;
                        switch (index) {
                        case 1:
                            temp = bitmap1;
                            break;
                        case 2:
                            temp = bitmap2;
                            break;
                        case 3:
                            temp = bitmap3;
                            break;
                        default:
                            temp = bitmap1;
                            break;
                        }
                    } else if (colorFlag.equals("1")) {
                        temp = bitmap1;
                    } else if (colorFlag.equals("2")) {
                        temp = bitmap2;
                    } else if (colorFlag.equals("3")) {
                        temp = bitmap3;
                    }
                    l = new Leaf(temp, this.heightOfCanvas, this.widthOfCanvas);
                    this.leafList.add(l);
                }
            }

            SurfaceHolder holder = this.getSurfaceHolder();
            Canvas canvas = holder.lockCanvas();

            drawBackground(canvas);

            int size = Math.min(this.amount, this.leafList.size());
            for (int i = 0; i < size; i++) {
                Leaf l = this.leafList.get(i);
                if (l.isTouched()) {
                    l.handleTouched(touchX, touchY);
                } else {
                    l.handleFalling(this.fallingDown);
                }
                l.drawLeaf(canvas, paint);

            }
            holder.unlockCanvasAndPost(canvas);
            this.mHandler.sendEmptyMessageDelayed(DRAW_MSG, this.interval);

        }

        private void drawBackground(Canvas c) {

            c.drawBitmap(backgroundImage, 0, 0, null);

            clock.config(width / 2, height / 2, (int) (width * 0.6f),
                    new Date(), paint, colors, displayHandSec);
            clock.draw(c);

        }

        @Override
        public void onTouchEvent(MotionEvent event) {

            super.onTouchEvent(event);
            this.detector.onTouchEvent(event);
        }

        public boolean onDown(MotionEvent e) {

            touchX = e.getX();
            touchY = e.getY();
            int size = Math.min(this.amount, this.leafList.size());
            for (int i = 0; i < size; i++) {
                Leaf l = this.leafList.get(i);
                float centerX = l.getX() + l.getBitmap().getWidth() / 2.0f;
                float centerY = l.getY() + l.getBitmap().getHeight() / 2.0f;

                if (!l.isTouched()) {
                    if (Math.abs(centerX - touchX) <= 80
                            && Math.abs(centerY - touchY) <= 80
                            && centerX != touchX) {
                        l.setTouched(true);
                    }
                }
            }

            return true;
        }

        public void onShowPress(MotionEvent e) {

        }

        public boolean onSingleTapUp(MotionEvent e) {

            return false;
        }

        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {

            return false;
        }

        public void onLongPress(MotionEvent e) {

        }

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {

            return false;
        }

        public void onSharedPreferenceChanged(
                SharedPreferences sharedPreferences, String key) {

            if (key.equals("leaf_falling_speed")) {
                String speedStr = sharedPreferences.getString(key, "20");
                this.interval = Integer.parseInt(speedStr);

            } else if (key.equals("leaf_number")) {
                String amountStr = sharedPreferences.getString(key, "50");
                this.amount = Integer.parseInt(amountStr);

            } else if (key.equals("leaf_moving_direction")) {
                String directionFlag = sharedPreferences.getString(key, "0");
                if (directionFlag.equals("0")) {
                    this.fallingDown = true;
                } else {
                    this.fallingDown = false;
                }

            } else if (key.equals("leaf_color")) {
                this.colorFlag = sharedPreferences.getString(key, "0");
                this.leafList.removeAll(leafList);

            }
        }

    }

}

0 个答案:

没有答案