2014-12-03 77 views
-1

這裏我試圖添加一個圖像列表視圖和每個圖像上的文字。其實我試圖根據用戶輸入生成貼紙。 這是我的主要活動:如何添加圖像到可繪製的列表視圖

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ImageView im =(ImageView) findViewById(R.id.imageView1); 
     final EditText ed =(EditText) findViewById(R.id.editText1); 
     final TextView tx =(TextView) findViewById(R.id.textView1); 

     Button bt=(Button) findViewById(R.id.button1); 
     final Typeface custom_font = Typeface.createFromAsset(getAssets(), "font/d.ttf"); 

     bt.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       String ts1 = ed.getText().toString(); 
       tx.setText(ts1); 
       tx.setTypeface(custom_font); 


      } 
     }); 


    }} 

這是我的xml文件:

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.shifn2.MainActivity" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/editText1" 
     android:layout_alignBottom="@+id/editText1" 
     android:layout_toRightOf="@+id/editText1" 
     android:text="Button" /> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/button1" > 

    </ListView> 

</RelativeLayout> 

list.xml

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

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:textSize="30sp" 
      android:text="Large Text" 
      android:textColor="#FF0000" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/confidential" /> 

    </FrameLayout> 


</LinearLayout> 

請幫我做一個listview。任何幫助都是可以欣賞的。謝謝

+0

哪裏是ListView和適配器的代碼? – 2014-12-03 06:37:42

+0

我需要適配器代碼..即時新到android – 2014-12-03 06:45:29

+0

可以請你幫我嗎? – 2014-12-03 06:46:05

回答

2

這裏是圖像寫入文本代碼:

public static Bitmap writeTextOnDrawableImage(int drawableId, String text,Context con) { 

    Bitmap bm = BitmapFactory.decodeResource(con.getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true); 

    Typeface tf = Typeface.create("Verdana", Typeface.BOLD); 

    Paint paint = new Paint(); 
    paint.setStyle(Style.FILL); 
    paint.setColor(Color.WHITE); 
    paint.setTypeface(tf); 
    paint.setTextAlign(Align.CENTER); 
    paint.setTextSize(convertToPixels(con, 64)); 
    Rect textRect = new Rect(); 
    paint.getTextBounds(text, 0, text.length(), textRect); 

    Canvas canvas = new Canvas(bm); 

    // If the text is bigger than the canvas , reduce the font size 
    if (textRect.width() >= (canvas.getWidth() - 4)) // the padding on either sides is considered as 4, so as to appropriately fit in the text 
     paint.setTextSize(convertToPixels(con, 7)); // Scaling needs to be used for different dpi's 

    int xPos = (canvas.getWidth()/2) - 2; // -2 is for regulating the x position offset 

    int yPos = (int) ((canvas.getHeight()/2) - ((paint.descent() + paint 
      .ascent())/2)); 

    canvas.drawText(text, xPos, yPos, paint); 
    return bm; 
} 
+0

謝謝你的回答... bur我們可以把用戶輸入放在圖像上嗎? – 2014-12-03 08:50:02

+0

是的,你可以簡單地使用EditText.getText()。toString從EditText獲取文本,並將該值作爲上述方法中的參數傳遞。並請接受答案 – Narendra 2014-12-03 09:05:05

0

您可以創建ListViewImageViewTextView使用Volley庫或不。這裏是我找到的教程。

Using Volley Library

Without Volley Library

然後,你必須有創意落實到項目中。

+0

但我需要獲得圖像上的用戶輸入 – 2014-12-03 08:39:35

相關問題