0

我正在尝试通过 xml 文件创建图像。我想问是否可以这样做

以下是我想创建为图像以便在图像视图中显示的 XML 文件的代码。

主要活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
spinner2 = (Spinner) findViewById(R.id.spinner1);
imageshow =(ImageView) findViewById(R.id.imageshow);
items = fillMaps();

        SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.spinner,
                new String[]{"name"},
                new int[]{R.id.title});

        spinner2.setAdapter(adapter);

        spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener());

        spinner2.setVisibility(View.VISIBLE);}

布局/main.xml

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

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bible_help_title" />


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
         android:layout_below="@id/bible_help_title">


    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:prompt="@string/bible_help_title"
        android:drawSelectorOnTop="true"
        />
</RelativeLayout>


    <ImageView 
        android:id="@+id/imageshow"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_below="@id/layout_1"/>  

</RelativeLayout>

用户从 Spinner 中选择资源后,调用 showimagetask

public class MyOnItemSelectedListener implements OnItemSelectedListener {
            public void onItemSelected(AdapterView<?> arg0, View arg1, int `arg2,long arg3) {`



                new showimagetask(arg2).execute(arg2);


                //if (!snoop.isRecycled()) { snoop.recycle();}

            }

            public void onNothingSelected(AdapterView<?> arg0) {

            }
}

显示图像任务

   public class showimagetask extends AsyncTask<Integer, Void, Bitmap> 
        {
    ProgressDialog dialog;
    int checkid;

    String Map_name;
    String Drawing_1;
    int resID;

     public showimagetask(int id) {
            checkid = id;

            HashMap map = (HashMap)items.get(id);
            Map_name= map.get("name").toString();
            Drawing_1= map.get("Drawing").toString();
            resID = getResources().getIdentifier(Drawing_1, "raw", "tool.mobile");  



        }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        dialog =new ProgressDialog(BibleHelpActivity.this);
        dialog.setTitle(Map_name);
        dialog.setMessage("Loading");
        dialog.show();

        imageshow.setImageBitmap(null);
        snoop=null;
        System.gc();



    }


    @Override
    protected Bitmap doInBackground(Integer... params) {
        // TODO Auto-generated method stub
        snoop= BitmapFactory.decodeStream(getResources().openRawResource(resID));
        //Bitmap snoop = BitmapFactory.decodeResource(getResources(), resID);

        return snoop;
    }
    protected void onPostExecute(Bitmap result) {
        /*
        TouchImageView imageshow = imageButtonReference.get();
         */

        imageshow.setImageBitmap(snoop);
        imageshow.setTag(resID);
        dialog.dismiss();


     }

}


private List<HashMap<String, String>> fillMaps()

{
    List<HashMap<String, String>> items = new ArrayList<HashMap<String,String>>();

    HashMap<String,String> i = new HashMap<String,String>();
        i.put("name","EricPhoto");
        i.put("Drawing", "img_101");
        items.add(i);
                    i = new HashMap<String,String>();
                    i.put("name","PeterPhoto");
            i.put("Drawing", "img_102");
            items.add(i);
        i = new HashMap<String,String>();
        i.put("name","Schedule");
        i.put("Drawing", "table_1");
        items.add(i);
return items;
    }

原始文件夹中的文件

img_101.jpg
img_102.jpg
table_1.xml

原始文件夹中 table_1.xml 的代码

 <?xml version="1.0" encoding="utf-8"?>
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
             android:background="@drawable/white"
             android:orientation="vertical" 
            >
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10" />
            <TextView 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Eric Graduation" /> 
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="17" />
            <TextView 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="peter birthday" /> 
        </TableRow>
    </TableLayout>
4

1 回答 1

0

我不知道你这样做的理由是什么。

如果您的目标是在 ImageView 中显示一些 View 结构作为图像,那么有一种更简单的方法来实现它。

使用 LayoutInflator 像往常一样为您的 TableLayout 充气。一旦你有一个对它的引用,你就可以调用getDrawingCache()来获取一个可以传递给 ImageView 的位图对象。

Bitmap mImg = tbl.getDrawingCache();
mImgView.setImageBitmap(mImg);
于 2012-06-01T15:10:32.767 回答