问题用画布绘制文本到一个ImageView的 [英] Problems drawing text onto an ImageView using a Canvas

查看:214
本文介绍了问题用画布绘制文本到一个ImageView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的ListView页脚将作为与五行的菜单式车架。我想要的元素才能够(动态,根据用户输入)显示图像或文字(或可能两个,但我现在还没有)。最终,这将是更多的互动,但现在我是想实现它,这样的ImageView的(默认情况下显示图像)将删除的图像和显示文本来代替。

我想实现这个的最佳方式是画在画布上的文字和使用 android.view.View.draw(帆布C)方法来绘制到ImageView的。然而,我无法这样做。具体来说,我可以修改,几乎任何方式(设置背景颜色,改变绘制,等等等等)的观点,这一切工作正常,和code画在画布上编译正确,但是它没有<强>做在运行时什么。该视图保持完全不变。结果
注意:我试图通过消除code任何愚蠢的错误(即确信文本并不透明/颜色相同BG,确信文本绘制视图的区域内等),但我不是100%,它是免费的错误。)

我都尝试绘图传入的onClick功能视图和图纸,从通过视图构建一个新的ImageView - 既不工作。
有没有人有什么建议?
code如下:

  login_button.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(查看视图){
            位图IMG = Bitmap.createBitmap(60,60,Config.ARGB_8888);
            帆布C =新的Canvas();
            c.setBitmap(IMG);            涂料myPaint =新的油漆();
            myPaint.setTypeface(MFACE);
            myPaint.setColor(android.R.color.black);
            myPaint.setTextSize(2);
            myPaint.setTextAlign(Paint.Align.LEFT);            字符串内容=测试;
            c.drawText(内容,0,0,myPaint);
            view.draw(三); //不执行任何
            ImageView的视图2 =(ImageView的)视图。
            view2.setBackgroundColor(android.R.color.white); //这个完美的作品
            view2.draw(三); //不执行任何
            Toast.makeText(MainListView.this,+ c.getHeight(),Toast.LENGTH_SHORT).show(); //确保c的高度 - 这将返回60预期
            Toast.makeText(MainListView.this,法结束,Toast.LENGTH_SHORT).show();
        }
    });


解决方案

View.draw(画布)绘制查看到画布上,而不是相反像你似乎期望的。

如果你想这样做它一起,你已经是行,你需要扩展现有查看类,可能的TextView 的ImageView ,并覆盖的onDraw(),如的自定义组件开发指南

我想我会做的是使用像一个的FrameLayout ,并将其设置为包含任何适当的的TextView 的ImageView 是必要的。这似乎不是在ImageView的手工绘制文本清洁。

I'm trying to implement a footer for my ListView which will act as a menu-type frame with five elements. I want the elements to be able to (dynamically, based on user input) display either images or text (or possibly both, but I'm not there yet). Eventually it will be more interactive but for now I was trying to implement it so that the ImageView (which by default displays an image) would erase the image and display text instead.

I thought the best way to implement this was to draw text on a canvas and use the android.view.View.draw(canvas c) method to draw it onto the ImageView. However, I'm having trouble doing this. Specifically, I can modify the view in almost any way (set the background color, change the drawable, etc etc) and it all works fine, and the code to draw the canvas compiles properly, but it doesn't do anything at runtime. The view remains completely unchanged.
(Note: I've tried to eliminate any stupid mistakes through code (i.e. made sure text wasn't transparent/same color as bg, made sure text was drawn inside the area of the view, etc) but I am not 100% it's error free.)

I tried both drawing to the view passed into the onClick function and drawing to a new ImageView constructed from the passed view - neither works. Does anyone have any suggestions? Code follows:

login_button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Bitmap img = Bitmap.createBitmap( 60, 60, Config.ARGB_8888);
            Canvas c = new Canvas();
            c.setBitmap(img);

            Paint myPaint = new Paint();
            myPaint.setTypeface(mFace);
            myPaint.setColor(android.R.color.black);
            myPaint.setTextSize(2);
            myPaint.setTextAlign(Paint.Align.LEFT);

            String content = "testing";
            c.drawText(content, 0,0, myPaint);
            view.draw(c);//does nothing
            ImageView view2 = (ImageView) view;
            view2.setBackgroundColor(android.R.color.white); //this works perfectly
            view2.draw(c);//does nothing
            Toast.makeText(MainListView.this, ""+c.getHeight(), Toast.LENGTH_SHORT).show(); //making sure c has height - this returns 60 as expected
            Toast.makeText(MainListView.this, "end of method", Toast.LENGTH_SHORT).show();
        }
    });

解决方案

View.draw(Canvas) draws the View onto the Canvas, not vice versa like you seem to expect.

If you want to do it along the lines that you already are, you need to extend an existing View class, likely TextView or ImageView, and override onDraw(), as explained in the Custom Components Dev Guide.

I think what I would do is use something like a FrameLayout and set it to contain either the appropriate TextView or ImageView as necessary. That seems cleaner than manually rendering text in an ImageView.

这篇关于问题用画布绘制文本到一个ImageView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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