我希望这个例子可以帮助: 首先使自己的视图,并让调用它DynamicView:
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DynamicView extends View {
int i = 0;
public DynamicView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
Random random = new Random();
paint.setColor(Color.argb(255, random.nextInt(255),
random.nextInt(255), random.nextInt(255)));
canvas.drawLine(random.nextInt(600), random.nextInt(600),
random.nextInt(600), random.nextInt(600), paint);
if (i < 1000) {
i++;
invalidate();
}
}
}
这种观点将无效1000时各画
后,现在这里是您的主要活动onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DynamicView myView = new DynamicView(this);
setContentView(myView);
}
as sim这样做。
在回答你的问题这是一个更好的代码,更复杂,但还是很容易理解 所有我们需要做的是汲取位图的线条,然后画上认为,位图,所以将位图不会改变,将所有以前绘制的线条,仍然借鉴它
现在这里是更新后的代码:
DynamicView
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
public class DynamicView extends View {
int i = 0;
Bitmap frame ;
Canvas frameDrawer;
Rect bounds;
Paint paint ;
Random random ;
int width , height;
public DynamicView(Context context ,int width ,int height) {
super(context);
this.width = width;
this.height = height;
frame = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
frameDrawer = new Canvas(frame);
bounds = new Rect(0 , 0, width,height);
//this initialization will make the frameDrawer draw on the frame bitmap
//always avoid allocating new objects in the draw method to optimize the performance
paint = new Paint();
random = new Random();
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.argb(255, random.nextInt(255),
random.nextInt(255), random.nextInt(255)));
//instead of drawing on the view canvas draw on the frame bitmap
frameDrawer.drawLine(random.nextInt(width), random.nextInt(height),
random.nextInt(width), random.nextInt(height), paint);
// and then draw the bitmap on the view canvas
canvas.drawBitmap(frame, null, bounds , null);
if (i < 1000) {
i++;
invalidate();
}
}
}
现在MainActivity中的新事物是,现在我们可以控制视图的绘制边界,但仍然不是视图边界需要所有的屏幕空间(如果要调整视图的边界,只需实现onLayout方法并输入您的值的)
MainActivity
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DynamicView myView = new DynamicView(this,320,480);
setContentView(myView);
}
}
这是一个很好的答案,居然让我明白的onDraw()/无效()范例。谢谢你。我看到我在我的问题中省略了一些东西 - 我希望将新行添加到视图中,以便显示的是所有绘制的线的聚合。当我运行你的例子时,结果是1000个独立的线条,每个线条都在原始的白色背景上绘制。 – dkimble
非常感谢。它的工作非常漂亮。现在了解更多关于它为什么起作用的信息(即关于位图)。 – dkimble