Android · 2014年12月18日 0

AndroidUI设计 之 图片浏览器

图片浏览器效果图 :

源码下载地址 :

CSDN : http://download.csdn.net/detail/han1202012/6875083

GitHub : https://github.com/han1202012/AndroidPictureViewer.git

.

作者 :万境绝尘 

转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835

.

 

一. 图片浏览器显示界面ImageView介绍

1. ImageView的上下继承结构

下面是API中的结构:

java.lang.Object
   ↳ android.view.View
android.widget.ImageView
Known Direct Subclasses

Known Indirect Subclasses

 

绘制成UML图 :

通过上面的分析 : ImageView有两个子类 ImageButton 和 QuickContactBadge, ImageButton还有一个子类是 ZoomButton;

2. XML文件属性

调整边界, 保持长宽比 :android:adjustViewBounds, setAdjustViewBounds(boolean), 是否调整自己的边界, 用来保持图片的长宽比例, 该属性与 android:maxHeight 和 android:maxWidth 属性一起使用才有效果, 单独使用没有效果;

设置最大宽度, 高度 :android:maxWidth(android:maxHeight), setMaxWidth(int)[setMaxHeight(int)], 该属性需要与android:adjustViewBounds属性一起使用,单独使用无效果;

设置图片固定大小, 同时保持长宽比 : a. 设置android:adjustViewBounds 为 true; b. 设置最大宽度, 高度; c. 设置android:layout_width 与 android:layout_height 值为 warp_content;

裁剪保留空白 :android:cropToPadding, setCropToPadding(boolean), 是否裁剪, 用来保留ImageView的padding, 该属性与android:scrollY 属性一起使用的时候才有用, 单独使用没有效果; 即 在滚动的时候, 滚动到边界, 边界的padding空白是否显示;

填充方式 :android:scaleType, setScaleType(ImageView.ScaleType), 设置图片缩放类型以适配ImageView大小, 即填充方式;

可能的取值 : matrix, fitXY, fitStart, fitCenter, fitEnd, center, centerCrop, centerInside;

matrix : 方法中的常量值为 ImageView.ScaleType.MATRIX, 使用矩阵来进行绘图;

fitXY : 方法中的常量值为 ImageView.ScaleType.FIT_XY, 在x y 两个方向上缩放, 使图片完全填充整个ImageView 不按照长宽比例缩放;

fitStart : 方法中的常量值为 ImageView.ScaleType.FIT_START, 保持长宽比缩放, 直到该图片完全显示在ImageView中, 缩放完成之后该图片在左上角;

fitCenter : 方法中的常量值为 ImageView.ScaleType.FIT_CENTER, 保持长宽比缩放, 直到该图片完全显示在ImageView中, 缩放完成之后该图片位于中央;

fitEnd : 方法中的常量值为 ImageView.ScaleType.FIT_END, 保持长宽比缩放, 直到该图片完全显示在ImageView中, 缩放完成之后该图片位于右下角;

center : 方法中的常量值为 ImageView.ScaleType.CENTER, 将图片放在ImageView的中央, 不进行缩放;

centerCrop : 方法中的常量值为 ImageView.ScaleType.CENTER_CROP, 保持长宽比缩放, 使图片完全覆盖ImageView;

centerInside : 方法中的常量值为 ImageView.ScaleType.CENTER_INSIDE, 保持长宽比缩放, 是的ImageView完全显示图片;

实例 :

XML文件 :

  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:orientation=“vertical”>
  6.     <ImageView
  7.         android:id=“@+id/im”
  8.         android:background=“#00FF00”
  9.         android:layout_height=“300dp”
  10.         android:layout_width=“300dp”
  11.         android:src=“@drawable/pic”
  12.         <strong>android:scaleType</strong>=”matrix”/>
  13. </LinearLayout>

修改其中的 android:scaleType属性值, 查看其中的差异 :

原图 :

android:scaleType 默认情况下 :

android:scaleType = “matrix” : 由下图可以看出, ImageView中的图片宽度与原图一样, 该属性不进行任何缩放,直接将图片放在左上角;

android:scaleType = “fixXY” : 长宽不按比例拉伸, 图片明显变形 :

android:scaleType = “fitStart” , 图片按比例缩放, 宽先达到边界, 图片位于上边; 如果高先达到边界, 图片位于左边;

android:scaleType = “fieCenter” ,长宽按照比例缩放, 宽度先达到边界, 上下有空白; 如果高度先达到边界, 那么左右有空白;

