嗨,我一直在嘗試在Imageview上編寫數字。 N個問題的圖像。如果用戶輸入的答案是正確的,那麼它應該顯示帶有標記圖像的問題編號,否則帶有錯誤標記圖像的問題編號。我需要在圖片上寫下問題編號。我的代碼是在這裏:如何在android編碼中的ImageView上編寫文本?
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
圖片沒有在運行時顯示。 我在做什麼錯?
嘿謝謝你的想法。它工作!但我希望文字處於圖像的特定位置。怎麼做? –
@VigneshBala我認爲這將是非常困難的,如果有可能參考drawable的特定部分。當然,你可以用'gravity'屬性來移動文本... – Mena
感謝buddy它的工作!你太棒了...終於我得到我想要的東西 –