Example usage for android.webkit WebView setLayerType

List of usage examples for android.webkit WebView setLayerType

Introduction

In this page you can find the example usage for android.webkit WebView setLayerType.

Prototype

@Override
    public void setLayerType(int layerType, Paint paint) 

Source Link

Usage

From source file:org.ciasaboark.tacere.activity.fragment.AboutLicenseFragment.java

private void initViews() {
    // Show the Up button in the action bar.
    //        setupActionBar();

    WebView wv = (WebView) rootView.findViewById(R.id.webView1);

    String htmlData = "";
    try {//from   w  ww .  j a va 2s  . c o  m
        BufferedReader br = new BufferedReader(new InputStreamReader(context.getAssets().open("license.html")));

        String line;
        while ((line = br.readLine()) != null) {
            htmlData += line;
        }
    } catch (FileNotFoundException e) {
        Log.e(TAG, e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }

    int colorInt = getResources().getColor(R.color.link_color);
    String hexColor = String.format("#%06X", (0xFFFFFF & colorInt));
    while (htmlData.contains("LINKCOLOR")) {
        htmlData = htmlData.replace("LINKCOLOR", hexColor);
    }

    wv.loadData(htmlData, "text/html", "UTF8");

    // All links should open in the default browser, not this WebView
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        }
    });

    wv.setBackgroundColor(0x00000000);
    wv.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
}

From source file:com.androzic.waypoint.WaypointInfo.java

@SuppressLint("NewApi")
private void updateWaypointInfo(double lat, double lon) {
    Androzic application = Androzic.getApplication();
    Activity activity = getActivity();//from   w ww. ja  va2 s . c om
    Dialog dialog = getDialog();
    View view = getView();

    WebView description = (WebView) view.findViewById(R.id.description);

    if ("".equals(waypoint.description)) {
        description.setVisibility(View.GONE);
    } else {
        String descriptionHtml;
        try {
            TypedValue tv = new TypedValue();
            Theme theme = activity.getTheme();
            Resources resources = getResources();
            theme.resolveAttribute(android.R.attr.textColorSecondary, tv, true);
            int secondaryColor = resources.getColor(tv.resourceId);
            String css = String.format(
                    "<style type=\"text/css\">html,body{margin:0;background:transparent} *{color:#%06X}</style>\n",
                    (secondaryColor & 0x00FFFFFF));
            descriptionHtml = css + waypoint.description;
            description.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    view.setBackgroundColor(Color.TRANSPARENT);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                        view.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
                }
            });
            description.setBackgroundColor(Color.TRANSPARENT);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                description.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
        } catch (Resources.NotFoundException e) {
            description.setBackgroundColor(Color.LTGRAY);
            descriptionHtml = waypoint.description;
        }

        WebSettings settings = description.getSettings();
        settings.setDefaultTextEncodingName("utf-8");
        settings.setAllowFileAccess(true);
        Uri baseUrl = Uri.fromFile(new File(application.dataPath));
        description.loadDataWithBaseURL(baseUrl.toString() + "/", descriptionHtml, "text/html", "utf-8", null);
    }

    String coords = StringFormatter.coordinates(" ", waypoint.latitude, waypoint.longitude);
    ((TextView) view.findViewById(R.id.coordinates)).setText(coords);

    if (waypoint.altitude != Integer.MIN_VALUE) {
        ((TextView) view.findViewById(R.id.altitude)).setText(StringFormatter.elevationH(waypoint.altitude));
    }

    double dist = Geo.distance(lat, lon, waypoint.latitude, waypoint.longitude);
    double bearing = Geo.bearing(lat, lon, waypoint.latitude, waypoint.longitude);
    bearing = application.fixDeclination(bearing);
    String distance = StringFormatter.distanceH(dist) + " " + StringFormatter.angleH(bearing);
    ((TextView) view.findViewById(R.id.distance)).setText(distance);

    if (waypoint.date != null)
        ((TextView) view.findViewById(R.id.date))
                .setText(DateFormat.getDateFormat(activity).format(waypoint.date) + " "
                        + DateFormat.getTimeFormat(activity).format(waypoint.date));
    else
        ((TextView) view.findViewById(R.id.date)).setVisibility(View.GONE);

    dialog.setTitle(waypoint.name);
}