android:scaleType = “fitEnd” , 长宽等比例缩放, 宽度先达到边界, 位于下边; 如果高度先达到边界, 位于右边;

android:scaleType = “center” ,长宽不进行缩放, 图片的中心 与 ImageView 的中心重合;

android:scaleType = “centerCrop” ,长宽等比例缩放, 使图片完全覆盖ImageView, 图片中心与ImageView中心重合, 使图片最短的边能覆盖ImageView边界;

android:scaleType = “centerInside” ,长宽等比例缩放, 如果图片宽高小于等于ImageView宽高, 那么就按照原图大小显示; 如果图片大于ImageView, 那么按照等比例缩小直到能完全显示为止;

3. ImageView常用方法

设置图片 :

设置位图 : setImageBitmap(bitmap), 为ImageView设置Bitmap位图显示;

设置Drawable : setImageDrawable(drawable), 为ImageView设置Drawable显示;

设置资源 : setImageResource(int), 为ImageView设置资源图片;

设置路径 : setImageUri(uri), 为ImageView设置图片路径, 显示该路径的图片;

二. 图片浏览器操作介绍

1. 实现左右循环切换图片

图片数组 : 将图片放在数组中, ImageView显示数组中的图片;

当前显示图片下标索引 : 设置一个int值, 用来表示当前显示图片数组中的图片, 这个值不是int下标, 这个值设置很大设置成Integer.MAXVALUE / 2, 该值与图片数组的长度进行取模运算结果就是当前显示的图片数组下标值;

翻页操作 : 上一页操作就将当前显示索引自减1, 然后模上 图片数组大小; 下一页就将当前索引自增1, 然后模上 图片数组大小;

代码示例 :

  1. //设置一个很大的值, 保证前后翻页不会出现异常
  2. currentImage = Integer.MAX_VALUE / 2;
  3. //为了保证图片能够循环, 这里模运算是关键, 显示图片的下标始终是长度的模
  4. image_all.setImageResource(images[ ++currentImage % images.length ]);
  5. image_all.setImageResource(images[ –currentImage % images.length ]);

 

2. 透明度改变

设置当前透明度 : 设置一个当前透明度值, 初始值为255, 255是不透明, 0为完全透明;

透明度改变 : 当点击透明度增加按钮的时候, 透明度自增20, 如果结果透明度大于255, 那么改透明度强制设置为255; 当点击透明度见效按钮的时候, 透明度自减20, 当透明度小于0的时候, 透明度强制设置为0;

代码示例 :

  1. //透明度初始值
  2. alpha = 255;
  3. //透明度增加
  4. alpha += 20;
  5. if(alpha >= 255)
  6.     alpha = 255;
  7. image_all.setAlpha(alpha);
  8. //透明度减小
  9. alpha -= 20;
  10. if(alpha <= 0)
  11.     alpha = 0;
  12. image_all.setAlpha(alpha);

 

3. 图片的放大缩小

获取View组件宽高 : 在Activity普通方法中无法获取到view组件的准确值, 如果想要获取view组件的宽高, 可以在 onWindowFocusChanged()方法中获取;

计算每次自增自减的单位值 : 当按下缩放按钮的时候, 就对ImageView的宽高值进行自增自减单位值操作;

为ImageView设置宽高 : 即设置LayoutParams, 注意是LinearLayout.LayoutParams对象;

代码示例 :

获取宽高 :

  1. @Override
  2. public void onWindowFocusChanged(boolean hasFocus) {
  3.     // TODO Auto-generated method stub
  4.     super.onWindowFocusChanged(hasFocus);
  5.     //获取ImageView组件的宽高
  6.     imageWidth = image_all.getWidth();
  7.     imageHeight = image_all.getHeight();
  8.     //计算每次自增自减的值
  9.     addWidth = imageWidth / 5;
  10.     addHeight = imageHeight / 5;
  11. }

缩放图片操作 :

  1. case R.id.big:          //放大图片
  2.     imageWidth += addWidth;
  3.     imageHeight += addHeight;
  4.     image_all.setLayoutParams(new LinearLayout.LayoutParams(imageWidth, imageHeight));
  5.     break;
  6. case R.id.small:        //缩小图片
  7.     imageWidth -= addWidth;
  8.     imageHeight -= addHeight;
  9.     if(imageWidth <= 0 || imageHeight <=0){
  10.         imageWidth += addWidth;
  11.         imageHeight += addHeight;
  12.     }
  13.     image_all.setLayoutParams(new LinearLayout.LayoutParams(imageWidth, imageHeight));
  14.     break;

 

4. 旋转图片操作

