How to disable anti-aliasing when scaling up an imageview in android? - android

I have a pixel art that I want to scale up. The problem is when I scale it up, it gets all blurred cause of the antialiasing. Is there a way to disable antialiasing directly from xml?

i hope its useful
Bitmap bmpSource = BitmapFactory.decodeResource(getResources(), SourceFileId);
//100 is dimension to resizing image size
//Passing filter = false will result in a blocky, pixellated image.
//Passing filter = true will give you smoother edges
Bitmap bmpScaled = Bitmap.createScaledBitmap(bmpSource, 100, 100, false);
ImageView img = (ImageView) findViewById(Your imageView Control Id);
img.setImageBitmap(bmpScaled);
is this what u want!?

Related

Blur effect on background

I need to show something like this in an ImageView
Do you know a method to set an image in an ImageView like so?
Image in background is bluerd and image on it is with blur effect
ImageView imageView=(ImageView)findViewById(R.id.imageView2);
imageView.setImageBitmap(bitmap);
I need put in bitmap my picture and fuzzy picture in background.
this only sets one picture and I need put some fuzzy background
I know how put only one image in 100% quality.
But I don't know how set the background image to this picture.
I don't know how this "filter" name and I could not Google for that.
From https://stackoverflow.com/a/36193733:
I recently came across Renderscript API.
//Set the radius of the Blur. Supported range 0 < radius <= 25
private static final float BLUR_RADIUS = 25f;
public Bitmap blur(Bitmap image) {
if (null == image) return null;
Bitmap outputBitmap = Bitmap.createBitmap(image);
final RenderScript renderScript = RenderScript.create(this);
Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);
//Intrinsic Gausian blur filter
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
Use the above code snippet in the image view as shown below.
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.nature);
Bitmap blurredBitmap = blur(bitmap);
imageView.setImageBitmap(blurredBitmap);
Dont forget to add below lines in build.gradle file
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
You can use View Pager and Add ImageView in each page, that way you can switch between images,
Set your fuzzy/blurry image as the background, using
android:background = "#drawable/myFuzzyImage"
Set your clear image as you would normally, just like you have, using the setImageBitmap, or using an ImageView in the xml file of your activity layout.

How can i take screenshot of rotated TextView with emoji?

I'm trying to make a screenshot of the rotated TextView which contain emoji icons. But on resulting bitmap i see that emoji are not rotated! Why is this happening? How can i make a screenshot with rotated emoji ?
What i expect:
And this is what i get:
I'm using this method to get screenshot of view:
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
Bitmap bitmap = null;
if (layout.getDrawingCache() != null)
bitmap = layout.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
layout.setDrawingCacheEnabled(false);
layout.destroyDrawingCache();
Update:
as i figured, if I set textView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); then emoji will not be rotated even in the TextView (if you rotate TextView - they will not be rotated, they will be just moving around like a carousel), but still I don't really understand why is this happening, or rotation of emoji (on first picture) is only because of hardware acceleration?
ok, i couldnt find a way to solve this really annoying issue, so i had to hack it a bit.
imageviews works perfectly with rotation.
so i basically do all the manipulations with image view - and setting it's image out of the emoji text i want using this method:
private Bitmap generateBitmapFromText(int size, String res) {
TextView tv = new TextView(getContext());
tv.setText(res);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 150f);
tv.setTextColor(0xffffffff);
tv.measure(size, size);
int questionWidth = tv.getMeasuredWidth();
int questionHeight = tv.getMeasuredHeight();
Bitmap bitmap = Bitmap.createBitmap(questionWidth, questionHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
tv.layout(0, 0, questionWidth, questionHeight);
tv.draw(c);
return bitmap;
}
and then i call
Bitmap bitmap = generateBitmapFromText(stickersStartingSize, res);
imageview.setImageBitmap(bitmap);

How to set bitmap size that fill parent?

I want to creat a bit map in the size that fill parent of the app window. how can i do that?
Bitmap maskImage = BitmapFactory.decodeResource(getResources(), R.drawable.img);
ImageView on = (ImageView) findViewById(R.id.on);
Bitmap result = Bitmap.createBitmap(maskImage.getWidth(), maskImage.getHeight(), Bitmap.Config.ARGB_8888);
on.setImageBitmap(result);
Only a single additional line is needed:
Once you have your ImageView:
ImageView on = (ImageView) findViewById(R.id.on);
on.setImageBitmap( . . . );
on.setScaleType(ScaleType.FIT_XY);
ScaleTypes are options for scaling the bounds of your image. You can read more about ScaleTypes in the official docs.