From source file:com.androzic.waypoint.WaypointDetails.java

@SuppressLint("NewApi")
private void updateWaypointDetails(double lat, double lon) {
    Androzic application = Androzic.getApplication();
    AppCompatActivity activity = (AppCompatActivity) getActivity();

    activity.getSupportActionBar().setTitle(waypoint.name);

    View view = getView();//  w  ww .  ja  va 2  s .c  om

    final TextView coordsView = (TextView) view.findViewById(R.id.coordinates);
    coordsView.requestFocus();
    coordsView.setTag(Integer.valueOf(StringFormatter.coordinateFormat));
    coordsView.setText(StringFormatter.coordinates(" ", waypoint.latitude, waypoint.longitude));
    coordsView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int format = ((Integer) coordsView.getTag()).intValue() + 1;
            if (format == 5)
                format = 0;
            coordsView.setText(StringFormatter.coordinates(format, " ", waypoint.latitude, waypoint.longitude));
            coordsView.setTag(Integer.valueOf(format));
        }
    });

    if (waypoint.altitude != Integer.MIN_VALUE) {
        ((TextView) view.findViewById(R.id.altitude))
                .setText("\u2336 " + StringFormatter.elevationH(waypoint.altitude));
        view.findViewById(R.id.altitude).setVisibility(View.VISIBLE);
    } else {
        view.findViewById(R.id.altitude).setVisibility(View.GONE);
    }

    if (waypoint.proximity > 0) {
        ((TextView) view.findViewById(R.id.proximity))
                .setText("~ " + StringFormatter.distanceH(waypoint.proximity));
        view.findViewById(R.id.proximity).setVisibility(View.VISIBLE);
    } else {
        view.findViewById(R.id.proximity).setVisibility(View.GONE);
    }

    double dist = Geo.distance(lat, lon, waypoint.latitude, waypoint.longitude);
    double bearing = Geo.bearing(lat, lon, waypoint.latitude, waypoint.longitude);
    bearing = application.fixDeclination(bearing);
    String distance = StringFormatter.distanceH(dist) + " " + StringFormatter.angleH(bearing);
    ((TextView) view.findViewById(R.id.distance)).setText(distance);

    ((TextView) view.findViewById(R.id.waypointset)).setText(waypoint.set.name);

    if (waypoint.date != null) {
        view.findViewById(R.id.date_row).setVisibility(View.VISIBLE);
        ((TextView) view.findViewById(R.id.date))
                .setText(DateFormat.getDateFormat(activity).format(waypoint.date) + " "
                        + DateFormat.getTimeFormat(activity).format(waypoint.date));
    } else {
        view.findViewById(R.id.date_row).setVisibility(View.GONE);
    }

    if ("".equals(waypoint.description)) {
        view.findViewById(R.id.description_row).setVisibility(View.GONE);
    } else {
        WebView description = (WebView) view.findViewById(R.id.description);
        String descriptionHtml;
        try {
            TypedValue tv = new TypedValue();
            Theme theme = activity.getTheme();
            Resources resources = getResources();
            theme.resolveAttribute(android.R.attr.textColorPrimary, tv, true);
            int secondaryColor = resources.getColor(tv.resourceId);
            String css = String.format(
                    "<style type=\"text/css\">html,body{margin:0;background:transparent} *{color:#%06X}</style>\n",
                    (secondaryColor & 0x00FFFFFF));
            descriptionHtml = css + waypoint.description;
            description.setWebViewClient(new WebViewClient() {
                @SuppressLint("NewApi")
                @Override
                public void onPageFinished(WebView view, String url) {
                    view.setBackgroundColor(Color.TRANSPARENT);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                        view.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
                }
            });
            description.setBackgroundColor(Color.TRANSPARENT);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                description.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
        } catch (Resources.NotFoundException e) {
            description.setBackgroundColor(Color.LTGRAY);
            descriptionHtml = waypoint.description;
        }

        WebSettings settings = description.getSettings();
        settings.setDefaultTextEncodingName("utf-8");
        settings.setAllowFileAccess(true);
        Uri baseUrl = Uri.fromFile(new File(application.dataPath));
        description.loadDataWithBaseURL(baseUrl.toString() + "/", descriptionHtml, "text/html", "utf-8", null);
        view.findViewById(R.id.description_row).setVisibility(View.VISIBLE);
    }
}

