I have some problems with webview content size. I need to convert full web page to a single image and tried this code
Bitmap screenshot;
screenshot = Bitmap.createBitmap(view.getWidth(), view.getContentHeight(), Bitmap.Config.ARGB_8888);
final Canvas c =new Canvas(screenshot);
view.draw(c);
where "view" is a WebView object
But the result is an image that have 1024x769 pixels. My web page is quite bigger (height is about 2000px). I've tried different ways to solve this problem, but still with zero rezult.
I find solution.
If you have the same problem, you can add onPicture Listener to your webview, like this
web.setPictureListener(new WebView.PictureListener() {
public void onNewPicture(WebView view, Picture picture) {
float temp = (float) view.getHeight();
height = view.getContentHeight() * a;
}
});
and get all your need height and width
For taking the picture of webview you have to enabled cache and from cache you can get it as a bitmap ,below i am mentioning the code hope will useful for you-
webview.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
it is working for me just check your side,will wait for your acceptance of answer.thanks
Related
This is driving me crazy. I would like to be able to resize an xml vector drawable icon programmatically in order to use it in an ImageView.
This is what I've done so far which is not working
Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.ic_marker,null);
drawable.setBounds(0,0,512,512);
imageVenue.setImageDrawable(drawable);
The vector icon ic_marker is not resized. It just keeps the hardcoded width and height values every time.
Any ideas?
You can change the width and height of your imageview programmatically. Since vector drawables will preserve the original quality of the image, this will make the desired output happen.
ImageView iv = (ImageView) findViewById(R.id.imgview);
int width = 60;
int height = 60;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(params);
I'm currently facing the same problem.
I'm trying something like this, cause ViewParent has actually height set explicitly, so I use match_parent and set margins. It doesn't work all the time though, cause I simply use this view in a viewholder for RecyclerView... Also I've noticed that sometimes I see scaled up version with artifacts, sometimes full size, sometimes there are margins, and bigger margins... But it still might work for you, if you use it in a simpler scenario.
mImageViewFront.setImageDrawable(vectorDrawable);
final int paddingLR = mImageViewFront.getWidth() / 4;
final int paddingTB = mImageViewFront.getHeight() / 4;
LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.setMargins(paddingLR, paddingTB, paddingLR, paddingTB);
mImageViewFront.setLayoutParams(params);
I'm trying to build an simple image-zoom-replace functionality based on the Image-Zoom-Library "PhotoView":
https://github.com/chrisbanes/PhotoView
I just want to load a small image into a imageView and zoom/scale it. This code works perfectly:
ImageView galleryPictureNew = (ImageView)findViewById(R.id.galleryImage);
PhotoViewAttacher mAttacher = new PhotoViewAttacher(view);
mAttacher.update();
The problem is, i want to replace the small image with a large (hd-image) if i zoom in.
There is function called "setOnScaleChangeListener" like this:
mAttacher.setOnScaleChangeListener(new PhotoViewAttacher.OnScaleChangeListener() {
#Override
public void onScaleChange(float scaleFactor, float focusX, float focusY) {
//REPLACE IMAGE
}
});
This code above works, too BUT i want to "jump" to the last "position" and "zooming-level"?!
Does anyone has a solution for it?
Or maybe someone has an alternative library for this problem?
Do i have to save the last "position-state"?
I have the following code which I got from a youtube tutorial for drawing canvas:
(https://www.youtube.com/watch?v=6NBt36fO0iw):
#Override
protected void onDraw(Canvas canvasporch) {
canvasporch.drawColor(Color.BLACK);
for (int i = temps.size() - 1; i >= 0; i--){
temps.get(i).onDraw(canvasporch);
for (Sprite sprite : sprites){
sprite.onDraw(canvasporch);
This works just fine. The only problem is instead of having the color Black as background, I want to display an image from my drawable folder as the background for my sprites. I've tried using drawBitmap() but I cannot seem to make it work. Thanks.
1) Create a member variable to store the Bitmap:
Bitmap spriteBackground;
2) In onCreate() (or some other initialization function) load the bitmap from the drawable:
spriteBackground = BitmapFactory.decodeResource(getResources(), R.drawable.sprite_background);
3) Draw the bitmap using drawBitmap() inside onDraw():
canvasporch.drawBitmap(spriteBackground, 0, 0, null);
If you want to scale the bitmap to cover the canvas then either scale it at load time using createScaledBitmap (uses more memory) or apply a matrix transform to the canvas (possibly slower).
Another way is to pass the canvas bounds to drawBitmap(), like this:
canvasporch.drawBitmap(spriteBackground, null, canvasporch.getClipBounds(), null);
I'm trying to draw on an ImageViewTouch, a library which enables pinch zooming. I'm able to draw over the image using Canvas, but when I zoom the image, the drawing disappears.
For this, I'm trying to convert the view to a bitmap and set theImageBitmap for this same view. Here's the code:
mImage.setDrawPath(true);
mImage.setImageBitmap(loadBitmapFromView(mImage));
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getWidth(), v.getHeight());
v.draw(c);
return b;
}
When I do this, I get the following log error:
07-11 21:13:41.567: E/AndroidRuntime(20056): java.lang.IllegalArgumentException: width and height must be > 0
07-11 21:13:41.567: E/AndroidRuntime(20056): at android.graphics.Bitmap.createBitmap(Bitmap.java:638)
07-11 21:13:41.567: E/AndroidRuntime(20056): at android.graphics.Bitmap.createBitmap(Bitmap.java:620)
If I remove the loadBitmapFromView call the drawing normally appears over the image. When I try to do any interaction with the image (like zooming in or out), the drawing disappears, remaing only the background image, which is a picture.
--- EDIT ---
Here's some more code placed after the loadBitmapFromView call. The case is: I have a radio group listenner and when I check some radio button, I have to load the image and draw some possibles drawings over it.. then I'm trying to convert everything (the image and the drawings) into only one bitmap.
Here's the ohter part of the code:
bitmap = BitmapUtils.decodeSampledBitmapFromResource(root + DefinesAndroid.CAMINHO_SHOPPINGS_SDCARD + nomeImagemAtual, size.x, size.y);
mImage.setImageBitmap(bitmap);
After that, I draw everything I have to draw and try to convert the view to bitmap using the loadImageBitmap method I have shown.
the decodeSampledBitmapFromResource method I got from this link on android developers http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
I finally figured out a solution for my problem.
I'm using the post method of the View, which will be executed only after the view measuring and layouting, this way getWidth() and getHeight() returns the actual width and height.
Here's the code sample:
mImage.post(new Runnable() {
#Override
public void run() {
mImage.setImageBitmap(loadBitmapFromView(mImage));
}
});
Hope it also helps someone else :)
If everything else is correct you are executing your code too early.
The view's layout has not yet been measured by Android. Try executing in OnResume just to see if this is your problem.
Cheers!
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
loadBitmapFromView(mImage);
}
}, 1000);
java.lang.IllegalArgumentException: width and height must be > 0 is because in your Bitmap.createBitmap() call v.getLayoutParams().width or v.getLayoutParams().height is 0. Probably the LayoutParams are wrongly set in mImage.
Update: setLayoutParams() for the mImage before using createBitmap(). Something like this:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
mImage.setLayoutParams(layoutParams);
In my case, I forgot to set orientation to vertical for parent LinearLayout and the default value of horizontal has been set, so I set it and my problem gone...
android:orientation="vertical"
I'm not sure if it relates to this specific question , but I had the same error and I solved it with getting a image source , in the layout xml file , from drawable instead of from mipmap (of course I had to put the image there before).
I have a webView that I manipulate with this code: (this all works fine)
WebView myWebView = (WebView) findViewById(R.id.stats_webview);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setPadding(0,0,0,0);
myWebView.setInitialScale(getScale());
Which gets the initial scale from this method
private int getScale(){
Display display = ((WindowManager)
getSystemService(StatsActivity.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
Double val = new Double(width)/new Double(720);
val = val * 100d;
return val.intValue();
}
That all works fine and loads the view slightly scaled down to fit the screen from my 720pixel web source. The problem is that once someone zooms in, they cannot zoom back out all the way to the initial scale. They can only zoom back out to a scale of 1, so they are unable to fit the entire page back on the screen. I've found references to a setMinimumScale method but it doesn't seem to be available any more. Am I just missing an include file or is there a new way to accomplish this?
I have tried:
myWebView.setMinimumScale(getScale());
and
myWebView.getSettings().setMinimumScale(getScale());
but neither works.
If editing the HTML file is an option, you can use the viewport meta tag.
check the documentation for more details.