Android: фон на SurfaceView? - PullRequest
       15

Android: фон на SurfaceView?

0 голосов
/ 05 января 2012

У меня есть вид поверхности, и всякий раз, когда я касаюсь экрана, изображение появляется в том месте, где я касаюсь.Это здорово, но я не могу понять, как поместить фон в SurfaceView.Я попытался с помощью OnDraw нарисовать фон сразу (без необходимости прикасаться к нему), и это работает только в некоторых случаях.Он принудительно закрывается большую часть времени.

Кто-нибудь захочет взглянуть на мой код и посмотреть, возможно ли получить фоновое изображение на виде поверхности?Заранее спасибо.

Класс MyView расширяет SurfaceView и реализует SurfaceHolder.Callback {private Thready _thread;private ArrayList _graphicsz = new ArrayList ();приватный GraphicObject _currentGraphic = null;

public MyView(Context context) {
    super(context);
    getHolder().addCallback(this);
    _thread = new Thready(getHolder(), this);
    setFocusable(true);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    synchronized (_thread.getSurfaceHolder()) {
        GraphicObject graphic = null;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.cat1small));
            graphic.getCoordinates().setX((int) event.getX() - graphic.getGraphic().getWidth() / 2);
            graphic.getCoordinates().setY((int) event.getY() - graphic.getGraphic().getHeight() / 2);
            _currentGraphic = graphic;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            _currentGraphic.getCoordinates().setX((int) event.getX() - _currentGraphic.getGraphic().getWidth() / 2);
            _currentGraphic.getCoordinates().setY((int) event.getY() - _currentGraphic.getGraphic().getHeight() / 2);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            _graphicsz.add(_currentGraphic);
            _currentGraphic = null;
        }

        return true;
    }
}

@Override
public void onDraw(Canvas canvas) {


    canvas.drawColor(Color.BLACK);
    Bitmap bitmap1;
    GraphicObject.Coordinates coords1;
    for (GraphicObject graphic : _graphicsz) {
        bitmap1 = graphic.getGraphic();
        coords1 = graphic.getCoordinates();
        canvas.drawBitmap(bitmap1, coords1.getX(), coords1.getY(), null);
    }
    // draw current graphic at last...
    if (_currentGraphic != null) {
        bitmap1 = _currentGraphic.getGraphic();
        coords1 = _currentGraphic.getCoordinates();
        canvas.drawBitmap(bitmap1, coords1.getX(), coords1.getY(), null);
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub
}

public void surfaceCreated(SurfaceHolder holder) {
    _thread.setRunning(true);
    _thread.start();
}




public void surfaceDestroyed(SurfaceHolder holder) {
    // simply copied from sample application LunarLander:
    // we have to tell thread to shut down & wait for it to finish, or else
    // it might touch the Surface after we return and explode
    boolean retry = true;
    _thread.setRunning(false);
    while (retry) {
        try {
            _thread.join();
            retry = false;
        } catch (InterruptedException e) {
            // we will try it again and again...
        }
    }
}

}

класс Thready extends Thread {private SurfaceHolder _surfaceHolder;частный MyView _panel;private булево _run = false;

public Thready(SurfaceHolder surfaceHolder, MyView panel) {
    _surfaceHolder = surfaceHolder;
    _panel = panel;
}

public void setRunning(boolean run) {
    _run = run;
}

public SurfaceHolder getSurfaceHolder() {
    return _surfaceHolder;
}

@Override
public void run() {
    Canvas c;
    while (_run) {
        c = null;
        try {
            c = _surfaceHolder.lockCanvas(null);
            synchronized (_surfaceHolder) {
                _panel.onDraw(c);
            }
        } finally {
            // do this in a finally so that if an exception is thrown
            // during the above, we don't leave the Surface in an
            // inconsistent state
            if (c != null) {
                _surfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}

}

class GraphicObject {/ ** * Содержит координаты графического объекта.* / public class Coordinates {private int _x = 100;private int _y = 0;

    public int getX() {
        return _x + _bitmap.getWidth() / 2;
    }

    public void setX(int value) {
        _x = value - _bitmap.getWidth() / 2;
    }

    public int getY() {
        return _y + _bitmap.getHeight() / 2;
    }

    public void setY(int value) {
        _y = value - _bitmap.getHeight() / 2;
    }

    public String toString() {
        return "Coordinates: (" + _x + "/" + _y + ")";
    }
}

private Bitmap _bitmap;
private Coordinates _coordinates;

public GraphicObject(Bitmap bitmap) {
    _bitmap = bitmap;
    _coordinates = new Coordinates();
}

public Bitmap getGraphic() {
    return _bitmap;
}

public Coordinates getCoordinates() {
    return _coordinates;
}

}

1 Ответ

0 голосов
/ 25 апреля 2012

Ну, что меня прежде всего поражает (и что может вызвать много проблем):

вы слишком часто создаете новое растровое изображение

Каждый раз, когда вы получаете сенсорное событие, вы загружаете свои растровые изображения ... и вы можете получать от 80 до 100 ударов в секунду!

Итак, для начала определите глобальные растровые изображения (частное растровое изображение bmp1; и т. Д.), Загрузите их в другое место и ТОГДА используйте их в событии касания.

Кроме того, удалите загрузку / создание bmp из onDraw и переместите его в другое место.

После того, как вы это сделаете, посмотрите, что произойдет; у вас могло бы быть больше проблем (ДЕЙСТВИТЕЛЬНО быстрая проверка вашего кода казалась хорошей), но не поможет создание и загрузка растровых изображений 80 раз в секунду :)

...