Android 简单图片浏览器 ImageView

main.xml文件:



	
    
    




    


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(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(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")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(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;
    }
    
}

效果图:

Android 简单图片浏览器 ImageView_第1张图片Android 简单图片浏览器 ImageView_第2张图片

你可能感兴趣的:(android,textview)