Android 简单图片浏览器 ImageView

来源:互联网 发布:淘宝网儿童玩具手推车 编辑:程序博客网 时间:2024/05/01 19:37

main.xml文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:background="#66FFCC" android:layout_width="fill_parent"android:layout_height="fill_parent">    <LinearLayout     android:layout_width="fill_parent"    android:layout_height="400dp"    android:orientation="vertical"    android:gravity="center_horizontal"    >    <!-- 定义显示图片整体的ImageView --><ImageView    android:id="@+id/image1"    android:layout_width="fill_parent"    android:layout_height="match_parent"    android:scaleType="fitCenter"    android:src="@drawable/shuangta" /></LinearLayout>    <LinearLayoutandroid:orientation="horizontal" android:layout_marginTop="3dp"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"><Button android:id="@+id/plus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="增大透明度"/><Button android:id="@+id/minus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="降低透明度"/><Button android:id="@+id/next"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="下一张"/></LinearLayout></LinearLayout>

MainActivity.java文件:

public class MainActivity extends Activity {//定义一个访问图片的数组int [] images = new int[]{R.drawable.lijiang,R.drawable.qiao,R.drawable.shuangta,R.drawable.shui,R.drawable.xiangbi,};//定义默认显示的图片int currentImg = 2;//定义图片的初始透明度private int alpha = 255;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        this.requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.main);        final Button plus = (Button) findViewById(R.id.plus);        final Button minus = (Button) findViewById(R.id.minus);        final Button next = (Button) findViewById(R.id.next);                final ImageView image1 = (ImageView) findViewById(R.id.image1);                //定义查看下一张图片的监听器        next.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(currentImg >= 4) currentImg = -1;BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();//如果图片还未回收,先强制回收图片if(!bitmapDrawable.getBitmap().isRecycled()){bitmapDrawable.getBitmap().isRecycled();}image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), images[++currentImg]));}        });                OnClickListener listener = new OnClickListener(){@SuppressWarnings("deprecation")@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v == plus) alpha += 50;if(v == minus) alpha -= 50;if(alpha >= 255) alpha = 255;if(alpha <= 0) alpha = 0;//改变图片的透明度image1.setAlpha(alpha);}        };                plus.setOnClickListener(listener);        minus.setOnClickListener(listener);            }    @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;    }    }

效果图:


0 0