精华内容
下载资源
问答
  • I am trying to draw an animation... To do so I have extended View and overridden the onDraw() method. What I would expect is that each time onDraw() is called the canvas would be in the state that I l...

    I am trying to draw an animation. To do so I have extended View and overridden the onDraw() method. What I would expect is that each time onDraw() is called the canvas would be in the state that I left it in and I could choose to clear it or just draw over parts of it (This is how it worked when I used a SurfaceView) but each time the canvas comes back already cleared. Is there a way that I can not have it cleared? Or maybe save the previous state into a Bitmap so I can just draw that Bitmap and then draw over top of it?

    解决方案

    I'm not sure if there is a way or not. But for my custom views I either redraw everything each time onDraw() is called, or draw to a bitmap and then draw the bitmap to the canvas (like you suggested in your question).

    Here is how i do it

    class A extends View {

    private Canvas canvas;

    private Bitmap bitmap;

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

    if (bitmap != null) {

    bitmap .recycle();

    }

    canvas= new Canvas();

    bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    canvas.setBitmap(bitmap);

    }

    public void destroy() {

    if (bitmap != null) {

    bitmap.recycle();

    }

    }

    public void onDraw(Canvas c) {

    //draw onto the canvas if needed (maybe only the parts of animation that changed)

    canvas.drawRect(0,0,10,10,paint);

    //draw the bitmap to the real canvas c

    c.drawBitmap(bitmap,

    new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()),

    new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()), null);

    }

    }

    展开全文
  • 以下是自定义View的一段代码: @Override public void onDraw(Canvas canvas) { // 首先定义一个paint Paint paint = new Paint(); // 绘制矩形区域-实心矩形 // 设置颜色 paint.setColor(Color.WHITE); // 设置样式...

    做java的都知道,绘图肯定首先需要一个Canvas,然后在用Graphics在上面绘制自己想要图案。不错,Android上面也类似,你可以从一个Bitmap得到它的Canvas,进行绘制,也可以自定义一个View,用它的 Canvas。不同的时,Android里没有Graphics,而用 Paint代之,当然用法也稍有不同。以下是自定义View的一段代码:

    @Override

    public void onDraw(Canvas canvas) {

    // 首先定义一个paint

    Paint paint = new Paint();

    // 绘制矩形区域-实心矩形

    // 设置颜色

    paint.setColor(Color.WHITE);

    // 设置样式-填充

    paint.setStyle(Style.FILL);

    // 绘制一个矩形

    canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paint);

    // 绘空心矩形

    // 设置颜色

    paint.setColor(Color.RED);

    // 设置样式-空心矩形

    paint.setStyle(Style.STROKE);

    // 绘制一个矩形

    canvas.drawRect(new Rect(10, 10, 50, 20), paint);

    // 绘文字

    // 设置颜色

    paint.setColor(Color.GREEN);

    // 绘文字

    canvas.drawText(str, 30, 30, paint);

    // 绘图

    // 从资源文件中生成位图

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

    // 绘图

    canvas.drawBitmap(bitmap, 10, 10, paint);

    }

    以上需要注意的有三点:

    1、Android中的Rect和java中的可能稍有区别,前两个参数是左上角的坐标,后两个参数是右下角的坐标(不是宽度和高度);

    2、Style.STROKE和Style.FILL外边的像素数是有区别的,这点和java里一样;

    3、绘文字时,设置的坐标点为(30,30),但绘出来后你会发现,文字的左上角坐标要比你设置的偏上,不知道是android设置的bug,还是我们有理解到坐标点的意义。

    展开全文
  • OnDraw function for my custom View is being called infinitely and is looping !! What could be possible reason??Here is my custom view:-public class Balls extends View{private static final String TAG =...

    OnDraw function for my custom View is being called infinitely and is looping !! What could be possible reason??

    Here is my custom view:-

    public class Balls extends View{

    private static final String TAG = "BallsView";

    private int mMode = READY;

    public static final int PAUSE = 0;

    public static final int READY = 1;

    public static final int RUNNING = 2;

    public static final int LOSE = 3;

    private final Paint mPaint = new Paint();

    private double mUx = 0.1;

    private double mUy = 2;

    private double mVy;

    private double mVx;

    private double mSx;

    private double mSy;

    private double mRange;

    private float mX1;

    private float mY1;

    private int mX2;

    private int mY2;

    private int mDx;

    private int mDy;

    Time t;

    float mAngle;

    private final double mGravity = -9.8;

    private long mLastTime;

    private double mT;

    private Canvas mCanvas = null;

    public Balls(Context context, AttributeSet attrs, int defStyle){

    super(context, attrs, defStyle);

    setFocusable(true);

    setWillNotDraw(false);

    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

    mPaint.setStrokeWidth(10);

    mPaint.setAntiAlias(true);

    mPaint.setStrokeCap(Cap.ROUND);

    //mPaint.setColor(0xff00ffff);

    mPaint.setARGB(255, 0, 255, 0);

    mLastTime = System.currentTimeMillis();

    }

    public Balls(Context context, AttributeSet attrs) {

    super(context, attrs);

    setFocusable(true);

    setWillNotDraw(false);

    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

    mPaint.setStrokeWidth(10);

    mPaint.setAntiAlias(true);

    mPaint.setStrokeCap(Cap.ROUND);

    mPaint.setColor(0xff00ffff);

    }

    @Override

    public void onDraw(Canvas canvas) {

    Log.w(this.getClass().getName(),"onDraw of Balls called");

    super.onDraw(canvas);

    mCanvas = canvas;

    if(mCanvas!= null)

    Log.w(this.getClass().getName(),"Canvas is not null");

    }

    This view is inflated as follows in another activity:-

    mBalls = (Balls) findViewById(R.id.balls);

    This view is placed in xml file inside a relative view and the relative view is the child of horizontal scroll view.

    解决方案

    There isn't an infinite loop here. What is going on is the OS is redrawing your activity as fast as possible. When your activity gets redrawn it redraws all of its children views. As your code has very little computation, from what I can see here, it is running very fast and is probably redrawing at a rate >30 FPS. Your log message makes it appear as if there is an infinite loop when there isn't. In fact there isn't even a loop inside your onDraw method.

    To illustrate what is going on try this. Add a protected member drawCount to your Balls class and set it to 0:

    protect int drawCount = 0;

    Then append drawCount to the end of your onDraw log message

    public void onDraw(Canvas canvas){

    drawCount++;

    Log.w(this.getClass().getName(),"onDraw of Balls called. Total draws:" + Integer.toString(drawCount));

    ...

    }

    What you should see is each log message will display a different drawCount.

    If you want to get fancy and calculate the framerate of your app you could measure the time since the first draw and then divide the drawCount by how much time has passed which would give you a estimate of your activities framerate.

    展开全文
  • 这里有一点误会 . invalidate和postInvalidate方法用于告诉View它需要在最早的绘图周期中刷新和重绘 . 不同之处在于应该从UI线程中调用invalidate方法,并且应该从UI线程外部调用postInvalidate ....

    这里有一点误会 . invalidate和postInvalidate方法用于告诉View它需要在最早的绘图周期中刷新和重绘 . 不同之处在于应该从UI线程中调用invalidate方法,并且应该从UI线程外部调用postInvalidate .

    这里简要描述了这些方法:

    另一方面,AsyncTask是一个专为您在UIThread中运行're facing. When you need to perform a big task in the background, asynchronously you need the AsyncTask for that, but! the AsyncTask'回调方法的问题设计的类!

    在这里看一下AsyncTask方法的解释:

    执行异步任务时,任务将执行4个步骤:

    onPreExecute(),在执行任务后立即在UI线程上调用 . 此步骤通常用于设置任务,例如通过在用户界面中显示进度条 . doInBackground(Params ...),在onPreExecute()完成执行后立即在后台线程上调用 . 此步骤用于执行可能需要很长时间的后台计算 . 异步任务的参数将传递给此步骤 . 计算结果必须由此步骤返回,并将传递回最后一步 . 此步骤还可以使用publishProgress(Progress ...)发布一个或多个进度单元 . 这些值发布在UI线程的onProgressUpdate(Progress ...)步骤中 . onProgressUpdate(Progress ...),在调用publishProgress(Progress ...)后在UI线程上调用 . 执行的时间是不确定的 . 此方法用于在后台计算仍在执行时显示用户界面中的任何形式的进度 . 例如,它可用于为进度条设置动画或在文本字段中显示日志 . onPostExecute(Result),在后台计算完成后在UI线程上调用 . 背景计算的结果作为参数传递给该步骤 .

    这意味着在onPostExecute方法中,您应该尝试使用invalidate方法而不是从UIThread调用的postInvalidate方法 .

    展开全文
  • 安卓onDraw问题待整理

    2018-03-24 19:33:05
    JSONObjet 和JSONArray必须在安卓运行环境下才能运行,因为这两个东西是安卓提供的API不是Java原生的API,如果要单元测试的话建议不要用这两个东西。建议用gson或者fastjson等独立的jar包来测试。因为单元测试没有...
  • } } } invalidate()和postInvalidate() 的区别及使用 当Invalidate()被调用的时候,View的OnDraw()就会被调用;Invalidate()是刷新UI,UI更新必须在主线程,所以invalidate必须在UI线程中被调用,如果在子线程中...
  • 你只需显式地调用View对象中的invalidate(){关于invalidate的解释:当调用线程处于空闲状态时,会调用onDraw,刷新界面,也就是说,该函数仅是标记当前界面过期,并不直接负责刷新界面;}方法即可。系统会自动调用Vie...
  • 关注 安卓007 ,免费获取全套安卓开发学习资料用途支持对任何视图进行包裹后3D翻转.效果图主要代码class ThreeDLayout(context: Context?, attrs: AttributeSet? = null) : ViewGroup(context, attrs) { private val ...
  • If the view is visible, {@link #onDraw} will * be called at some point in the future. This must be called from a * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}. */ public ...
  • 在编写一个自定义视图时由于有一点动画,所以便想到了如果是在播放动画时是否会调用View.onDraw()方法进行更新界面呢?刚开始我理所当然的觉得是应该需要,因为看起来这个动画的大小和位置和大小过都变了啊,既然位置...
  • 继续研究,发现过度绘制问题是由于OverDrawView类中的ondraw方法中多次绘制了矩形导致的,代码如下: @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = getWidth(); int ...
  • 画完背景后,draw过程会调用onDraw(Canvas canvas)方法,然后就是dispatchDraw(Canvas canvas)方法,dispatchDraw()主要是分发给子组件进行绘制,我们通常定制组件的时候重写的是onDraw()方法。值得注意的是...
  • 1、ViewGroup onDraw不执行的原因 2、怎么让ViewGroup onDraw执行 3、setWillNotDraw(boolean)作用 如果对原理不感兴趣,请拉到最后的总结查看解决办法~ 小例子 我们知道自定义view的时候会重写onDraw()方法,如下...
  • [Android]【安卓】自定义控件onDraw前调用的方法 本篇博客已收录到我的安卓开发小结中——点击【安卓开发小结】 这个方法的调用是在onDraw方法之前。 view.getViewTreeObserver().addOnGlobalLayoutListener...
  • 原文链接:... View通过View.onDraw(Canvas canvas)来Draw. 我们可以定义自己的继承于View的TestView,然后重载View.onDraw(Canvas canvas).   对于自定义的TestView如何与Ac
  • 安卓自定义view全解安卓自定义view在本文的示例中,我们使用view基类的基本函数进行了属性设置,在onDraw函数中使用Canvas进行绘图,在onMeasure函数中调整控件大小,并且重写了onKeyDown、onKeyUp、...
  • 安卓自定义view中invalidate后并没有调用ondraw(),是怎么回事啊?
  • protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.BLACK); // 画笔颜色为灰色 paint.setColor(Color.GRAY); // 宽度3个像素 paint.setStrokeWidth(3); //字体...
  • 从 ViewRootImpl.TraversalRunnable.run 到 onMeasure,onLayout,onDraw 函数栈 onMeasure的函数栈: onLayout的函数栈: onDraw的调用栈 子视图的绘制调用栈 android.view.ViewRootImpl.TraversalRunnable 函数栈 ...
  • 自定义View之 onDraw()(三)  上两篇已经介绍了自定义View的onMeasure和onLayout方法,那么接下来我们继续深究自定义view的 onDraw(Canvas canvas) ,在探究 onDraw方法之前,我们必须先深入了解两个类Paint和...
  • protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Rect r = new Rect(); getLocalVisibleRect(r); canvas.drawArc(new RectF(r), 0, degree, ...
  • 随手指运动的小球–Painter,OnDraw,invalidate,drawCircle 1、创建DrawView继承自View public class DrawView extends View { //创建一个画笔 private Paint painter; //创建一个画圆的参数 private float ...
  •  在自定义View中,常常碰见onMeasure(),onLayout(),onDraw()三个方法,还有onTouchEvent(),可以构建我们想要实现的控件。 我们知道,无论是我们自定义的View还是系统自带的View(例如button,textview),都是...
  • protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Rect r = new Rect(); getLocalVisibleRect(r); canvas.drawArc(new RectF(r), 0, degree, ...
  • Android 中的每个控件都会在界面中占得一块矩形的区域, 而 android ... protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.parseColor("#00aad6")); }...

空空如也

空空如也

1 2 3 4 5 ... 9
收藏数 180
精华内容 72
关键字:

安卓ondraw