2013-07-23 56 views
10

嗨,我一直在嘗試在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 

圖片沒有在運行時顯示。 我在做什麼錯?

回答

14

我相信最簡單的解決方法,不會覆蓋任何東西將有一個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"); 
+0

嘿謝謝你的想法。它工作!但我希望文字處於圖像的特定位置。怎麼做? –

+0

@VigneshBala我認爲這將是非常困難的,如果有可能參考drawable的特定部分。當然,你可以用'gravity'屬性來移動文本... – Mena

+0

感謝buddy它的工作!你太棒了...終於我得到我想要的東西 –

2

它不可能在imageview上寫文字,但是你可以用extend這樣做。另外,

  1. 您可以添加在layout重疊ImageView的元素,並使其可見,只有當你的條件爲真。

  2. ,或者您可以使用textView來顯示圖像,作爲背景或作爲繪製左/右..

PS:但是,選項1是更容易實現,但它會有額外的渲染。所以首先考慮其他選項。

+0

您可以繼承ImageView,重寫onDraw並在super.onDraw之後調用canvas.drawText。所以實際上可以在ImageView上編寫文本。 – npace

+0

@npace ...我沒有考慮到這一點,因爲我提出了其他方式更容易一些。 – Ankit

+0

夠公平的。你的第二個建議應該是一樣的好,但第一個建議應該是多餘的平局,即使這是最簡單的方法。 – npace

4

Android documentation

BitmapDrawable(位圖位圖) 這個構造函數是在API層面4.使用BitmapDrawable(資源,位圖)不贊成以確保繪製已正確設置它的目標密度。

您也可以創建自己的視圖(例如,參見Android: Creating Custom Views tutorial),並將圖像和文本放在此視圖中。

3

1)。做你自己的佈局說像這樣的Image_TextView.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:gravity="center" > 

     <ImageView 
      android:id="@+id/imgview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center" 
      android:adjustViewBounds="true" 
      android:cropToPadding="true" 
      android:scaleType="center" 
      android:src="@drawable/box" /> 

     <RelativeLayout 
      android:layout_width="150dp" 
      android:layout_height="35dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:layout_marginBottom="0dp" 
      android:background="#80666666" > 

      <TextView 
       android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:text="Hello" 
       android:textColor="#FFFFFF" 
       android:textStyle="bold"/> 
     </RelativeLayout> 
    </RelativeLayout> 

</LinearLayout> 

這將使圖像與文字在它上面。 2)。然後使用佈局充氣器在您的父視圖中膨脹此視圖。

注意:使用佈局充氣器,您可以添加其他視圖到您的父視圖。

相關問題