我前段时间制作了一款全屏显示随机像素颜色的应用。第二种选择是在屏幕上移动点(现在我也在它周围绘制圆圈)。
该应用在Android 4.0和4.1上运行良好。现在我也有4.4和5.1的设备,并且完全随机屏幕两者都不起作用(我发了一条日志消息,确保所有的drawPoint-s都是为所有屏幕制作的 - 它们都是,但我的视图没有显示),点包围按圆圈在5.1.1上工作正常,但是在绘制4.4个圆圈时,中心的点不是。
为什么以及为什么这样的差异,比如没有画出来?如何在Andoid 4.4和5上绘制很多点 - 使onDraw在后台线程中运行?我的4.0(Micromax A90)和4.1不是CPU-GPU的顶级,5.1是Sony Xperia Z,如果跳过的帧是问题。 ÿ
以下是我的代码的一部分: 对于圆圈和点:
public class DrawView02 extends View {
Random rnd = new Random();
Paint paint = new Paint();
Paint paint2 = new Paint();
int myWidth;
int myHeight;
int myColor;
View myView;
int myCount = 0;
public DrawView02(Context context, View view) {
super(context);
myView = view;
}
@Override
public void onDraw(Canvas canvas) {
myWidth = myView.getWidth();
myHeight = myView.getHeight();
myColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
paint.setColor(myColor);
paint2.setColor(Color.argb(255, 255, 255, 255));
if (myCount==0) {
// too much effort for one starting point
canvas.drawPoint(myWidth - 1, myHeight - 1, paint);
} else {
canvas.drawCircle(myCount + 1, myCount + 1, 25, paint);
canvas.drawCircle(myCount + 1, myCount + 1, 20, paint2);
canvas.drawCircle(myCount + 1, myCount + 1, 10, paint);
canvas.drawCircle(myCount + 1, myCount + 1, 9, paint2);
canvas.drawPoint(myCount+1, myCount+1, paint);
}
myCount++;
}
}
完全随机屏幕:
主要活动中的
public void setTimer(){
myTimerTask = new TimerTask() {
public void run() {
myHandler.post(new Runnable() {
public void run() {
myRun();
//Log.d("TIMER", "TimerTask run");
}
});
}};
// public void schedule (TimerTask task, long delay ms, long period ms)
myTimer.schedule(myTimerTask, 1000, 10);
Log.d("TIMER", "TimerTask set");
}
public void myRun () {
myView.removeAllViews();
drawView = new DrawView(myContext, myView);
myView.addView(drawView);
}
public class DrawView extends View {
Random rnd = new Random();
Paint paint = new Paint();
int myWidth;
int myHeight;
int myColor;
View myView;
public DrawView(Context context, View view) {
super(context);
myView = view;
}
@Override
public void onDraw(Canvas canvas) {
myWidth = myView.getWidth();
myHeight = myView.getHeight();
Log.i ("TAG", "starting to draw points");
//MainActivity.myThread();
for (int i=1;i<myWidth;i++){
for (int j=1;j<myHeight;j++){
myColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
paint.setStyle(Paint.Style.STROKE);
paint.setColor(myColor);
canvas.drawPoint(i,j,paint);
}
}
int test = 1;
int test2 = test;
Log.i ("TAG", "all points are drawn");
}
}
登录5.1.1:
02-03 21:45:34.303 10103-10103/julius.randomscreen I/TAG: all points are drawn
02-03 21:45:35.382 10103-10103/julius.randomscreen I/Choreographer: Skipped 64 frames! The application may be doing too much work on its main thread.
登录4.4:
02-03 22:00:28.075 17419-17419/julius.randomscreen I/TAG: all points are drawn
02-03 22:00:28.075 17419-17419/julius.randomscreen D/View: [ANR Warning]onDraw time too long, this =julius.randomscreen.DrawView{41d65380 V.ED.... ........ 0,0-838,354}time =3911 ms
02-03 22:00:44.441 17419-17419/julius.randomscreen I/Choreographer: Skipped 36 frames! The application may be doing too much work on its main thread.
实际上经过几次“跳过X帧”和长时间5.1显示我的视图后,4.4几分钟后没有多次“跳帧”
更新: 在5.1视图上显示为仅在onDraw的第二次运行时填充随机颜色点,从开始记录到我看到我的视图时的预期(我的Macromax上的PS花了大约15秒用点填充屏幕,在Xperia Z上它是两个分钟):
02-04 04:01:50.885 20146-20146/julius.randomscreen D/TIMER: application timer set
02-04 04:01:51.906 20146-20146/julius.randomscreen I/TAG: starting to draw points
02-04 04:02:35.878 20146-20146/julius.randomscreen I/TAG: all points are drawn
02-04 04:02:38.031 20146-20146/julius.randomscreen I/Choreographer: Skipped 127 frames! The application may be doing too much work on its main thread.
02-04 04:02:38.034 20146-20146/julius.randomscreen I/TAG: starting to draw points
02-04 04:03:07.963 20146-20159/julius.randomscreen W/art: Suspending all threads took: 5.157ms
02-04 04:04:06.577 20146-20159/julius.randomscreen W/art: Suspending all threads took: 8.789ms
02-04 04:04:09.007 20146-20146/julius.randomscreen I/TAG: all points are drawn