图片浏览器

摘要:
创建三种图片浏览器:1.使用ImageViewstep1:创建工程ImagesShow1,其中main.xml文件内容如下:main.xml12671319253137383946475354step2:ImageShow1.javaImageShow11packagecom.cb.imagesshow;23importandroid.app.Activity;4importandroid.graphics.Bitmap;5importandroid.graphics.BitmapFactory;6importandroid.graphics.drawable.BitmapDrawable;7importandroid.os.Bundle;8importandroid.view.MotionEvent;9importandroid.view.View;10importandroid.view.View.OnClickListener;11importandroid.view.View.OnTouchListener;12importandroid.widget.Button;13importandroid.widget.ImageView;1415/*16*这个图片浏览器使用ImageView完成,功能:局部放大.17*存在的问题是:getWidth()获取了整个显示界面的宽,导致鼠标点击处获取的图片与局部放大中显示的图片位置有差异。

创建三种图片浏览器:

1.使用ImageView

step1:创建工程ImagesShow1,其中main.xml文件内容如下:

图片浏览器第1张图片浏览器第2张main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6     
7     <LinearLayout 
8         android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:orientation="horizontal"
11 android:gravity="center_horizontal"
12         >
13         <Button 
14             android:id="@+id/plus"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:text="@string/plus"
18             />
19         <Button 
20             android:id="@+id/minus"
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:text="@string/minus"
24             />
25         <Button 
26             android:id="@+id/prior"
27 android:layout_width="wrap_content"
28 android:layout_height="wrap_content"
29 android:text="@string/prior"
30             />
31         <Button 
32             android:id="@+id/next"
33 android:layout_width="wrap_content"
34 android:layout_height="wrap_content"
35 android:text="@string/next"
36             />
37         </LinearLayout>
38     
39     <ImageView 
40         android:id="@+id/image1"
41 android:layout_width="fill_parent"
42 android:layout_height="300dp"
43 android:src="@drawable/baiyang"
44 android:background="#ffffff"
45         />
46     
47     <ImageView 
48         android:id="@+id/image2"
49 android:layout_width="120dp"
50 android:layout_height="120dp"
51 android:background="#ffffff"
52         />
53 
54 </LinearLayout>

step2:ImageShow1.java

图片浏览器第3张图片浏览器第4张ImageShow1
1 packagecom.cb.imagesshow;
2 
3 importandroid.app.Activity;
4 importandroid.graphics.Bitmap;
5 importandroid.graphics.BitmapFactory;
6 importandroid.graphics.drawable.BitmapDrawable;
7 importandroid.os.Bundle;
8 importandroid.view.MotionEvent;
9 importandroid.view.View;
10 importandroid.view.View.OnClickListener;
11 importandroid.view.View.OnTouchListener;
12 importandroid.widget.Button;
13 importandroid.widget.ImageView;
14 
15 /*
16 * 这个图片浏览器使用 ImageView 完成,功能:局部放大.
17 * 存在的问题是:getWidth()获取了整个显示界面的宽,导致鼠标点击处获取的图片与局部放大中显示的图片位置有差异。怎么解决呢???求助。
18  */
19 
20 public class ImagesShow1 extendsActivity {
21     private int[] imageIds = new int[]{
22 R.drawable.baiyang,R.drawable.chunv,R.drawable.jinniu,
23 R.drawable.juxie,R.drawable.mojie,R.drawable.sheshou,
24 R.drawable.shizi,R.drawable.shuangyu,R.drawable.shuangzi,
25 R.drawable.shuiping,R.drawable.tiancheng,R.drawable.tianxie
26 };
27     private int currentImg = 0;
28     private int alpha = 255;
29     
30     privateButton plus;
31     privateButton minus;
32     privateButton prior;
33     privateButton next;
34     
35     privateImageView image1;
36     privateImageView image2;
37     
38 @Override
39     public voidonCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41 setContentView(R.layout.main);
42         
43         plus =(Button)findViewById(R.id.plus);
44         minus =(Button)findViewById(R.id.minus);
45         prior =(Button)findViewById(R.id.prior);
46         next =(Button)findViewById(R.id.next);
47         
48         image1 =(ImageView)findViewById(R.id.image1);
49         image2 =(ImageView)findViewById(R.id.image2);
50 
51         //定义查看上/下一张图片的监听器
52         OnClickListener listener1 = newOnClickListener() {
53 @Override
54             public voidonClick(View v) {
55                 if (v ==prior) {
56                     if (currentImg <= 0) {
57                         currentImg = 12;
58 }
59 }
60                 if (v ==next) {
61                     if (currentImg >= 11) {
62                         currentImg = -1;
63 }
64 }
65                 BitmapDrawable bitmapDrawable =(BitmapDrawable)image1.getDrawable();
66                 //如果图片还未回收,先强制回收该图片
67                 if (!bitmapDrawable.getBitmap().isRecycled()) {
68 bitmapDrawable.getBitmap().recycle();
69 }
70                 //改变image1显示的图片
71                 if (v ==prior) {
72                     image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), imageIds[--currentImg]));
73 }
74                 else if(v ==next){
75                     image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), imageIds[++currentImg]));
76 }
77 }
78 };
79 prior.setOnClickListener(listener1);
80 next.setOnClickListener(listener1);
81         
82         //定义增大/减小透明度的监听器
83         OnClickListener listener2 = newOnClickListener() {
84 @Override
85             public voidonClick(View v) {
86                 if (v ==plus) {
87                     alpha += 20;
88 }
89                 if (v ==minus) {
90                     alpha -= 20;
91 }
92                 if (alpha > 255) {
93                     alpha =255;
94 }
95                 if (alpha < 0) {
96                     alpha = 0;
97 }
98 image1.setAlpha(alpha);
99 }
100 };
101 plus.setOnClickListener(listener2);
102 minus.setOnClickListener(listener2);
103         
104         //设置在图片上点击的监听器
105         image1.setOnTouchListener(newOnTouchListener() {
106 @Override
107             public booleanonTouch(View v, MotionEvent event) {
108                 BitmapDrawable bitmapDrawable =(BitmapDrawable)image1.getDrawable();
109                 //获取第一个图片显示框中的位图
110                 Bitmap bitmap =bitmapDrawable.getBitmap();
111                 //bitmap图片实际大小与第一个ImageView的缩放比例
112                 double scale = bitmap.getWidth()/320.0;
113                 int x = (int)(event.getX() *scale);
114                 int y = (int)(event.getY() *scale);
115                 if (x > bitmap.getWidth() - 120) {
116                     x = bitmap.getWidth() - 120;
117 }
118                 if (y > bitmap.getHeight() - 120) {
119                     y = bitmap.getHeight() - 120;
120 }
121                 image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));
122 image2.setAlpha(alpha);
123                 return false;
124 }
125 });
126 }
127 }

