Android 简单图片浏览器 ImageView

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>
    
<LinearLayout
	android: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(){

			@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;
    }
    
}

效果图:


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值