Getting ColorMode from a ImageView Bitmap

Looking at this example, Im trying to determine of the image in view is of CMYK colormode or not. If it it, I it must not try to display it, and continue with the next image in my list
This is how I am setting up my ImageView
options.inSampleSize = calculateInSampleSize(options, imageWidth, imageWidth);
imageView = (ImageView)findViewById(R.id.imageView);
Bitmap image = decodeSampledBitmapFromResource(path, 1280, 700);
imageView.setImageBitmap(image);
imageView.setVisibility(View.VISIBLE);
How do I determine if this image is CMYK ? Or simply said, I do I determine if this image can be displayed or not

Applying an overlay (image filter) to a Bitmap

I am trying to capture an image from the camera intent and then apply an image filter on it. To elaborate the image would be an image captured by the camera and the image filter would be available in the resources as a png file. I am able to overlay the filter on top of the original image. But, once overlay-ed the original image is 'almost' invisible (which means that the filter is infact being stacked on the original image and not merely replacing it). I have a couple of images to illustrate my problem. The first image was in Photoshop - when I placed a filter on top of an image, it seemed just fine. The second image is the produced by the code cited below - you can clearly see that the filter effect is missing. Would someone have a clue as to why something like this is occurring. Am I missing some logic here?
The following is the code that I have. I apologize if you find any best practices missing here. I am initially trying to test the code:
mPictureView = (ImageView) findViewById(R.id.pictureView);
filterButton = (Button) findViewById(R.id.filter_button1);
// define the threshold fro scaling the image
private final double SCALE_THRESHOLD = 6.0;
// acquire the bitmap (photo captured) from the Camera Intent - the uri is
// passed from a previous activity that accesses the camera and the current
// activity is used to display the bitmap
Uri imageUri = getIntent().getData();
Bitmap imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
// set the imageView in the current activity to display the picture retrieved
// from the camera
mPictureView.setImageBitmap(imageBitmap);
// get the dimensions of the original bitmap
int photoWidth = imageBitmap.getWidth();
int photoHeight = imageBitmap.getHeight();
filterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// set the options
Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
// get the image (png file) filter from resources using the options
Bitmap filter = BitmapFactory.decodeResource(getResources(), R.drawable.colorful_filter,options);
// create a scaled copy of the filter
Bitmap filtercopy = Bitmap.createScaledBitmap(filter, (int)(photoWidth/SCALE_THRESHOLD,(int)(photoHeight/SCALE_THRESHOLD), true);
// recycle the used bitmap
filter.recycle();
filter = null;
// get a scaled, mutable copy of the orginial image
Bitmap imagecopy = Bitmap.createScaledBitmap(imageBitmap,(int)(photoWidth/SCALE_THRESHOLD), (int)(photoHeight/SCALE_THRESHOLD),true);
// recycle the used bitmap
imageBitmap.recycle();
imageBitmap = null;
Paint paint = new Paint();
paint.setAntiAlias(true);
//paint.setAlpha(230); - if a discrete value is set, then the image beneath
// the filter is visible. But, I don't understand why I need to do this.
// Besides, that reduces the efficacy of the filter
// create a canvas with the original image as the underlying image
Canvas canvas = new Canvas(imagecopy);
// now, draw the filter on top of the bitmap
canvas.drawBitmap(filtercopy, 0, 0, paint);
// recycle the used bitmap
filtercopy.recycle();
filtercopy = null;
//set the filtered bitmap as the image
mPictureView.setImageBitmap(imagecopy);
}
EDIT 1: I was able to make some progress with the help of the article that Joru has provided. The problem seems to be with blending of the 2 bitmaps. The method drawBitmap would just draw one bitmap over the other in the situation that I have. The following line of code will actually attempt to blend the 2 bitmaps. I have also attached an image which depicts my progress. The underlying bitmap is noticeably more visible now:
paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
I have to still play around with it for some time, before achieving the desired output.
You could try a few things:
Bitmap new = old.copy(Config.ARGB_8888, true);
To make sure the bitmap you are opening from MediaStore is in that format. If it isn't, that is likely to cause your problem.

Categories

Resources