如何从drawable添加图像到listview

时间:2014-12-03 06:32:37

标签: android xml listview

这里我试图将图像添加到列表视图和每个图像上的文本。实际上我试图根据用户输入生成贴纸。 这是我的主要活动:

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>

请帮我制作一份清单。任何帮助都是值得赞赏的。谢谢

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

您可以使用ListView库创建ImageView TextViewVolley。在这里,我找到了制作它的教程。

Using Volley Library

Without Volley Library

然后,您必须有创意将其实施到您的项目中。