gpt4 book ai didi

Android:在 Canvas 上反复绘制相同位图时出现问题

转载 作者:行者123 更新时间:2023-11-29 22:28:05 25 4
gpt4 key购买 nike

所以我有一个从资源文件(PNG 图像)加载的位图:

Bitmap map = BitmapFactory.decodeResource(getResources(), R.drawable.wave);

如果我只使用 canvas.drawBitmap(...); 绘制此位图一次,则没有问题。但是,如果我多次绘制相同的位图,那么图片会一直来回闪烁,不像以前那样稳定。

我怀疑我不能多次使用相同的位图,所以每次我想绘制相同的图片时,我都尝试将图像加载到新的位图中,但没有帮助,行为仍然存在。

程序很复杂,但基本上,我想画一个海浪。我有一个小波浪的形象。使波浪的效果从屏幕的左边缘移动到右边缘。我跟踪位图左边缘的位置。

// The ocean.
private ArrayList<Wave> waves;

// Draw the waves and update their positions.
for (int i = 0; i < this.waves.size(); i++)
{
Wave wave = this.waves.get(i);

// Go through each of the sub-waves of this current wave.
for (int j = 0; j < wave.getSubWaveEdges().size(); j++)
{
// Get the sub wave.
final float subWaveEdge = wave.getSubWaveEdges().get(j);

canvas.drawBitmap( wave.getSubWave(j), subWaveEdge, 40, brush);

wave.setSubWaveEdge(j, subWaveEdge + (float) 0.5);
}

// Update this current wave.
wave.update();

// If the wave has passed the left edge of the screen then add a new sub-wave.
if (wave.getFarthestEdge() >= 0)
wave.addSubWaveEdges(wave.getFarthestEdge() - this.getWidth());
}

如果位图的左边缘在屏幕内,那么我从同一个图像文件创建一个新位图并绘制。这是 Wave 类:

    private class Wave
{
private Bitmap wave;
private float farthestEdge;
private ArrayList<Float> subWaveEdges;
private ArrayList<Bitmap> subWaves;

public Wave(Bitmap wave)
{
this.wave = wave;

this.farthestEdge = 0;

this.subWaveEdges = new ArrayList<Float>();

this.subWaves = new ArrayList<Bitmap>();
}

public Bitmap getWave ()
{ return this.wave; }

public void setWave (Bitmap wave)
{ this.wave = wave; }

public float getFarthestEdge ()
{ return this.farthestEdge; }

public void setFarthestEdge (final float furthestEdge)
{ this.farthestEdge = furthestEdge; }

public ArrayList<Float> getSubWaveEdges ()
{ return subWaveEdges; }

public void setSubWaveEdge (final int index, final float value)
{
this.subWaveEdges.remove(index);

this.subWaveEdges.add(value);
}

public void addSubWaveEdges (final float edge)
{
this.subWaveEdges.add(edge);

Bitmap newSubWave = BitmapFactory.decodeResource(getResources(), R.drawable.wave);

newSubWave = Bitmap.createScaledBitmap(newSubWave, MasterView.this.getWidth(), newSubWave.getHeight(), true);

this.subWaves.add(newSubWave);
}

public Bitmap getSubWave(final int index)
{ return this.subWaves.get(index); }

public void update ()
{

// Check to see if there is any sub-wave going outside of the screen.
// If there is then remove that wave.
for (int index = 0; index < this.subWaveEdges.size(); index++)
if (this.subWaveEdges.get(index) > MasterView.this.getWidth())
{
this.subWaveEdges.remove(index);

this.subWaves.remove(index);
}

// Set the farthest edge to the other side of the screen.
this.farthestEdge = MasterView.this.getWidth();

// Get the farthest edge of the wave.
for (int index = 0; index < this.subWaveEdges.size(); index++)
if (this.subWaveEdges.get(index) < this.farthestEdge)
this.farthestEdge = this.subWaveEdges.get(index);
}
}

我的另一个怀疑是,当我从同一个资源文件创建两个位图时,图像的像素被分成两个位图,这意味着每个位图只获得部分像素,而不是全部。我怀疑这是因为当绘制位图时,它们重叠的部分被稳定地绘制,没有闪烁。

有人偶然发现这个问题并知道如何解决吗?

谢谢,

最佳答案

Viktor Lannér,感谢您的帮助,但我认为这不是问题所在。我知道很难阅读我的代码,因为它只是大程序的一小部分。

但是,我发现了问题:这个在我原来的问题中没有提到,但是为了模拟两个波浪相继移动,我必须在第一个波浪进入屏幕后立即绘制下一个波浪。但是,每个波浪都长于屏幕的宽度。因此,如果您明白我的意思,我必须从屏幕“外部”绘制下一波。这意味着下一波是从屏幕外的负 x 坐标绘制的:

    // If the wave has passed the left edge of the screen then add a new sub-wave.
if (wave.getFarthestEdge() >= 0)
wave.addSubWaveEdges(wave.getFarthestEdge() - this.getWidth());

而且我发现它不喜欢这样。这就是导致来回闪烁的原因。

为了解决这个问题,我没有从屏幕外绘制下一波,而是使用了这个方法:

     canvas.drawBitmap (Bitmap bitmap, Rect source, Rect destination, Paint paint)

此方法允许您在要绘制到屏幕的位图上指定一个矩形区域,以及在屏幕上绘制该部分位图的矩形区域。我用这个方法来画下一波。随着下一波进入屏幕,我适本地更改“源”和“目标”以绘制部分位图。

关于Android:在 Canvas 上反复绘制相同位图时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5277897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com