gpt4 book ai didi

java - 如何在 Android 编码中在 ImageView 上写文本?

转载 作者:搜寻专家 更新时间:2023-10-30 21:00:41 24 4
gpt4 key购买 nike

您好,我一直在尝试在 Imageview 上编写数字。 N 个问题的图像。如果用户输入的答案是正确的,则应显示带有刻度图像的问题编号,否则显示带有错误标记图像的问题编号。我需要在图像上写下问题编号。我的代码在这里:

    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

图像在运行时不显示。我做错了什么?

最佳答案

我相信在不覆盖任何内容的情况下最简单的解决方法是拥有一个 TextView 并使用 drawable 资源设置其背景。

例如:

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");

关于java - 如何在 Android 编码中在 ImageView 上写文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17809801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com