AndroidUI設計 之 圖片瀏覽器


圖片瀏覽器效果圖 : 

源碼下載地址 : 

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

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


 

一. 圖片瀏覽器顯示界面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.   
  7.     <ImageView   
  8.         android:id="@+id/im"  
  9.         android:background="#00FF00"  
  10.         android:layout_height="300dp"  
  11.         android:layout_width="300dp"  
  12.         android:src="@drawable/pic"  
  13.         <strong>android:scaleType</strong>="matrix"/>  
  14.   
  15. </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.     //計算每次自增自減的值  
  10.     addWidth = imageWidth / 5;  
  11.     addHeight = imageHeight / 5;  
  12. }  

縮放圖片操作 : 

 

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



 

4. 旋轉圖片操作

 

 

設置Matrix對象 : 該對象用來存放圖像的旋轉角度;

 

設置旋轉角度 : matrix.setRotate(), 即可設置旋轉角度;

 

創建Bitmap : 創建一個位圖, 注意將設置了旋轉角度的 matrix 設置上去;

 

源碼示例 :  

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



 

三. 圖片瀏覽器源碼 

 

 

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.   
  8.     <LinearLayout   
  9.         android:orientation="horizontal"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_width="fill_parent"  
  12.         android:padding="5dp"  
  13.         android:gravity="center">  
  14.           
  15.         <Button   
  16.             android:id="@+id/alpha_plus"  
  17.             android:layout_height="fill_parent"  
  18.             android:layout_width="fill_parent"  
  19.             android:layout_weight="1"  
  20.             android:text="透明度+"  
  21.             android:background="@drawable/bg_bt"  
  22.             android:onClick="onClick"/>  
  23.           
  24.         <Button   
  25.             android:id="@+id/alpha_minus"  
  26.             android:layout_height="fill_parent"  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_weight="1"  
  29.             android:text="透明度-"  
  30.             android:background="@drawable/bg_bt"  
  31.             android:onClick="onClick"/>  
  32.           
  33.         <Button   
  34.             android:id="@+id/prev_page"  
  35.             android:layout_height="fill_parent"  
  36.             android:layout_width="fill_parent"  
  37.             android:layout_weight="1"  
  38.             android:text="上一張"  
  39.             android:background="@drawable/bg_bt"  
  40.             android:onClick="onClick"/>  
  41.           
  42.         <Button   
  43.             android:id="@+id/next_page"  
  44.             android:layout_height="fill_parent"  
  45.             android:layout_width="fill_parent"  
  46.             android:layout_weight="1"  
  47.             android:text="下一張"  
  48.             android:background="@drawable/bg_bt"  
  49.             android:onClick="onClick"/>  
  50.           
  51.     </LinearLayout>  
  52.       
  53.     <LinearLayout   
  54.         android:orientation="horizontal"  
  55.         android:layout_height="wrap_content"  
  56.         android:layout_width="fill_parent"  
  57.         android:padding="5dp"  
  58.         android:gravity="center">  
  59.           
  60.         <Button   
  61.             android:id="@+id/big"  
  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.           
  69.         <Button   
  70.             android:id="@+id/small"  
  71.             android:layout_height="fill_parent"  
  72.             android:layout_width="fill_parent"  
  73.             android:layout_weight="1"  
  74.             android:text="縮小"  
  75.             android:background="@drawable/bg_bt"  
  76.             android:onClick="onClick"/>  
  77.           
  78.         <Button   
  79.             android:id="@+id/turn_left"  
  80.             android:layout_height="fill_parent"  
  81.             android:layout_width="fill_parent"  
  82.             android:layout_weight="1"  
  83.             android:text="左轉"  
  84.             android:background="@drawable/bg_bt"  
  85.             android:onClick="onClick"/>  
  86.           
  87.         <Button   
  88.             android:id="@+id/turn_right"  
  89.             android:layout_height="fill_parent"  
  90.             android:layout_width="fill_parent"  
  91.             android:layout_weight="1"  
  92.             android:text="右轉"  
  93.             android:background="@drawable/bg_bt"  
  94.             android:onClick="onClick"/>  
  95.           
  96.     </LinearLayout>  
  97.       
  98.     <ImageView   
  99.         android:id="@+id/image_all"  
  100.         android:layout_width="fill_parent"  
  101.         android:layout_height="fill_parent"  
  102.         android:scaleType="fitCenter"  
  103.         android:src="@drawable/mary1"/>  
  104.   
  105. </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.       
  4.     <item android:state_pressed="true"  
  5.         android:drawable="@drawable/bt_focus"></item>  
  6.       
  7.     <item android:state_pressed="false"  
  8.         android:drawable="@drawable/bt_normal"></item>  
  9.   
  10. </selector>  

 

 

