我是靠谱客的博主 微笑发箍,这篇文章主要介绍Android 简单图片浏览器 ImageView,现在分享给大家,希望可以做个参考。

main.xml文件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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文件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部