canvas教程

android:onDraw不叫 - android:onDraw not called

字号+ 作者:H5之家 来源:H5之家 2015-10-15 18:13 我要评论( )

28im技术大牛是面向程序员的知识学习与分享社区,追求最前沿技术

主题:

我想叫onDraw/20ms,,但是它不正确~我搜索一些答案,他们都说添加setWillNotDraw(假)构造函数,我试一试,但没有效果。它看起来像你的文章主要是代码,请添加一些更多的细节。它看起来像你的文章主要是代码,请添加一些更多的细节。

原文:

package com.star.xxx.xxx; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.graphics.Rect; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.WindowManager; public class ShowActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WindowManager wm = this.getWindowManager(); Point p = new Point(); wm.getDefaultDisplay().getSize(p); show_view v = new show_view(this, p); v.setWillNotDraw(false); v.layout(0, 0, p.x, p.y); setContentView(v); } public class show_view extends View{ public show_view(Context context, Point window_size) { super(context); _window_size = window_size; show_thread t = new show_thread(); t.run(); } @Override protected void onDraw(Canvas canvas) { for (int i = 0; i < RECT_COUNT; i++) { canvas.drawRect(_rects[i], GetPaint()); } super.onDraw(canvas); } private void Update(){ Point p_pos; Point p_rect_size = new Point(); for (int i = 0; i < RECT_COUNT; i++) { p_pos = GetRandomPoint(_window_size); p_rect_size.x = _window_size.x - p_pos.x; p_rect_size.y = _window_size.y - p_pos.y; p_rect_size = GetRandomPoint(p_rect_size); _rects[i] = new Rect(); _rects[i].top = p_pos.x; _rects[i].left = p_pos.y; _rects[i].bottom = p_pos.x + p_rect_size.x; _rects[i].right = p_pos.y + p_rect_size.y; } } private Point GetRandomPoint(Point p){ Point new_p = new Point(); double x = Math.random() * p.x; double y = Math.random() * p.y; new_p.set((int) x, (int) y); return new_p; } private Paint GetPaint(){ Paint p = new Paint(); int r, g, b; r = (int)(Math.random() * 255); g = (int)(Math.random() * 255); b = (int)(Math.random() * 255); p.setColor(Color.rgb(r, g, b)); return p; } private Point _window_size = null; final int RECT_COUNT = 20; private Rect[] _rects = new Rect[RECT_COUNT]; public class show_thread extends Thread{ @Override public void run() { long time_down = 5 * 1000 + System.currentTimeMillis(); while (time_down > System.currentTimeMillis()){ Update(); postInvalidate(); try { Thread.sleep(20, 0); } catch (InterruptedException e) { e.printStackTrace(); } } super.run(); } } } }

i want to call onDraw/20ms,,but it works incorrectly~ i search some answers, they are all says add setWillNotDraw(false) to constructor, i try it ,but no effect.

It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details.

楼主:@MikeM。初始化。后者被称为一次,它吸引了一些矩形在屏幕上,

(原文:@MikeM. inited. the onDraw is called once, it draws some rects on screen,)

网友:删除这些东西,而不是使用“属性动画框架”或年代

(原文:remove this public class show_thread extends Thread stuff, instead use "Property Animation framework" or Handlers)

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Canvas与ValueAnimator

    Canvas与ValueAnimator

    2017-04-28 18:00

  • Android Bitmap和Canvas学习笔记(转)

    Android Bitmap和Canvas学习笔记(转)

    2017-04-28 17:00

  • 21天学习android开发教程之SurfaceView与多线程的混搭

    21天学习android开发教程之SurfaceView与多线程的混搭

    2017-04-27 12:00

  • Android画图学习免费下载

    Android画图学习免费下载

    2017-04-27 11:01

网友点评