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

公司主營(yíng)業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。成都創(chuàng)新互聯(lián)公司推出西安免費(fèi)做網(wǎng)站回饋大家。
通常Dialog是作為Activity一部分來(lái)創(chuàng)建的,也就是說(shuō)在Activity的onCreateDialog(int)中創(chuàng)建。當(dāng)在 onCreateDialog(int)創(chuàng)建Dialog時(shí),Android系統(tǒng)將自動(dòng)管理Dialog的狀態(tài),并把當(dāng)前Activity作為 Dialog的所有者。并且Dialog也繼承當(dāng)前Activity的一些屬性,比如說(shuō)Option Menu。
創(chuàng)建好Dialog后,可以使用showDialog(int) 來(lái)顯示Dialog ,showDialog的參數(shù)為Dialog的ID。在顯示Dialog之前,如果想對(duì)Dialog做些改動(dòng),可以 在 onPrepareDialog(int, Dialog) 添加代碼。dismiss()關(guān)閉對(duì)話框。如果在Activity中則使用dismissDialog(int) 。
本例中使用一個(gè)按鈕來(lái)觸發(fā)Dialog,在res\layout 在添加images.xml
- android:orientation=”vertical”
- android:background=”@drawable/white”
- android:layout_width=”fill_parent”
- android:layout_height=”fill_parent”>
- android:id=”@+id/graphics2dview”
- android:layout_weight=”1″
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content”/>
- android:layout_width=”wrap_content” android:layout_height=”wrap_content”
- android:orientation=”horizontal”
- >
修改Image.java
- public class Images extends Graphics2DActivity
- implements OnClickListener{
- private Button btnImages;
- private int[] imageDuke;
- static final private int IMAGE_DIALOG=1;
- int w, h;
- int offX, offY;
- int alpha = 128;
- FontEx font = FontEx.getSystemFont();
- int fontSize = 24;
- Pen pen = new Pen(Color.RED, 2);
- char[] message = "Guidebee".toCharArray();
- int widthOfMessage = 0;
- private int numlocs = 2;
- private int numcells = numlocs * numlocs;
- private int[] cells;
- int cw, ch;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.images);
- graphic2dView = (GuidebeeGraphics2DView)
- findViewById(R.id.graphics2dview);
- btnImages = (Button) findViewById(R.id.btnImages);
- btnImages.setOnClickListener(this);
- Bitmap bitmap
- = BitmapFactory.decodeResource(getResources(),
- R.drawable.duke_skateboard);
- imageDuke = new int[bitmap.getHeight()
- * bitmap.getWidth()];
- bitmap.getPixels(imageDuke, 0, bitmap.getWidth(), 0, 0,
- bitmap.getWidth(), bitmap.getHeight());
- widthOfMessage = font.charsWidth(message, 0,
- message.length, fontSize);
- w=bitmap.getWidth();
- h=bitmap.getHeight();
- offX = (SharedGraphics2DInstance.CANVAS_WIDTH - w) / 2;
- offY = (SharedGraphics2DInstance.CANVAS_HEIGHT - h) / 2;
- cw = w / numlocs;
- ch = h / numlocs;
- cells = new int[numcells];
- for (int i = 0; i < numcells; i++) {
- cells[i] = i;
- }
- }
- private void drawJumbleImage(){
- Random rand = new Random();
- int ri;
- for (int i = 0; i < numcells; i++) {
- while ((ri = rand.nextInt(numlocs)) == i) {
- }
- int tmp = cells[i];
- cells[i] = cells[ri];
- cells[ri] = tmp;
- }
- graphics2D.clear(Color.WHITE);
- graphics2D.Reset();
- int dx, dy;
- for (int x = 0; x < numlocs; x++) {
- int sx = x * cw;
- for (int y = 0; y < numlocs; y++) {
- int sy = y * ch;
- int cell = cells[x * numlocs + y];
- dx = (cell / numlocs) * cw;
- dy = (cell % numlocs) * ch;
- graphics2D.drawImage(imageDuke, w, h,
- dx + offX, dy + offY,
- sx, sy, cw, ch);
- }
- }
- graphic2dView.refreshCanvas();
- }
- private void drawSeeThroughImage(){
- alpha += 16;
- if(alpha>255) alpha=0;
- graphics2D.clear(Color.WHITE);
- graphics2D.Reset();
- graphics2D.setDefaultPen(pen);
- graphics2D.drawChars(font, fontSize, message,
- 0, message.length, offX
- + (w - widthOfMessage) / 2, offY + h / 2);
- graphics2D.drawImage(imageDuke, w, h,
- offX, offY,
- 0xFFFF00FF, alpha);
- graphic2dView.refreshCanvas();
- }
- protected Dialog onCreateDialog(int id) {
- Dialog dialog;
- switch(id) {
- case IMAGE_DIALOG:
- final CharSequence[] items = {"DrawMap",
- "JumbleImage","SeeThroughImage"};
- AlertDialog.Builder builder
- = new AlertDialog.Builder(this);
- builder.setTitle("Images");
- builder.setSingleChoiceItems(items,
- -1, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int item) {
- switch(item){
- case 0:
- break;
- case 1:
- drawJumbleImage();
- break;
- case 2:
- drawSeeThroughImage();
- break;
- }
- dialog.dismiss();
- }
- });
- AlertDialog alert = builder.create();
- dialog=alert;
- break;
- default:
- dialog = null;
- }
- return dialog;
- }
- @Override
- protected void drawImage() {
- drawJumbleImage();
- }
- @Override
- public void onClick(View view) {
- showDialog(IMAGE_DIALOG);
- }
- }
從代碼中看到,Dialog是通過(guò)AlertDialog.Builder 來(lái)創(chuàng)建的,這里Dialog顯示了三個(gè)選項(xiàng),通過(guò)builder.setSingleChoiceItems添加處理事件。實(shí)際AlertDialog可以有多種選項(xiàng),具體請(qǐng)參考Android AlertDialog 文檔。
文章標(biāo)題:Android開(kāi)發(fā)速成簡(jiǎn)潔教程十七:Dialog顯示圖像
轉(zhuǎn)載來(lái)源:http://www.dlmjj.cn/article/djdedci.html


咨詢
建站咨詢
