开发者

Android Dialog (not custom) problem

I am trying to display a simple alert working on my activity when no internet connection is available, however, for some reason I dont know, when I set the icon for the alert, the box gets messy, too big. Below is my code:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 1:
        return new AlertDialog.Builder(SplashScreen.this)
        .setTitle("Aviso")
        .setIcon(R.drawable.alert_dialog_icon)
        .setMessage("Acesso à internet não disponível. Clique OK para sair.")
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               SplashScreen.this.finish();
           }
        })
        .create();  
    }
    return null;
}

the result of this code is displayed in the image below.

Android Dialog (not custom) problem

What am I doing wrong? Am I missing anything? This code was straightly copied from the API demos source code, which works perfectly in my device开发者_JAVA百科.

Thanks a lot T


The best way is to change your icon size. If you want to do it programmatically check out this..

    // load the origial BitMap (500 x 500 px)
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
           R.drawable.android);

    int width = bitmapOrg.width();
    int height = bitmapOrg.height();
    int newWidth = 200;
    int newHeight = 200;

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                      width, height, matrix, true); 

    // make a Drawable from Bitmap to allow to set the BitMap 
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

This bitmap can be used as follows

builder.setIcon(bmd);


May be the icon you are using is of large size.... Scale the image to a bitmap upto small size and use BitmapDrawable..!! instead of id


There is no width() and Height() functions, below is the corrected the code:

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.cyj);
int width = bitmapOrg.**getWidth**();
int height = bitmapOrg.**getHeight()**;
int newWidth = 45;
int newHeight = 45;

 // calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;

// create matrix for the manipulation
Matrix matrix = new Matrix();

// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);

// rotate the Bitmap
matrix.postRotate(0);

// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                              width, height, matrix, true); 

// make a Drawable from Bitmap to allow to set the BitMap 
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