0
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("ВЫХОД из программы");
dialog.setIcon(R.drawable.tt128);
dialog.setMessage("Уже уходите ?!");

Иконка "tt128.png" имеет размер: 128х128.

Иконка на разных экранах имеет разный размер (с маленьким разрешением: слишком большая, с большим разрешением: слишком маленькая).

Ширина (dW) и высота (dH) экрана, с которого запускают приложение, известны. Как установить размеры иконки вышеописанного диалога в зависимости от dW и dH ?!

Решение: сделать много иконок для разных экранов - не предлагать)

Спасибо!

2 ответа 2

1

Самое простое - предварительно отресайзить битмап в нужный вам размер - пример.

Также можно использовать android.graphics.Bitmap.createScaledBitmap

Другой вариант - насколько я помню, у Dialog можно сделать setView(), указав нужный вам layout (например, считать его из ресурсов), тем самым полностью заменить содержимое диалога, в том числе message и icon.
Ну а уж в своем layout можно наворопупить всего чего угодно, в том числе icon нужного размера.

1

Все получилось!

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      tools:context=".MainActivity">
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_weight="1"
            android:scaleType="fitStart"
            android:layout_gravity="center_vertical"
            android:src="@drawable/homecolor"/>
</LinearLayout>

MainActivity.xml

package com.images;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private static int sW;
    private int sH;
    public static Float dens;
    public static int newW;
    public Bitmap im;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int height = this.getWindowManager().getDefaultDisplay().getHeight();
        int width = this.getWindowManager().getDefaultDisplay().getWidth();
        dens = getResources().getDisplayMetrics().density;
        sW = width; sH = height;
        newW = sW/2; // вывод изображения в половину ширины экрана

        ImageView image = (ImageView) findViewById(R.id.imageView);
        image.setImageBitmap(getResizedBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.homecolor), newW, newW));
    }

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    }
}

Тестовая картинка

В результате картинка выводится на всех экранах в половину ширины экрана (что и требовалось)!

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.