我正在尝试通过xml文件创建图像。我想问一下是否可以这样做
以下是我想要创建为图像的XML文件的代码,以便在imageview中显示。
主要活动
@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) {
}
}
showimagetask
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>
答案 0 :(得分:0)
我不知道你的理由是什么。
如果您要做的只是将一些View结构显示为ImageView内部的图像,那么有一种更简单的方法。
使用LayoutInflator正常膨胀TableLayout。一旦你有了它的引用,你可以调用getDrawingCache()来获取一个可以传递给ImageView的位图对象。
Bitmap mImg = tbl.getDrawingCache();
mImgView.setImageBitmap(mImg);