设置Matrix对象 : 该对象用来存放图像的旋转角度;

设置旋转角度 : matrix.setRotate(), 即可设置旋转角度;

创建Bitmap : 创建一个位图, 注意将设置了旋转角度的 matrix 设置上去;

源码示例 :

  1. matrix = new Matrix();
  2. //向左旋转进行的操作
  3. anglel += 45;
  4. matrix.setRotate(anglel);
  5. Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(images[currentImage % images.length])).getBitmap();
  6. bitmap = Bitmap.createBitmap(bitmap, 00, bitmap.getWidth(),bitmap.getHeight(), matrix, true);
  7. image_all.setImageBitmap(bitmap);
  8. //向右旋转进行的操作
  9. anglel -= 45;
  10. matrix.setRotate(anglel);
  11. Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable(images[currentImage % images.length])).getBitmap();
  12. bitmap1 = Bitmap.createBitmap(bitmap1, 00, bitmap1.getWidth(),bitmap1.getHeight(), matrix, true);
  13. image_all.setImageBitmap(bitmap1);

 

.

作者 :万境绝尘 

转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835

.

 

三. 图片浏览器源码

XML布局文件 :

  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:background=“@drawable/bg_im”
  6.     android:orientation=“vertical”>
  7.     <LinearLayout
  8.         android:orientation=“horizontal”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_width=“fill_parent”
  11.         android:padding=“5dp”
  12.         android:gravity=“center”>
  13.         <Button
  14.             android:id=“@+id/alpha_plus”
  15.             android:layout_height=“fill_parent”
  16.             android:layout_width=“fill_parent”
  17.             android:layout_weight=“1”
  18.             android:text=“透明度+”
  19.             android:background=“@drawable/bg_bt”
  20.             android:onClick=“onClick”/>
  21.         <Button
  22.             android:id=“@+id/alpha_minus”
  23.             android:layout_height=“fill_parent”
  24.             android:layout_width=“fill_parent”
  25.             android:layout_weight=“1”
  26.             android:text=“透明度-“
  27.             android:background=“@drawable/bg_bt”
  28.             android:onClick=“onClick”/>
  29.         <Button
  30.             android:id=“@+id/prev_page”
  31.             android:layout_height=“fill_parent”
  32.             android:layout_width=“fill_parent”
  33.             android:layout_weight=“1”
  34.             android:text=“上一张”
  35.             android:background=“@drawable/bg_bt”
  36.             android:onClick=“onClick”/>
  37.         <Button
  38.             android:id=“@+id/next_page”
  39.             android:layout_height=“fill_parent”
  40.             android:layout_width=“fill_parent”
  41.             android:layout_weight=“1”
  42.             android:text=“下一张”
  43.             android:background=“@drawable/bg_bt”
  44.             android:onClick=“onClick”/>
  45.     </LinearLayout>
  46.     <LinearLayout
  47.         android:orientation=“horizontal”
  48.         android:layout_height=“wrap_content”
  49.         android:layout_width=“fill_parent”
  50.         android:padding=“5dp”
  51.         android:gravity=“center”>
  52.         <Button
  53.             android:id=“@+id/big”
  54.             android:layout_height=“fill_parent”
  55.             android:layout_width=“fill_parent”
  56.             android:layout_weight=“1”
  57.             android:text=“放大”
  58.             android:background=“@drawable/bg_bt”
  59.             android:onClick=“onClick”/>
  60.         <Button
  61.             android:id=“@+id/small”
  62.             android:layout_height=“fill_parent”
  63.             android:layout_width=“fill_parent”
  64.             android:layout_weight=“1”
  65.             android:text=“缩小”
  66.             android:background=“@drawable/bg_bt”
  67.             android:onClick=“onClick”/>
  68.         <Button
  69.             android:id=“@+id/turn_left”
  70.             android:layout_height=“fill_parent”
  71.             android:layout_width=“fill_parent”
  72.             android:layout_weight=“1”
  73.             android:text=“左转”
  74.             android:background=“@drawable/bg_bt”
  75.             android:onClick=“onClick”/>
  76.         <Button
  77.             android:id=“@+id/turn_right”
  78.             android:layout_height=“fill_parent”
  79.             android:layout_width=“fill_parent”
  80.             android:layout_weight=“1”
  81.             android:text=“右转”
  82.             android:background=“@drawable/bg_bt”
  83.             android:onClick=“onClick”/>
  84.     </LinearLayout>
  85.     <ImageView
  86.         android:id=“@+id/image_all”
  87.         android:layout_width=“fill_parent”
  88.         android:layout_height=“fill_parent”
  89.         android:scaleType=“fitCenter”
  90.         android:src=“@drawable/mary1”/>
  91. </LinearLayout>

