How to write Text on ImageView in android coding?(如何在 Android 编码中在 ImageView 上编写文本?)
问题描述
您好,我一直在尝试在 Imageview 上编写数字.N 个问题的图像.如果用户输入的答案是正确的,那么它应该显示带有勾号图像的问题编号,否则带有错误标记图像的问题编号.我需要在图片上写下问题编号.我的代码在这里:
Hi I've been trying to write Numbers on Imageview. Images for N number of questions. If the user entered answer is correct, then it should be displayed with question number with tick mark image else question number with wrong mark image. I need to write the question number on the image. My code is here:
LinearLayout l_layout = (LinearLayout) findViewById(R.id.linear_view_report);
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
ImageView[] imgview=new ImageView[questions.length];
for(int i=0;i<no_of_questions;i++)
{
if(userEnteredAnswers[i]==correct_answer[i]){
Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.correct);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText(i, 5, 5, paint);
imgview[i]=new ImageView(this);
imgview[i].setImageDrawable(new BitmapDrawable(bm));
l_layout.addView(imgview[i]);
}
else {
Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.wrong);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText(i, 5, 5, paint);
imgview[i]=new ImageView(this);
imgview[i].setImageDrawable(new BitmapDrawable(bm));
l_layout.addView(imgview[i]);
}
}
我收到此警告:
The constructor BitmapDrawable(Bitmap) is deprecated
图像在运行时不显示.我做错了什么?
Image doesn't showing at run time. What am I doing wrong?
推荐答案
我相信不覆盖任何东西的最简单的解决方法是拥有一个 TextView
并使用 drawable
设置其背景代码>资源.
I believe the easiest workaround without overriding anything would be to have a TextView
and set its background with a drawable
resource.
例如:
TextView t = (TextView)findViewById(R.id.my_text_view);
// setting gravity to "center"
t.setGravity(Gravity.CENTER);
t.setBackgroundResource(R.drawable.my_drawable);
t.setText("FOO");
这篇关于如何在 Android 编码中在 ImageView 上编写文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Android 编码中在 ImageView 上编写文本?
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01