Android диалог (не пользовательский) проблема - PullRequest
1 голос
/ 23 марта 2011

Я пытаюсь отобразить простое оповещение, работающее над моей активностью, когда нет доступа к интернету, однако по какой-то причине я не знаю, когда я устанавливаю значок для оповещения, окно становится грязным, слишком большим. Ниже мой код:

@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;
}

результат этого кода отображается на изображении ниже.

device screenshot

Что я делаю не так? Я что-то пропустил? Этот код был прямо скопирован из исходного кода демонстрационных программ API, который отлично работает на моем устройстве.

Большое спасибо T

Ответы [ 3 ]

2 голосов
/ 23 марта 2011

Лучший способ - изменить размер иконки. Если вы хотите сделать это программно, проверьте это ..

    // 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);

Это растровое изображение может использоваться следующим образом

builder.setIcon (BMD);

1 голос
/ 24 августа 2012

Нет функций width() и Height(), ниже приведен исправленный код:

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);
1 голос
/ 23 марта 2011

Может быть значок , который вы используете, имеет большой размер .... Масштабируйте изображение до растрового изображения до небольшого размера и используйте BitmapDrawable ..!!вместо id

...