Drawable资源 :

整个界面的背景 : 渐变的颜色

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <shape xmlns:android=“http://schemas.android.com/apk/res/android” >
  3.     <gradient
  4.         android:startColor=“#A9F5A9”
  5.         android:centerColor=“#2EFE2E”
  6.         android:endColor=“#A9F5A9”
  7.         android:type=“linear”/>
  8. </shape>

按钮的背景 : 两个9patch图片, 按下的时候按钮背景会改变

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <selector xmlns:android=“http://schemas.android.com/apk/res/android” >
  3.     <item android:state_pressed=“true”
  4.         android:drawable=“@drawable/bt_focus”></item>
  5.     <item android:state_pressed=“false”
  6.         android:drawable=“@drawable/bt_normal”></item>
  7. </selector>

主Activity核心代码 :

  1. package shuliang.han.imageview_test;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Matrix;
  5. import android.graphics.drawable.BitmapDrawable;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.ImageView;
  9. import android.widget.LinearLayout;
  10. public class MainActivity extends Activity {
  11.     private int[] images;       //图片资源id数组
  12.     private int currentImage;   //当前显示图片
  13.     private int alpha;          //透明度
  14.     private Matrix matrix;
  15.     private int anglel;         //角度
  16.     private int imageWidth;
  17.     private int imageHeight;
  18.     private int addWidth;
  19.     private int addHeight;
  20.     private ImageView image_all;
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.         init();
  26.     }
  27.     //初始化成员变量
  28.     private void init() {
  29.         images = new int[]{R.drawable.mary1, R.drawable.mary2};
  30.         currentImage = Integer.MAX_VALUE / 2;
  31.         alpha = 255;
  32.         matrix = new Matrix();
  33.         image_all = (ImageView) findViewById(R.id.image_all);
  34.         image_all.setImageResource(images[currentImage % images.length]);
  35.     }
  36.     public void onClick(View view) {
  37.         int id = view.getId();
  38.         switch (id) {
  39.             case R.id.alpha_plus:   //增大透明度
  40.                 alpha += 20;
  41.                 if(alpha >= 255)
  42.                     alpha = 255;
  43.                 image_all.setAlpha(alpha);
  44.                 break;
  45.             case R.id.alpha_minus:  //减小透明度
  46.                 alpha -= 20;
  47.                 if(alpha <= 0)
  48.                     alpha = 0;
  49.                 image_all.setAlpha(alpha);
  50.                 break;
  51.             case R.id.next_page:    //显示下一张图片
  52.                 //为了保证图片能够循环, 这里模运算是关键, 显示图片的下标始终是长度的模
  53.                 image_all.setImageResource(images[ ++currentImage % images.length ]);
  54.                 break;
  55.             case R.id.prev_page:    //显示上一张图片
  56.                 image_all.setImageResource(images[ –currentImage % images.length ]);
  57.                 break;
  58.             case R.id.big:          //放大图片
  59.                 imageWidth += addWidth;
  60.                 imageHeight += addHeight;
  61.                 image_all.setLayoutParams(new LinearLayout.LayoutParams(imageWidth, imageHeight));
  62.                 break;
  63.             case R.id.small:        //缩小图片
  64.                 imageWidth -= addWidth;
  65.                 imageHeight -= addHeight;
  66.                 if(imageWidth <= 0 || imageHeight <=0){
  67.                     imageWidth += addWidth;
  68.                     imageHeight += addHeight;
  69.                 }
  70.                 image_all.setLayoutParams(new LinearLayout.LayoutParams(imageWidth, imageHeight));
  71.                 break;
  72.             case R.id.turn_left:    //向左旋转
  73.                 anglel += 45;
  74.                 matrix.setRotate(anglel);
  75.                 Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(images[currentImage % images.length])).getBitmap();
  76.                 bitmap = Bitmap.createBitmap(bitmap, 00, bitmap.getWidth(),bitmap.getHeight(), matrix, true);
  77.                 image_all.setImageBitmap(bitmap);
  78.                 break;
  79.             case R.id.turn_right:   //向右旋转
  80.                 anglel -= 45;
  81.                 matrix.setRotate(anglel);
  82.                 Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable(images[currentImage % images.length])).getBitmap();
  83.                 bitmap1 = Bitmap.createBitmap(bitmap1, 00, bitmap1.getWidth(),bitmap1.getHeight(), matrix, true);
  84.                 image_all.setImageBitmap(bitmap1);
  85.                 break;
  86.             default:
  87.                 break;
  88.         }
  89.     }
  90.     @Override
  91.     public void onWindowFocusChanged(boolean hasFocus) {
  92.         // TODO Auto-generated method stub
  93.         super.onWindowFocusChanged(hasFocus);
  94.         //获取ImageView组件的宽高
  95.         imageWidth = image_all.getWidth();
  96.         imageHeight = image_all.getHeight();
  97.         //计算每次自增自减的值
  98.         addWidth = imageWidth / 5;
  99.         addHeight = imageHeight / 5;
  100.     }
  101. }

 

