我正在使用列表视图来显示 res/drawable 文件夹中存在的一组图像。为此,我创建了一个适配器类,它将填充(main.xml)。我已经编写了这样的代码,如果单击 listItem,图像应该在新活动中全屏显示。我还需要包括图像缩放功能。你能帮他把缩放功能附加到下面的代码中吗?
MainActivity.java
public class MainActivity extends ListActivity
{
static final String[] Furniture_types = new String[] { "Type1", "Type2","Type3", "Type4"};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new FurnitureArrayAdapter(this,Furniture_types ));
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
Intent myIntent = new Intent();
myIntent.setClassName("com.mink7.furniture.list","com.mink7.furniture.list.ImageDisplayActivity");
myIntent.putExtra("ID", id);
startActivity(myIntent);
}
}
FurnitureArrayAdapter.java
public class FurnitureArrayAdapter extends ArrayAdapter<String>
{
private final Context context;
private final String[] values;
public FurnitureArrayAdapter(Context context, String[] values)
{
super(context, R.layout.activity_main, values);
this.context = context;
this.values = values;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.type);
ImageView imageView = (ImageView) rowView.findViewById(R.id.photo);
textView.setText(values[position]);
String s = values[position];
System.out.println(s);
if (s.equals("Type1")) {
imageView.setImageResource(R.drawable.img1);
} else if (s.equals("Type2")) {
imageView.setImageResource(R.drawable.img2);
} else if (s.equals("Type3")) {
imageView.setImageResource(R.drawable.img3);
} else {
imageView.setImageResource(R.drawable.img4);
}
return rowView;
}
}
ImageDisplayActivity.java
public class ImageDisplayActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imagedisp);
Bundle extras = getIntent().getExtras();
long id =extras.getLong("ID");
String s=String.valueOf(id);
ImageView image = (ImageView) findViewById(R.id.imageView1);
System.out.println(s);
if (s.equals("0"))
{
image.setImageResource(R.drawable.img1);
}
else if (s.equals("1"))
{
image.setImageResource(R.drawable.img2);
}
else if (s.equals("2"))
{
image.setImageResource(R.drawable.img3);
}
else
{
image.setImageResource(R.drawable.img4);
}
}
}