专业相框设计(ImageView的堆栈应用)
新建一个继承Activity类的ImageViewStackActivity,并设置布局文件为:imageviewstack.xml。
在布局文件中添加一个ImageView和1个Button组件。
<Button android:id="@+id/imageviewstack_btn01" style="@android:style/Widget.Button.Inset" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/next" />
<ImageView android:id="@+id/imageviewstack_image01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> |
当点击按钮后显示下一张图片。
package lyx.feng.second; ...... public class ImageViewStackActivity extends Activity { private Button btn = null; private ImageView image = null; private List<Integer> images = new ArrayList<Integer>(); private int current = 0;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.imageviewstack); this.btn = (Button) super.findViewById(R.id.imageviewstack_btn01);
Field field[] = R.drawable.class.getDeclaredFields(); for (int i = 0; i < field.length; i++) { if (field[i].getName().startsWith("icon_")) { try { images.add(field[i].getInt(R.drawable.class)); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
this.image = (ImageView) super .findViewById(R.id.imageviewstack_image01); this.btn.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) { image.setImageResource(images.get(current % images.size())); current++; } }); }
}
|