我正在尝试了解如何在Android上动态生成2D图形。我无法在网上找到有人这样做的例子。
下面的示例随机抽取1000行随机颜色。我想看到所有的线都被绘制,但是在setContentView()调用之前我没有看到任何东西(此时所有的线都已被绘制)。
我尝试了很多排列,包括在draw()调用之前移动setContentView(),并在for循环中添加myView.invalidate(),但在每种情况下屏幕都保持白色,直到所有行都显示为绘制。
如果有人可以指出这项工作的一个例子,我很乐意去研究它并在这里自己添加正确答案。
package com.example.graphicsdemo;
+imports
public class MainActivity extends Activity {
ImageView ourView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw();
setContentView(ourView);
}
public void draw() {
Bitmap blankBitmap = Bitmap.createBitmap(600,600,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(blankBitmap);
ourView = new ImageView(this);
ourView.setImageBitmap(blankBitmap);
Paint paint = new Paint();
Random random = new Random();
for(int i = 0 ; i < 1000 ; i++) {
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);
}
}
}
答案 0 :(得分:4)
我希望这个例子会有所帮助: 首先制作自己的View,然后将其命名为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);
}
这很简单。
回答您的问题这是一个更好的代码,更复杂但仍然易于理解 我们所要做的就是在位图上绘制线条,然后在视图上绘制该位图,这样位图就不会改变,并且将包含所有先前绘制的线条并仍在其上绘制
现在这里是更新的代码:
<强> 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方法并输入你的值&#39; s)
<强> 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);
}
}