Android简明开发教程十七:Dialog 显示图像

jerry Android 2015年08月24日 收藏

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

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:orientation=”vertical”
    android:background=”@drawable/white”
 android:layout_width=”fill_parent”
 android:layout_height=”fill_parent”>
    <com.pstreets.graphics2d.GuidebeeGraphics2DView
     android:id=”@+id/graphics2dview”
     android:layout_weight=”1″
     android:layout_width=”fill_parent”
     android:layout_height=”wrap_content”/>
 <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  android:layout_width=”wrap_content” android:layout_height=”wrap_content”
  android:orientation=”horizontal”
  
  >
  
   <Button android:text=”Images”
       android:id=”@+id/btnImages”
    android:layout_width=”wrap_content”
    android:textColor=”@color/black”
    android:checked=”true”
    android:layout_height=”wrap_content”>
   </Button>
  
 </LinearLayout>

</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(), 0, 0,
  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.         -1, new 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.  
  153. }

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