step3:效果如图

图片浏览器第5张

2.使用GridView和ImageSwitcher

step1:创建工程ShowImages,其中main.xml文件内容如下:

图片浏览器第6张图片浏览器第7张main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" 
6 android:gravity="center_horizontal">
7 
8     <GridView 
9         android:id="@+id/grid01"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 
13 android:numColumns="4"
14 android:gravity="center"
15         />
16     <ImageSwitcher 
17         android:id="@+id/switcher"
18 android:layout_width="320dp"
19 android:layout_height="320dp"
20 android:gravity="center_horizontal"
21         />
22     
23         
24 </LinearLayout>

另外,SimpleAdapter中的布局文件cell.xml为:

图片浏览器第8张图片浏览器第9张cell.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="horizontal" 
6 android:gravity="center">
7 
8     <ImageView
9         android:id="@+id/image1" 
10 android:layout_width="40dp"
11 android:layout_height="40dp"
12         />
13         
14 </LinearLayout>

step2:ShowImages.java

图片浏览器第10张图片浏览器第11张ShowImages
1 packagecom.cb.showimages;
2 
3 importjava.util.ArrayList;
4 importjava.util.HashMap;
5 importjava.util.List;
6 importjava.util.Map;
7 
8 importandroid.R.anim;
9 importandroid.app.Activity;
10 importandroid.os.Bundle;
11 importandroid.view.View;
12 importandroid.view.ViewGroup.LayoutParams;
13 importandroid.view.animation.Animation;
14 importandroid.view.animation.AnimationUtils;
15 importandroid.widget.AdapterView;
16 importandroid.widget.AdapterView.OnItemClickListener;
17 importandroid.widget.AdapterView.OnItemSelectedListener;
18 importandroid.widget.GridView;
19 importandroid.widget.ImageSwitcher;
20 importandroid.widget.ImageView;
21 importandroid.widget.SimpleAdapter;
22 importandroid.widget.ViewSwitcher.ViewFactory;
23 /*
24 * This project uses GridView and ImageSwitcher.
25 * 功能:大图预览
26  */
27 public class ShowImages extendsActivity {
28     private int[] imageIds = new int[]{
29 R.drawable.sunset,R.drawable.water,R.drawable.winter,R.drawable.sunset,
30 R.drawable.water,R.drawable.winter,R.drawable.sunset,R.drawable.water,
31 R.drawable.winter,R.drawable.sunset,R.drawable.water,R.drawable.winter,
32 R.drawable.sunset,R.drawable.water,R.drawable.winter,R.drawable.sunset
33 };
34     
35     privateGridView mGridView;
36     privateImageSwitcher switcher;
37     
38 @Override
39     public voidonCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41 setContentView(R.layout.main);
42         
43         /*
44 * 1.创建List对象集合,其元素为Map对象
45 * 2.使用Map对象来存储单个元素--图像
46 * 3.将Map对象存入List对象中
47          */
48         List<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>();
49         for (int i = 0; i < imageIds.length; i++) {
50             Map<String, Object> listItem = new HashMap<String, Object>();
51             listItem.put("images", imageIds[i]);
52 listItems.add(listItem);
53 }
54 
55         switcher =(ImageSwitcher)findViewById(R.id.switcher);
56         //设置图片更换的动画效果
57         switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
58         switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
59         
60         //为ImageSwitcher设置图片切换的动画效果。ImageSwitcher对象负责显示makeView()方法返回的imageView对象.
61         switcher.setFactory(newViewFactory() {
62             
63 @Override
64             publicView makeView() {
65                 ImageView imageView = new ImageView(ShowImages.this);
66                 imageView.setBackgroundColor(0xff0000);
67 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
68                 imageView.setLayoutParams(newImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
69                 returnimageView;
70 }
71 });
72         //创建SimpleAdapter
73         SimpleAdapter simpleAdapter = newSimpleAdapter(
74                 this, 
75 listItems, 
76                 R.layout.cell, //该布局定义List对象中各个item的布局 
77                 new String[]{"images"}, 
78                 new int[]{R.id.image1}
79 );
80         mGridView =(GridView)findViewById(R.id.grid01);
81 mGridView.setAdapter(simpleAdapter);
82         
83         //定义列表项被单击的监听器
84         mGridView.setOnItemClickListener(newOnItemClickListener() {
85 
86 @Override
87             public void onItemClick(AdapterView<?>parent, View view,
88                     int position, longid) {
89                 //显示当前被选中的图片
90                 switcher.setImageResource(imageIds[position %imageIds.length]);
91 }
92 });
93         //定义列表项被选中的监听器
94         mGridView.setOnItemSelectedListener(newOnItemSelectedListener() {
95 
96 @Override
97             public void onItemSelected(AdapterView<?>parent, View view,
98                     int position, longid) {
99                 switcher.setImageResource(imageIds[position %imageIds.length]);
100 }
101 
102 @Override
103             public void onNothingSelected(AdapterView<?>parent) {}
104             
105 });
106         
107 }
108 }