From source file:im.vector.adapters.ImagesSliderAdapter.java

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    View view = mLayoutInflater.inflate(R.layout.activity_image_web_view, null, false);

    // hide the pie chart
    final PieFractionView pieFractionView = (PieFractionView) view
            .findViewById(R.id.download_zoomed_image_piechart);
    pieFractionView.setVisibility(View.GONE);

    final WebView webView = (WebView) view.findViewById(R.id.image_webview);

    // black background
    view.setBackgroundColor(0xFF000000);
    webView.setBackgroundColor(0xFF000000);

    final SlidableImageInfo imageInfo = mListImageMessages.get(position);

    String mediaUrl = imageInfo.mImageUrl;
    final int rotationAngle = imageInfo.mRotationAngle;
    final String mimeType = imageInfo.mMimeType;

    final MXMediasCache mediasCache = Matrix.getInstance(this.context).getMediasCache();
    File mediaFile = mediasCache.mediaCacheFile(mediaUrl, mimeType);

    // is the high picture already downloaded ?
    if (null != mediaFile) {
        if (mHighResMediaIndex.indexOf(position) < 0) {
            mHighResMediaIndex.add(position);
        }/*from w  w w .  j  a  va 2  s .  c om*/
    } else {
        // try to retrieve the thumbnail
        mediaFile = mediasCache.mediaCacheFile(mediaUrl, mMaxImageWidth, mMaxImageHeight, null);
    }

    // the thumbnail is not yet downloaded
    if (null == mediaFile) {
        // display nothing
        container.addView(view, 0);
        return view;
    }

    String mediaUri = "file://" + mediaFile.getPath();

    String css = computeCss(mediaUri, mMaxImageWidth, mMaxImageHeight, rotationAngle);
    final String viewportContent = "width=640";

    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setBuiltInZoomControls(true);

    loadImage(webView, Uri.parse(mediaUri), viewportContent, css);

    container.addView(view, 0);
    return view;
}

