自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android開發(fā)速成簡潔教程十七:Dialog 顯示圖像

移動開發(fā) Android
Dialog是通過AlertDialog.Builder 來創(chuàng)建的,這里Dialog顯示了三個選項,通過builder.setSingleChoiceItems添加處理事件。實際AlertDialog可以有多種選項,具體請參考Android AlertDialog 文檔。

Dialog一般指可以顯示在Activity前面的小窗口,當(dāng)前的Activity失去焦點(Focus),Dialog將接受用戶輸入,一般可 以用來顯示消息或接受用戶輸入等等。使用Dialog時一般不需要直接創(chuàng)建Dialog類的實例。而是可以使用 AlertDialog,ProgressDialog,DatePickerDialog,TimePickerDialog。最常用的是 AlertDialog。下面就以使用AlertDialog為例,使用AlertDialog來選擇顯示圖像的三個例子:DrawMap, JumbleImage,SeeThroughImage。其中DrawMap暫時不介紹,將在后面介紹Internet應(yīng)用顯示在線地圖時再說。

通常Dialog是作為Activity一部分來創(chuàng)建的,也就是說在Activity的onCreateDialog(int)中創(chuàng)建。當(dāng)在 onCreateDialog(int)創(chuàng)建Dialog時,Android系統(tǒng)將自動管理Dialog的狀態(tài),并把當(dāng)前Activity作為 Dialog的所有者。并且Dialog也繼承當(dāng)前Activity的一些屬性,比如說Option Menu。

創(chuàng)建好Dialog后,可以使用showDialog(int) 來顯示Dialog ,showDialog的參數(shù)為Dialog的ID。在顯示Dialog之前,如果想對Dialog做些改動,可以 在 onPrepareDialog(int, Dialog) 添加代碼。dismiss()關(guān)閉對話框。如果在Activity中則使用dismissDialog(int) 。

本例中使用一個按鈕來觸發(fā)Dialog,在res\layout 在添加images.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:background=”@drawable/white” 
  5.  android:layout_width=”fill_parent” 
  6.  android:layout_height=”fill_parent”> 
  7.     <com.pstreets.graphics2d.GuidebeeGraphics2DView 
  8.      android:id=”@+id/graphics2dview” 
  9.      android:layout_weight=”1″ 
  10.      android:layout_width=”fill_parent” 
  11.      android:layout_height=”wrap_content”/> 
  12.  <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” 
  13.   android:layout_width=”wrap_content” android:layout_height=”wrap_content” 
  14.   android:orientation=”horizontal” 
  15.    
  16.   > 
  17.    
  18.    <Button android:text=”Images” 
  19.        android:id=”@+id/btnImages” 
  20.     android:layout_width=”wrap_content” 
  21.     android:textColor=”@color/black” 
  22.     android:checked=”true” 
  23.     android:layout_height=”wrap_content”> 
  24.    </Button> 
  25.    
  26.  </LinearLayout> 
  27.  
  28. </LinearLayout> 

