카테고리 없음

[기계적 인조 인간] WebView를 통해 표시되는 Android 로컬 이미지가 흐릿함

행복을전해요 2021. 2. 17. 14:29

I recall that problem. Webview automatically downsamples larger images probably out of memory concerns. If you want control of the quality you probably have to use ImageView. You could also try to use the gallery application instead.

-------------------

웹뷰에서 좋은 품질의 이미지를 보여주는 솔루션을 찾았습니다. (내가 webview로 작업하는 것을 혐오한다는 것을 알게하십시오!)

근본 원인 : 기본적으로 웹보기에는 XXHDPI 및 XHDPI 개념이 없습니다. XXHDPI 또는 XHDPI 장치에서 해당 이미지를 사용하면 이미지가 너무 큽니다! 그래서 결국 HDPI / MDPI 이미지를 사용하게됩니다. 적어도 제가 한 다음 이미지가 흐릿하게 보입니다.

솔루션 설명 : HTML (활동 내에서 프로그래밍 방식으로 생성)에서 확장을 활성화 한 다음 프로그래밍 방식으로 논리적 밀도를 감지합니다. 스케일 (예 : XXHDPI 장치에서 300 %)을 계산하고 그 값을 HTML에 넣으십시오. 그렇지 않으면 글꼴이 작습니다! 드로어 블 리소스 내에서 장치의 해상도와 일치하는 그래픽 (예 : XXHDPI 장치의 XXHDPI 이미지)을 사용하고 있는지 확인하십시오. 이것은 XXHDPI res 폴더의 MDPI 이미지를 교체하라는 알림입니다.

코딩 된 솔루션 :

// constants needed to put together a nice Android webview-friendly page
private static final String HTML_STYLE = "<style>img{image-rendering: optimizeQuality;} html{font-size: [TBD]% !important}</style>";
private static final String HTML_META = "<meta name='viewport' content='initial-scale=1.0, maximum-scale=1.0, user-scalable=no; target-densitydpi=device-dpi' />";
private static final String HTML_HEAD = "<html><head><title>description</title>"+HTML_META+HTML_STYLE+"</head><body bgcolor=\"black\"><font color=\"white\">";
private static final String HTML_TAIL = "</font></body></html>";

// content to show in the webview
final String strBody = "<ul><li>stuff1</li><li>stuff2</li><li>stuff3</li></ul><p align=\"center\"><img src=\"file:///android_res/drawable/image.png\"></p>";

// get the logical font size
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int iFontScale = (int) (displaymetrics.scaledDensity * 100.0f);

// alter the HTML to programmatically insert the font scale
final String strCompleteHTML = (HTML_HEAD+strBody+HTML_TAIL).replace("[TBD]", String.valueOf(iFontScale));

// configure the webview and load the HTML
final WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setHorizontalScrollBarEnabled(false);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL("file:///android_asset/", strCompleteHTML, "text/html", "utf-8", null);
-------------------

왜 복잡한 답이 필요한지 잘 모르겠습니까?

400px 이미지를 사용하여 200px 이미지가 흐릿한 문제를 해결하고 CSS에서 요소 2만큼 축소했습니다. 200px 및 background-size : contain / 200px 50px로 설정된 DIV 요소; 나를 위해 해결



출처
https://stackoverflow.com/questions/22079820