step3:效果如图:

图片浏览器第12张

3.使用Gallery和ImageSwitcher

step1:创建工程GalleryTest,其中main.xml文件内容如下:

图片浏览器第13张图片浏览器第14张main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent">
6 <!--定义一个ImageSwitcher组件 -->
7 <ImageSwitcher android:id="@+id/switcher"
8 android:layout_width="320dp"
9 android:layout_height="320dp"
10     />
11 <!--定义一个Gallery组件 -->
12 <Gallery android:id="@+id/gallery"
13 android:layout_width="fill_parent"
14 android:layout_height="wrap_content"
15 android:layout_marginTop="25dp" 
16 android:unselectedAlpha="0.6"
17 android:spacing="3pt"
18     />    
19 </LinearLayout>

step2:GalleryTest.java

图片浏览器第15张图片浏览器第16张GalleryTest
1 packageorg.crazyit.gallery;
2 
3 importandroid.app.Activity;
4 importandroid.content.res.TypedArray;
5 importandroid.os.Bundle;
6 importandroid.view.View;
7 importandroid.view.ViewGroup;
8 importandroid.view.animation.AnimationUtils;
9 importandroid.widget.AdapterView.OnItemSelectedListener;
10 importandroid.widget.AdapterView;
11 importandroid.widget.BaseAdapter;
12 importandroid.widget.Gallery;
13 importandroid.widget.ImageSwitcher;
14 importandroid.widget.ImageView;
15 importandroid.widget.Gallery.LayoutParams;
16 importandroid.widget.ViewSwitcher.ViewFactory;
17 
18 public class GalleryTest extendsActivity
19 {
20     int[] imageIds = new int[]
21 {
22 R.drawable.shuangzi, R.drawable.shuangyu, R.drawable.chunv, R.drawable.tiancheng, 
23 R.drawable.tianxie, R.drawable.sheshou, R.drawable.juxie, R.drawable.shuiping,
24 R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu, R.drawable.mojie 
25 };
26     
27     privateGallery gallery;
28     privateImageSwitcher switcher;
29     
30 @Override
31     public voidonCreate(Bundle savedInstanceState)
32 {
33         super.onCreate(savedInstanceState);
34 setContentView(R.layout.main);
35 
36         gallery =(Gallery) findViewById(R.id.gallery);
37         switcher =(ImageSwitcher)findViewById(R.id.switcher);
38 
39         switcher.setFactory(newViewFactory()
40 {
41 @Override
42             publicView makeView()
43 {
44                 ImageView imageView = new ImageView(GalleryTest.this);
45                 imageView.setBackgroundColor(0xff0000);
46 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
47                 imageView.setLayoutParams(newImageSwitcher.LayoutParams(
48 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
49                 returnimageView;
50 }
51 });
52         //设置图片更换的动画效果
53         switcher.setInAnimation(AnimationUtils.loadAnimation(this,
54 android.R.anim.fade_in));
55         switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
56 android.R.anim.fade_out));
57         //创建一个BaseAdapter对象,该对象负责提供Gallery所显示的图片
58         BaseAdapter adapter = newBaseAdapter()
59 {
60 @Override
61             public intgetCount()
62 {
63                 returnimageIds.length;
64 }
65 @Override
66             public Object getItem(intposition)
67 {
68                 returnposition;
69 }
70 @Override
71             public long getItemId(intposition)
72 {
73                 returnposition;
74 }
75 
76             //该方法的返回的View就是代表了每个列表项
77 @Override
78             public View getView(intposition, View convertView, ViewGroup parent)
79 {
80                 //创建一个ImageView
81                 ImageView imageView = new ImageView(GalleryTest.this);
82                 imageView.setImageResource(imageIds[position %imageIds.length]);
83                 //设置ImageView的缩放类型
84 imageView.setScaleType(ImageView.ScaleType.FIT_XY);
85                 imageView.setLayoutParams(new Gallery.LayoutParams(75, 100));
86                 TypedArray typedArray =obtainStyledAttributes(R.styleable.Gallery);
87                 imageView.setBackgroundResource(typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0));
88                 returnimageView;
89 }
90 };
91 gallery.setAdapter(adapter);
92         gallery.setOnItemSelectedListener(newOnItemSelectedListener()
93 {
94             //当Gallery选中项发生改变时触发该方法
95 @Override
96             public void onItemSelected(AdapterView<?>parent, View view,
97                 int position, longid)
98 {
99                 switcher.setImageResource(imageIds[position %imageIds.length]);
100 }
101 
102 @Override
103             public void onNothingSelected(AdapterView<?>parent){}
104 });
105 }
106 }

