2014-12-03 83 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

相关问题