主Activity核心代碼 :  

  1. package shuliang.han.imageview_test;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Matrix;  
  6. import android.graphics.drawable.BitmapDrawable;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.ImageView;  
  10. import android.widget.LinearLayout;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     private int[] images;       //圖片資源id數組  
  15.     private int currentImage;   //當前顯示圖片  
  16.     private int alpha;          //透明度  
  17.       
  18.     private Matrix matrix;  
  19.     private int anglel;         //角度  
  20.       
  21.     private int imageWidth;  
  22.     private int imageHeight;  
  23.       
  24.     private int addWidth;  
  25.     private int addHeight;  
  26.       
  27.     private ImageView image_all;  
  28.       
  29.     @Override  
  30.     protected void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.activity_main);  
  33.           
  34.         init();  
  35.           
  36.     }  
  37.       
  38.     //初始化成員變量  
  39.     private void init() {  
  40.         images = new int[]{R.drawable.mary1, R.drawable.mary2};  
  41.         currentImage = Integer.MAX_VALUE / 2;  
  42.         alpha = 255;  
  43.           
  44.         matrix = new Matrix();  
  45.           
  46.         image_all = (ImageView) findViewById(R.id.image_all);  
  47.         image_all.setImageResource(images[currentImage % images.length]);  
  48.     }  
  49.       
  50.     public void onClick(View view) {  
  51.         int id = view.getId();  
  52.           
  53.         switch (id) {  
  54.             case R.id.alpha_plus:   //增大透明度  
  55.                 alpha += 20;  
  56.                 if(alpha >= 255)  
  57.                     alpha = 255;  
  58.                 image_all.setAlpha(alpha);  
  59.                 break;  
  60.                   
  61.             case R.id.alpha_minus:  //減小透明度  
  62.                 alpha -= 20;  
  63.                 if(alpha <= 0)  
  64.                     alpha = 0;  
  65.                 image_all.setAlpha(alpha);  
  66.                 break;  
  67.                   
  68.             case R.id.next_page:    //顯示下一張圖片  
  69.                 //為了保證圖片能夠循環, 這里模運算是關鍵, 顯示圖片的下標始終是長度的模  
  70.                 image_all.setImageResource(images[ ++currentImage % images.length ]);  
  71.                 break;  
  72.               
  73.             case R.id.prev_page:    //顯示上一張圖片  
  74.                 image_all.setImageResource(images[ --currentImage % images.length ]);  
  75.                 break;  
  76.                   
  77.             case R.id.big:          //放大圖片  
  78.                 imageWidth += addWidth;  
  79.                 imageHeight += addHeight;  
  80.                       
  81.                 image_all.setLayoutParams(new LinearLayout.LayoutParams(imageWidth, imageHeight));  
  82.                 break;  
  83.                   
  84.             case R.id.small:        //縮小圖片  
  85.                 imageWidth -= addWidth;  
  86.                 imageHeight -= addHeight;  
  87.                 if(imageWidth <= 0 || imageHeight <=0){  
  88.                     imageWidth += addWidth;  
  89.                     imageHeight += addHeight;  
  90.                 }  
  91.                   
  92.                 image_all.setLayoutParams(new LinearLayout.LayoutParams(imageWidth, imageHeight));  
  93.                 break;  
  94.                   
  95.             case R.id.turn_left:    //向左旋轉  
  96.                 anglel += 45;  
  97.                 matrix.setRotate(anglel);  
  98.                 Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(images[currentImage % images.length])).getBitmap();  
  99.                 bitmap = Bitmap.createBitmap(bitmap, 00, bitmap.getWidth(),bitmap.getHeight(), matrix, true);   
  100.                 image_all.setImageBitmap(bitmap);  
  101.                 break;  
  102.                   
  103.             case R.id.turn_right:   //向右旋轉  
  104.                 anglel -= 45;  
  105.                 matrix.setRotate(anglel);  
  106.                 Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable(images[currentImage % images.length])).getBitmap();  
  107.                 bitmap1 = Bitmap.createBitmap(bitmap1, 00, bitmap1.getWidth(),bitmap1.getHeight(), matrix, true);   
  108.                 image_all.setImageBitmap(bitmap1);  
  109.                 break;  
  110.                   
  111.             default:  
  112.                 break;  
  113.         }  
  114.     }  
  115.       
  116.     @Override  
  117.     public void onWindowFocusChanged(boolean hasFocus) {  
  118.         // TODO Auto-generated method stub  
  119.         super.onWindowFocusChanged(hasFocus);  
  120.         //獲取ImageView組件的寬高  
  121.         imageWidth = image_all.getWidth();  
  122.         imageHeight = image_all.getHeight();  
  123.           
  124.         //計算每次自增自減的值  
  125.         addWidth = imageWidth / 5;  
  126.         addHeight = imageHeight / 5;  
  127.     }  
  128. }  



 

四. 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.       
  4.     <item android:state_pressed="true"   
  5.         android:drawable="@drawable/app3"></item>  
  6.       
  7.     <item android:state_pressed="false"   
  8.         android:drawable="@drawable/app4"></item>  
  9.   
  10. </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.       
  7.     <ImageButton   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_gravity="center_horizontal"  
  11.         android:background="@drawable/app2"/>  
  12.       
  13.     <ImageButton   
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_gravity="center_horizontal"  
  17.         android:background="@drawable/bt_bg"/>  
  18.       
  19.     <LinearLayout   
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:padding="10dp"  
  23.         android:layout_gravity="center_horizontal">  
  24.           
  25.         <ZoomButton   
  26.             android:id="@+id/zoom_1"  
  27.             android:layout_height="wrap_content"  
  28.             android:layout_width="wrap_content"  
  29.             android:src="@android:drawable/btn_minus"/>  
  30.           
  31.         <ZoomButton   
  32.             android:id="@+id/zoom_2"  
  33.             android:layout_height="wrap_content"  
  34.             android:layout_width="wrap_content"  
  35.             android:src="@android:drawable/btn_plus"/>  
  36.           
  37.     </LinearLayout>      
  38.       
  39.     <ZoomControls   
  40.         android:id="@+id/zoom_ctrl"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content"  
  43.         android:layout_gravity="center_horizontal"/>  
  44.   
  45. </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.       
  7.     <QuickContactBadge   
  8.         android:id="@+id/badge"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/app5"/>  
  12.   
  13. </LinearLayout>  



 

Activity代碼 : 

  

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM