我认为那是因为您的旧位图仍在您的绘图缓存中。因此,您首先需要将其从缓存中删除,然后将新图像放入缓存中。看看这个问题,它似乎是同一个话题:
删除绘图缓存
编辑:
所以,这是对我有用的代码。我使用按钮保存位图,然后将位图设置为图像视图:
private View rootView;
private ImageView bitmapView;
private Button switchButton;
public Bitmap capturedScreen;
public boolean bitmapNeeded = false;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// do the other stuff
rootView.setDrawingCacheEnabled(true); //enable Drawing cache on your view
switchButton.setOnClickListener(this);
}
...
@Override
public void onClick(View v) {
if (v == switchButton) { //when the button is clicked
captureScreen();
}
}
public void captureScreen() {
rootView.buildDrawingCache();
capturedScreen = Bitmap.createBitmap(rootView.getDrawingCache());
imageView.setImageBitmap(capturedScreen);
rootView.destroyDrawingCache();
}
....
//In the onDraw method of your View:
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(capturedScreen, 0, 0, paint);
}
它是这样工作的:每次用户单击按钮时,里面的所有内容rootView
都会保存为位图,然后绘制到imageView
. 如果需要,您当然可以从代码中的任何位置调用 captureScreen 方法。
我希望这个例子对你有所帮助。