step3:效果如图:

图片浏览器第17张

免责声明:文章转载自《图片浏览器》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇web自动化多次打开浏览器嫌烦?打开一次浏览器,pytest有个招notepad++快捷键大全下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

pl/sql中的取模运算

pl/sql语言的取模(即求余)运算不使用大部分语言所支持的a%b而是使用函数mod(a,b)例子如下:写一个匿名块判断某年是否是闰年,能被4但是不能被100整除,或者能被400整除1declare2judgevarchar2(200);3year_inputnumber;4begin5year_input:='&输入年份';6if(mod(year...

css动画延迟好像有点怪

项目需要使用动画Css。自定义时,会发现设置动画延迟和动画持续时间的总时间不正确,这将导致动画丢失。例如,bounceInLeft动画从左侧出现,然后抖动。当初始动画延迟为0时,动画持续时间为1s,动画已完成,但如果设置该值,动画延迟为1s且动画持续时间是2s,则动画未完成。具体的动画是从左侧出现,然后在1s延迟后直接到达终点,但没有抖动。然后我用w3c写了...

OA办公系统 Springboot vue.js 前后分离 跨域 Flowable 工作流

1.模型管理:web在线流程设计器,预览流程xml,导出xml,部署流程2.流程管理:导入和导出流程资源文件,查看流程图,根据流程实例反映流程模型,激活和挂起,自由跳转3.运行流程:查看流程信息,当前任务节点,当前流程图,作废和挂起流程,并分配待办事项人员4.历史流程:查看流程信息、流程时间流程状态、查看任务发起人信息5.待办任务:查看您的个人任务和此角色下...

rz上传文件及出错解决方案

原始链接:https://blog.csdn.net/yjk13703623757/article/details/87083997单独使用rz命令时有两个问题:上载中断和文件更改。解决方案是使用rz be进行上传,并在弹出对话框中删除“UploadfilesasASCII”之前的复选框。如果使用不带参数的rz命令上传一个大文件,则在上传过程中通常会中断。很...

Python生成pyd文件

Python的脚本文件是开源的,量化策略的安全性没有保障。那么要对Python代码进行混淆、加密保护。Python有py、pyc、pyw、pyo、pyd等文件格式。vcvarsall.bat是VC编译Python环境的文件之一。方案1:修改Python安装目录的文件设置方案2:修改注册表我采用方案1,亲测可用。测试结果,用py2exe可以正常使用pyd文件。...

SQL中一次插入多条数据

SQL中insert一次可以插入一条数据,我们有三种方法可以一次性插入多条数据。在此处还有一些有趣的问题,当我使用以下代码来插入多条数据时:selecttop0*intonewstudentfromstudentinsertintonewstudentselect*fromstudent这里会发生这样的报错:因为NewClass表中ClassId为标识列,所...