安卓:文本使用的drawText写不可见的() [英] Android: Text not visible on writing using drawText()

查看:139
本文介绍了安卓:文本使用的drawText写不可见的()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @覆盖
          保护无效的OnDraw(帆布油画)
          {
            //注:我不想使用画布对象,从这个函数参数
            //如果我这样做其工作,但我想知道为什么,下面是不工作

         帆布C =新的Canvas();
             涂料粉刷=新的油漆();
             paint.setStyle(Paint.Style.FILL);
             paint.setAntiAlias​​(真正的);
             paint.setColor(Color.WHITE);
             c.drawText(HELLO帆布,200,300,涂料);
        }
 


更多code

 公共类graphicProj延伸活动{

   私人帆布油画;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
    {
        ....

        SimpleView simpleview_obj =新SimpleView(本);
        的setContentView(simpleview_obj);
        simpleview_obj.onDraw(画布);
         .....

     新的GetData()执行()。

     }
    私有静态类SimpleView扩展视图{
        私人ShapeDrawable mDrawable =新ShapeDrawable();
....
    保护无效的OnDraw(帆布油画){

    //绘制图形对象
    ....
        }
     }

    公共类的GetData扩展的AsyncTask<太虚,字符串,太虚> {

         @覆盖
        在preExecute保护无效(){
             Log.d(PROJ,挑动);
        }

        @覆盖
        保护无效doInBackground(空...未使用){
        ////我的计算和阅读FRM的数据流中

         }

        @覆盖
        保护无效onProgressUpdate(字符串...数据){

           //我不断更新的结果...
            涂料粉刷=新的油漆();
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias​​(真正的);
            paint.setColor(Color.WHITE);
            canvas.drawText(结果,200,300,油漆);


        }

        @覆盖
        保护无效onPostExecute(作废不用){
             Log.d(PROJ,END);
        }
    }

}
 

解决方案

未在此处或您的其他问题有你提供了足够的信息,在为什么不能这样做。没有理由画上了一个新的画布,而不是已经存在的。

在code不工作,因为你的新画布 C 没有分配到任何东西。它像创建一个字符串 myString的的日志,但从来没有使用 Log.d(标签,MyString的)

修改(阅读完所有的意见后)

如果你计算你的onCreate()值并要显示在你的OnDraw()这个值,简单地做到这一点。结果存储在一个成员变量,您可以访问它在的OnDraw()。

否则:请提供您完整的code。我猜你只是做它的方式更加复杂比它应该是...

EDIT2

您code是有点乱,做了很多的东西在地方,你不应该这样做。所以里面绘制 onProgressUpdate()是严重的错误。你应该封装你的计算和绘图。

您应该做些什么(我建议使用SurfaceView而不是查看,反正...):

您应该启动AsynchTask哪些更新要绘制的字符串。该字符串应该是在你查看,你用它来绘制一个变量。 图纸本身应该由绘图线程调用(我记得:使用视图的SurfaceView而不是作为父类)。这里面的OnDraw(),你应该只使用您的油漆对象,给定的画布,要绘制的字符串(不要忘了让油漆变量也是一个成员变量以prevent再次重现相同的对象一遍又一遍的性能/内存的原因)。

如果你不知道如何使用SurfaceView,或者如果你想工作,了解如何可以与一个绘图线程请阅读我的教程的2D绘图:www.droidnova.com/2d-tutorial-series

一个短的最后一句话:你以正确的方式做了很多的事情,你只是混了,你做的地方。你应该试着重新考虑你真正想实现,以及如何可以做最简单的方法。也许我的教程有助于理清思绪一点。

          @Override 
          protected void onDraw(Canvas canvas) 
          {
            //Note:I do not  want to use the canvas object from this function param
            //If i do so its working , But i would like to understand why the following is not working

         Canvas c =new Canvas();
             Paint paint = new Paint();
             paint.setStyle(Paint.Style.FILL);
             paint.setAntiAlias(true);
             paint.setColor(Color.WHITE);
             c.drawText("HELLO CANVAS",200,300,paint);
        }


MORE CODE

public class graphicProj extends Activity {

   private Canvas canvas;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    {
        ....

        SimpleView simpleview_obj = new SimpleView(this);
        setContentView(simpleview_obj);
        simpleview_obj.onDraw(canvas); 
         .....

     new GetData().execute();

     }
    private static class SimpleView extends View {
        private ShapeDrawable mDrawable = new ShapeDrawable();
....    
    protected void onDraw(Canvas canvas) {

    //draw graphic objects
    ....
        }
     }

    public class GetData extends AsyncTask<Void, String, Void> {

         @Override
        protected void onPreExecute() {
             Log.d("PROJ","STARTIN");
        }

        @Override
        protected Void doInBackground(Void... unused) {
        ////My calculation and reading frm DataStream

         }

        @Override
        protected void onProgressUpdate(String... data) {

           //I Keep updating the result...
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            paint.setColor(Color.WHITE);
            canvas.drawText(result, 200, 300, paint);


        }

        @Override
        protected void onPostExecute(Void unused) {
             Log.d("PROJ","END");
        }
    }

}

解决方案

Not here or in your other question have you provided enough information on why you can't do that. There is no reason to draw on a new canvas instead of the already existing one.

The code is not working because your new Canvas c isn't assigned to anything. Its like creating a String myString for a log but never using Log.d(tag, myString)

edit (after reading all the comments)

If you calculate a value in your onCreate() and want to display that value in your onDraw(), that simply do that. Store the result in a member variable and you can access it in the onDraw().

Otherwise: Please provide your complete code. I guess you just do it way more complex than it should be...

edit2

Your code is a bit messy and does a lot of stuff in areas where you shouldn't do it. So drawing inside the onProgressUpdate() is seriously wrong. You should encapsulate your calculation and drawing.

What you should do (I recommend using SurfaceView instead of View, anyway...):

You should start your AsynchTask which updates the string you want to draw. The string should be a variable inside your View, where you use it for drawing. The drawing itself should be called by a drawing thread (I remember: use the SurfaceView instead of the View as a parent class). Inside that onDraw() you should just use your paint object, the given canvas and the string you want to draw (don't forget to make the paint variable also a member variable to prevent recreating the same object over and over again for performance/memory reasons).

If you do not know how to work with a SurfaceView or if you want to learn how you could work with a drawing thread please read my tutorial about 2d drawing: www.droidnova.com/2d-tutorial-series

A short last sentence: You did a lot of things in the right way, you just mixed up with the places where you do it. You should try to rethink what you really want to achieve and how it could be done the easiest way. Maybe my tutorial helps to clear your mind a bit.

这篇关于安卓:文本使用的drawText写不可见的()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