Android开发速成简洁教程十七:Dialog 显示图像

移动开发 Android
Dialog是通过AlertDialog.Builder 来创建的,这里Dialog显示了三个选项,通过builder.setSingleChoiceItems添加处理事件。实际AlertDialog可以有多种选项,具体请参考Android AlertDialog 文档。

Dialog一般指可以显示在Activity前面的小窗口,当前的Activity失去焦点(Focus),Dialog将接受用户输入,一般可 以用来显示消息或接受用户输入等等。使用Dialog时一般不需要直接创建Dialog类的实例。而是可以使用 AlertDialog,ProgressDialog,DatePickerDialog,TimePickerDialog。最常用的是 AlertDialog。下面就以使用AlertDialog为例,使用AlertDialog来选择显示图像的三个例子:DrawMap, JumbleImage,SeeThroughImage。其中DrawMap暂时不介绍,将在后面介绍Internet应用显示在线地图时再说。

通常Dialog是作为Activity一部分来创建的,也就是说在Activity的onCreateDialog(int)中创建。当在 onCreateDialog(int)创建Dialog时,Android系统将自动管理Dialog的状态,并把当前Activity作为 Dialog的所有者。并且Dialog也继承当前Activity的一些属性,比如说Option Menu。

创建好Dialog后,可以使用showDialog(int) 来显示Dialog ,showDialog的参数为Dialog的ID。在显示Dialog之前,如果想对Dialog做些改动,可以 在 onPrepareDialog(int, Dialog) 添加代码。dismiss()关闭对话框。如果在Activity中则使用dismissDialog(int) 。

本例中使用一个按钮来触发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 来创建的,这里Dialog显示了三个选项,通过builder.setSingleChoiceItems添加处理事件。实际AlertDialog可以有多种选项,具体请参考Android AlertDialog 文档。

责任编辑:闫佳明 来源: imobilebbs
相关推荐

2013-12-26 15:10:08

Android开发应用和框架Linux 内核

2013-12-26 15:43:07

Android开发Android应用Activities

2013-12-26 15:18:09

Android开发安装开发环境

2013-12-26 17:08:36

Android开发Android应用自定义Adapter显

2013-12-27 14:16:43

Android开发Android应用线程

2013-12-27 14:34:46

Android开发Android应用短信触发示例

2013-12-26 15:34:19

Android开发Android应用基本概念

2013-12-27 16:06:10

Android开发Android应用发布应用

2013-12-27 13:27:05

Android开发Android应用RadioButton

2013-12-26 15:46:30

Android开发Android应用用户界面设计

2013-12-26 16:24:13

Android开发Android应用Intents

2013-12-27 12:51:44

Android开发Android应用引路蜂

2013-12-27 13:49:22

Android开发Android应用Button

2013-12-26 16:59:12

Android开发Android应用数据绑定Data Bi

2013-12-27 15:31:26

Android开发Android应用资源Resources

2013-12-26 16:46:21

2013-12-27 13:00:30

Android开发Android应用Context Men

2013-12-27 15:11:17

Android开发访问Internet绘制在线地图

2013-12-27 14:10:36

Android开发Android应用Transform

2013-12-27 12:42:15

Android开发Android应用引路蜂
点赞
收藏

51CTO技术栈公众号