From source file:im.vector.adapters.VectorMediasViewerAdapter.java

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    View view = mLayoutInflater.inflate(R.layout.adapter_vector_medias_viewer, null, false);

    // hide the pie chart
    final PieFractionView pieFractionView = (PieFractionView) view.findViewById(R.id.media_slider_piechart);
    pieFractionView.setVisibility(View.GONE);

    final WebView imageWebView = (WebView) view.findViewById(R.id.media_slider_image_webview);
    final View videoLayout = view.findViewById(R.id.media_slider_videolayout);
    final ImageView thumbView = (ImageView) view.findViewById(R.id.media_slider_video_thumbnail);

    imageWebView.getSettings().setDisplayZoomControls(false);

    imageWebView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override/*from  w  ww  .j av  a 2 s  .c o m*/
        public boolean onLongClick(View v) {
            VectorMediasViewerAdapter.this.onLongClick();
            return true;
        }
    });

    thumbView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            VectorMediasViewerAdapter.this.onLongClick();
            return true;
        }
    });

    // black background
    view.setBackgroundColor(0xFF000000);
    imageWebView.setBackgroundColor(0xFF000000);
    videoLayout.setBackgroundColor(0xFF000000);

    final SlidableMediaInfo mediaInfo = mMediasMessagesList.get(position);
    String mediaUrl = mediaInfo.mMediaUrl;

    if (mediaInfo.mMessageType.equals(Message.MSGTYPE_IMAGE)) {
        imageWebView.setVisibility(View.VISIBLE);
        imageWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        imageWebView.getSettings().setJavaScriptEnabled(true);
        imageWebView.getSettings().setLoadWithOverviewMode(true);
        imageWebView.getSettings().setUseWideViewPort(true);
        imageWebView.getSettings().setBuiltInZoomControls(true);

        videoLayout.setVisibility(View.GONE);

        final int rotationAngle = mediaInfo.mRotationAngle;
        final String mimeType = mediaInfo.mMimeType;
        File mediaFile = mMediasCache.mediaCacheFile(mediaUrl, mimeType);

        // is the high picture already downloaded ?
        if (null != mediaFile) {
            if (mHighResMediaIndex.indexOf(position) < 0) {
                mHighResMediaIndex.add(position);
            }
        } else {
            // try to retrieve the thumbnail
            mediaFile = mMediasCache.mediaCacheFile(mediaUrl, mMaxImageWidth, mMaxImageHeight, null);
        }

        // the thumbnail is not yet downloaded
        if (null == mediaFile) {
            // display nothing
            container.addView(view, 0);
            return view;
        }

        String mediaUri = "file://" + mediaFile.getPath();

        String css = computeCss(mediaUri, mMaxImageWidth, mMaxImageHeight, rotationAngle);
        final String viewportContent = "width=640";
        loadImage(imageWebView, Uri.parse(mediaUri), viewportContent, css);
        container.addView(view, 0);
    } else {
        loadVideo(position, view, mediaInfo.mThumbnailUrl, mediaUrl, mediaInfo.mMimeType);
        container.addView(view, 0);
    }

    // check if the media is downloading
    String downloadId = mMediasCache.downloadMedia(mContext, mSession.getHomeserverConfig(), mediaUrl,
            mediaInfo.mMimeType);

    if (null != downloadId) {
        pieFractionView.setVisibility(View.VISIBLE);
        pieFractionView.setFraction(mMediasCache.getProgressValueForDownloadId(downloadId));
        pieFractionView.setTag(downloadId);

        mMediasCache.addDownloadListener(downloadId, new MXMediaDownloadListener() {
            @Override
            public void onDownloadError(String downloadId, JsonElement jsonElement) {
                pieFractionView.setVisibility(View.GONE);
                MatrixError error = JsonUtils.toMatrixError(jsonElement);

                if ((null != error) && error.isSupportedErrorCode()) {
                    Toast.makeText(VectorMediasViewerAdapter.this.mContext, error.getLocalizedMessage(),
                            Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onDownloadProgress(String aDownloadId, DownloadStats stats) {
                if (aDownloadId.equals(pieFractionView.getTag())) {
                    pieFractionView.setFraction(stats.mProgress);
                }
            }

            @Override
            public void onDownloadComplete(String aDownloadId) {
                if (aDownloadId.equals(pieFractionView.getTag())) {
                    pieFractionView.setVisibility(View.GONE);
                }
            }
        });
    }

    return view;
}

From source file:im.neon.adapters.VectorMediasViewerAdapter.java

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    View view = mLayoutInflater.inflate(R.layout.adapter_vector_medias_viewer, null, false);

    // hide the pie chart
    final PieFractionView pieFractionView = (PieFractionView) view.findViewById(R.id.media_slider_piechart);
    pieFractionView.setVisibility(View.GONE);

    final WebView imageWebView = (WebView) view.findViewById(R.id.media_slider_image_webview);
    final View videoLayout = view.findViewById(R.id.media_slider_videolayout);
    final ImageView thumbView = (ImageView) view.findViewById(R.id.media_slider_video_thumbnail);

    imageWebView.getSettings().setDisplayZoomControls(false);

    imageWebView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override/*from w w w  . j  av  a 2 s.  co  m*/
        public boolean onLongClick(View v) {
            VectorMediasViewerAdapter.this.onLongClick();
            return true;
        }
    });

    thumbView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            VectorMediasViewerAdapter.this.onLongClick();
            return true;
        }
    });

    // black background
    view.setBackgroundColor(0xFF000000);
    imageWebView.setBackgroundColor(0xFF000000);
    videoLayout.setBackgroundColor(0xFF000000);

    final SlidableMediaInfo mediaInfo = mMediasMessagesList.get(position);
    String mediaUrl = mediaInfo.mMediaUrl;

    if (mediaInfo.mMessageType.equals(Message.MSGTYPE_IMAGE)) {
        imageWebView.setVisibility(View.VISIBLE);
        imageWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        imageWebView.getSettings().setJavaScriptEnabled(true);
        imageWebView.getSettings().setLoadWithOverviewMode(true);
        imageWebView.getSettings().setUseWideViewPort(true);
        imageWebView.getSettings().setBuiltInZoomControls(true);

        videoLayout.setVisibility(View.GONE);

        final int rotationAngle = mediaInfo.mRotationAngle;

        if (TextUtils.isEmpty(mediaInfo.mMimeType)) {
            mediaInfo.mMimeType = "image/jpeg";
        }

        final String mimeType = mediaInfo.mMimeType;
        File mediaFile = mMediasCache.mediaCacheFile(mediaUrl, mimeType);

        // is the high picture already downloaded ?
        if (null != mediaFile) {
            if (mHighResMediaIndex.indexOf(position) < 0) {
                mHighResMediaIndex.add(position);
            }
        } else {
            // try to retrieve the thumbnail
            mediaFile = mMediasCache.mediaCacheFile(mediaUrl, mMaxImageWidth, mMaxImageHeight, null);
        }

        // the thumbnail is not yet downloaded
        if (null == mediaFile) {
            // display nothing
            container.addView(view, 0);
            return view;
        }

        String mediaUri = "file://" + mediaFile.getPath();

        String css = computeCss(mediaUri, mMaxImageWidth, mMaxImageHeight, rotationAngle);
        final String viewportContent = "width=640";
        loadImage(imageWebView, Uri.parse(mediaUri), viewportContent, css);
        container.addView(view, 0);
    } else {
        loadVideo(position, view, mediaInfo.mThumbnailUrl, mediaUrl, mediaInfo.mMimeType);
        container.addView(view, 0);
    }

    // check if the media is downloading
    String downloadId = mMediasCache.downloadMedia(mContext, mSession.getHomeserverConfig(), mediaUrl,
            mediaInfo.mMimeType, mediaInfo.mEncryptedFileInfo);

    if (null != downloadId) {
        pieFractionView.setVisibility(View.VISIBLE);
        pieFractionView.setFraction(mMediasCache.getProgressValueForDownloadId(downloadId));
        pieFractionView.setTag(downloadId);

        mMediasCache.addDownloadListener(downloadId, new MXMediaDownloadListener() {
            @Override
            public void onDownloadError(String downloadId, JsonElement jsonElement) {
                pieFractionView.setVisibility(View.GONE);
                MatrixError error = JsonUtils.toMatrixError(jsonElement);

                if ((null != error) && error.isSupportedErrorCode()) {
                    Toast.makeText(VectorMediasViewerAdapter.this.mContext, error.getLocalizedMessage(),
                            Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onDownloadProgress(String aDownloadId, DownloadStats stats) {
                if (aDownloadId.equals(pieFractionView.getTag())) {
                    pieFractionView.setFraction(stats.mProgress);
                }
            }

            @Override
            public void onDownloadComplete(String aDownloadId) {
                if (aDownloadId.equals(pieFractionView.getTag())) {
                    pieFractionView.setVisibility(View.GONE);
                }
            }
        });
    }

    return view;
}