修改Image.java

  1. public class Images extends Graphics2DActivity 
  2. implements OnClickListener{ 
  3.  
  4.  private Button btnImages; 
  5.  private int[] imageDuke; 
  6.   
  7.  static final private int IMAGE_DIALOG=1
  8.   
  9.  int w, h; 
  10.     int offX, offY; 
  11.     
  12.  int alpha = 128
  13.  FontEx font = FontEx.getSystemFont(); 
  14.     int fontSize = 24
  15.     Pen pen = new Pen(Color.RED, 2); 
  16.     char[] message = "Guidebee".toCharArray(); 
  17.     int widthOfMessage = 0
  18.     
  19.     
  20.     private int numlocs = 2
  21.     private int numcells = numlocs * numlocs; 
  22.     private int[] cells; 
  23.     int  cw, ch; 
  24.   
  25.   
  26.     
  27.  public void onCreate(Bundle savedInstanceState) { 
  28.   super.onCreate(savedInstanceState); 
  29.   setContentView(R.layout.images); 
  30.   graphic2dView = (GuidebeeGraphics2DView) 
  31.       findViewById(R.id.graphics2dview); 
  32.   btnImages = (Button) findViewById(R.id.btnImages); 
  33.   btnImages.setOnClickListener(this); 
  34.   Bitmap bitmap 
  35.     = BitmapFactory.decodeResource(getResources(), 
  36.     R.drawable.duke_skateboard); 
  37.   imageDuke = new int[bitmap.getHeight() 
  38.                           * bitmap.getWidth()]; 
  39.   bitmap.getPixels(imageDuke, 0, bitmap.getWidth(), 00
  40.     bitmap.getWidth(), bitmap.getHeight()); 
  41.   widthOfMessage = font.charsWidth(message, 0
  42.     message.length, fontSize); 
  43.   w=bitmap.getWidth(); 
  44.      h=bitmap.getHeight(); 
  45.         offX = (SharedGraphics2DInstance.CANVAS_WIDTH - w) / 2
  46.         offY = (SharedGraphics2DInstance.CANVAS_HEIGHT - h) / 2
  47.        
  48.         cw = w / numlocs; 
  49.         ch = h / numlocs; 
  50.         cells = new int[numcells]; 
  51.         for (int i = 0; i < numcells; i++) { 
  52.             cells[i] = i; 
  53.         } 
  54.         
  55.  
  56.  } 
  57.   
  58.  private void drawJumbleImage(){ 
  59.   Random rand = new Random(); 
  60.         int ri; 
  61.         for (int i = 0; i < numcells; i++) { 
  62.             while ((ri = rand.nextInt(numlocs)) == i) { 
  63.             } 
  64.  
  65.             int tmp = cells[i]; 
  66.             cells[i] = cells[ri]; 
  67.             cells[ri] = tmp; 
  68.         } 
  69.         graphics2D.clear(Color.WHITE); 
  70.         graphics2D.Reset(); 
  71.  
  72.         int dx, dy; 
  73.         for (int x = 0; x < numlocs; x++) { 
  74.             int sx = x * cw; 
  75.             for (int y = 0; y < numlocs; y++) { 
  76.                 int sy = y * ch; 
  77.                 int cell = cells[x * numlocs + y]; 
  78.                 dx = (cell / numlocs) * cw; 
  79.                 dy = (cell % numlocs) * ch; 
  80.                 graphics2D.drawImage(imageDuke, w, h, 
  81.                         dx + offX, dy + offY, 
  82.                         sx, sy, cw, ch); 
  83.             } 
  84.         } 
  85.         
  86.         graphic2dView.refreshCanvas(); 
  87.  } 
  88.   
  89.  private void drawSeeThroughImage(){ 
  90.   alpha += 16
  91.   if(alpha>255) alpha=0
  92.   graphics2D.clear(Color.WHITE); 
  93.   graphics2D.Reset(); 
  94.   graphics2D.setDefaultPen(pen); 
  95.         graphics2D.drawChars(font, fontSize, message, 
  96.           0, message.length, offX 
  97.                 + (w - widthOfMessage) / 2, offY + h / 2); 
  98.         graphics2D.drawImage(imageDuke, w, h, 
  99.                 offX, offY, 
  100.                 0xFFFF00FF, alpha); 
  101.         graphic2dView.refreshCanvas(); 
  102.  } 
  103.   
  104.  protected Dialog onCreateDialog(int id) {    
  105.   Dialog dialog;    
  106.   switch(id) {    
  107.      case IMAGE_DIALOG:      
  108.       final CharSequence[] items = {"DrawMap"
  109.         "JumbleImage","SeeThroughImage"}; 
  110.       AlertDialog.Builder builder 
  111.       = new AlertDialog.Builder(this); 
  112.       builder.setTitle("Images"); 
  113.       builder.setSingleChoiceItems(items, 
  114.         -1new DialogInterface.OnClickListener() {    
  115.             public void onClick(DialogInterface dialog, 
  116.               int item) {        
  117.                switch(item){ 
  118.                case 0
  119.                
  120.                 break
  121.                case 1
  122.                 drawJumbleImage(); 
  123.                 break
  124.                case 2
  125.                 drawSeeThroughImage(); 
  126.                 break
  127.  
  128.                } 
  129.                dialog.dismiss(); 
  130.            } 
  131.            }); 
  132.            AlertDialog alert = builder.create(); 
  133.          dialog=alert; 
  134.          break;        
  135.            default:        
  136.             dialog = null;   
  137.          }  
  138.      return dialog; 
  139.   } 
  140.    
  141.  @Override 
  142.  protected void drawImage() { 
  143.   drawJumbleImage(); 
  144.    
  145.  } 
  146.  
  147.  @Override 
  148.  public void onClick(View view) { 
  149.   showDialog(IMAGE_DIALOG); 
  150.    
  151.  } 
  152.  

從代碼中看到,Dialog是通過AlertDialog.Builder 來創(chuàng)建的,這里Dialog顯示了三個選項,通過builder.setSingleChoiceItems添加處理事件。實際AlertDialog可以有多種選項,具體請參考Android AlertDialog 文檔。

責(zé)任編輯:閆佳明 來源: imobilebbs
相關(guān)推薦

2013-12-26 15:43:07

Android開發(fā)Android應(yīng)用Activities

2013-12-26 15:10:08

Android開發(fā)應(yīng)用和框架Linux 內(nèi)核

2013-12-26 15:18:09

Android開發(fā)安裝開發(fā)環(huán)境

2013-12-26 17:08:36

Android開發(fā)Android應(yīng)用自定義Adapter顯

2013-12-27 14:34:46

Android開發(fā)Android應(yīng)用短信觸發(fā)示例

2013-12-27 14:16:43

Android開發(fā)Android應(yīng)用線程

2013-12-26 15:34:19

Android開發(fā)Android應(yīng)用基本概念

2013-12-27 13:49:22

Android開發(fā)Android應(yīng)用Button

2013-12-26 16:59:12

Android開發(fā)Android應(yīng)用數(shù)據(jù)綁定Data Bi

2013-12-27 12:51:44

Android開發(fā)Android應(yīng)用引路蜂

2013-12-26 16:24:13

Android開發(fā)Android應(yīng)用Intents

2013-12-27 13:27:05

Android開發(fā)Android應(yīng)用RadioButton

2013-12-27 16:06:10

Android開發(fā)Android應(yīng)用發(fā)布應(yīng)用

2013-12-26 15:46:30

Android開發(fā)Android應(yīng)用用戶界面設(shè)計

2013-12-26 16:46:21

2013-12-27 15:31:26

Android開發(fā)Android應(yīng)用資源Resources

2013-12-27 13:00:30

Android開發(fā)Android應(yīng)用Context Men

2013-12-27 12:42:15

Android開發(fā)Android應(yīng)用引路蜂

2013-12-27 14:10:36

Android開發(fā)Android應(yīng)用Transform

2013-12-27 15:11:17

Android開發(fā)訪問Internet繪制在線地圖
點贊
收藏

51CTO技術(shù)棧公眾號