标签:android blog http io ar os java 文件 on
(1)加载图片和浏览图片的布局文件
图片预先放置在这里。
主要的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" 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=".MainActivity" > <LinearLayout android:id="@+id/L1" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/p1" /> <Button android:id="@+id/b2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/p2" /> </LinearLayout> <ImageButton android:id="@+id/I1" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageButton> </LinearLayout>
(2)主要控制代码,点击下一张,上一张浏览图片
package com.example.tupian; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { ImageView iv; int count; int draeableIds[]={ R.drawable.pic0, R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7, R.drawable.pic8, R.drawable.pic9 , }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv=(ImageView)findViewById(R.id.I1); iv.setImageResource(R.drawable.no0); Button b2=(Button)findViewById(R.id.b2); Button b1=(Button)findViewById(R.id.b1); b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(count>0) { iv.setImageResource(draeableIds[--count]); } else Toast.makeText(MainActivity.this, "这是第一幅", Toast.LENGTH_SHORT).show(); } }); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(count<draeableIds.length-1) { iv.setImageResource(draeableIds[++count]); } else Toast.makeText(MainActivity.this, "这是最后一幅", Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
标签:android blog http io ar os java 文件 on
原文地址:http://blog.csdn.net/liuzuyi200/article/details/40951397