四. ZoomButton 和 QuickContactBadge

示例效果图 :

下载地址 :

GitHub : https://github.com/han1202012/ZoomButton_QuickContactBadge.git

1. ZoomButton

ZoomButton按钮 : ZoomButton按钮提供放大 缩小两个按钮, 两个按钮;

ZoomControls按钮 : 该按钮会自动生成一组两个按钮, 两个按钮分别是放大和缩小;

按钮点击切换背景 : 设置selector资源, 设置两个item, 一个item的状态为按下时, 显示一个图片, 另一个item的状态为普通情况下, 显示另一个图片;

selector源码 :

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <selector xmlns:android=“http://schemas.android.com/apk/res/android” >
  3.     <item android:state_pressed=“true”
  4.         android:drawable=“@drawable/app3”></item>
  5.     <item android:state_pressed=“false”
  6.         android:drawable=“@drawable/app4”></item>
  7. </selector>

 

XML源码 :

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:orientation=“vertical” >
  6.     <ImageButton
  7.         android:layout_width=“wrap_content”
  8.         android:layout_height=“wrap_content”
  9.         android:layout_gravity=“center_horizontal”
  10.         android:background=“@drawable/app2”/>
  11.     <ImageButton
  12.         android:layout_width=“wrap_content”
  13.         android:layout_height=“wrap_content”
  14.         android:layout_gravity=“center_horizontal”
  15.         android:background=“@drawable/bt_bg”/>
  16.     <LinearLayout
  17.         android:layout_width=“wrap_content”
  18.         android:layout_height=“wrap_content”
  19.         android:padding=“10dp”
  20.         android:layout_gravity=“center_horizontal”>
  21.         <ZoomButton
  22.             android:id=“@+id/zoom_1”
  23.             android:layout_height=“wrap_content”
  24.             android:layout_width=“wrap_content”
  25.             android:src=“@android:drawable/btn_minus”/>
  26.         <ZoomButton
  27.             android:id=“@+id/zoom_2”
  28.             android:layout_height=“wrap_content”
  29.             android:layout_width=“wrap_content”
  30.             android:src=“@android:drawable/btn_plus”/>
  31.     </LinearLayout>
  32.     <ZoomControls
  33.         android:id=“@+id/zoom_ctrl”
  34.         android:layout_width=“wrap_content”
  35.         android:layout_height=“wrap_content”
  36.         android:layout_gravity=“center_horizontal”/>
  37. </LinearLayout>

2. QuickContactBadge

本质 : QuickContactBadge 也是图片, 可以通过android:src 指定图片;

功能 : QuickContactBadge 可以关联手机通讯录中的联系人, 当点击图片的时候, 会弹出该联系人相关的界面;

关联方法 :

使用邮箱关联 :assignContactFromEmail(String address, boolean lazyLookup), 将图片关联到指定email联系人;

使用号码关联 :assignContactFromPhone(String number, boolean lazyLookup), 将图片关联到指定电话联系人;

使用Uri关联 :  assignContactUri(Uri uri), 将图片关联到Uri对应的联系人;

XML代码:

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:orientation=“vertical” >
  6.     <QuickContactBadge
  7.         android:id=“@+id/badge”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:src=“@drawable/app5”/>
  11. </LinearLayout>

 

Activity代码 :

  1. package shuliang.han.zoombutton_quickcontactbadge;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.QuickContactBadge;
  5. import shuliang.han.zoombutton_quickcontactbadge.R;
  6. public class QuickContactBadgeActivity extends Activity {
  7.     private QuickContactBadge badge;
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.quick_contact_badge);
  12.         badge = (QuickContactBadge) findViewById(R.id.badge);
  13.         badge.assignContactFromPhone(“120”false);
  14.     }
  15. }

.

 

转自:http://blog.csdn.net/shulianghan/article/details/18555131

Share this: