How can I change my Bitmap from drawCircle into an imported image in drawable?
I have this code:
canvas.drawCircle((currentX * totalCellWidth)+(cellWidth/2),
(currentY * totalCellHeight)+(cellWidth/2),
(cellWidth*0.45f),ball);
...which produces this output:
I tried this change to substitute a bitmap resource for the circle:
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(b, (currentX * totalCellWidth)+(cellWidth/2),
(currentY * totalCellHeight)+(cellWidth/2),
ball);
...but it doesn't seem to work.
If you are trying to change the size of the bitmap.
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
// assign value for the new target size
int newsizeX = width;
int newsizeY = height;
resizedbitmap = Bitmap.createScaledBitmap(resizedbitmap, width, height, true);
canvas.drawBitmap(resizedbitmap, *location in X*, *location in Y*, paint);
it should be working perfectly :)
Is your ic-launcher in your drawable folder? Because my ic-launcher bitmaps are in my drawable-hdpi/ldpi/mdpi/etc folders. If it is, try changing R.drawable.ic_launcher to R.drawable-hdpi/ldpi/mdpi/etc.ic_launcher
Related
I am trying to implement overlay transformation for Picasso library.
So, I am loading image into image view and want to display overlay using bitmpap
First of all I am fill canvas with transparent color
Then I am trying to draw drawable over
#Override
public Bitmap transform(Bitmap source){
Bitmap workingBitmap=Bitmap.createBitmap(source);
Bitmap mutableBitmap=workingBitmap.copy(Bitmap.Config.ARGB_8888,true);
Canvas canvas=new Canvas(mutableBitmap);
canvas.drawColor(getResources().getColor(R.color.gray_transparent));
BitmapFactory.Options options=new BitmapFactory.Options();
options.inScaled=false;
Bitmap b=BitmapFactory.decodeResource(getResources(),R.drawable.ic_gif_white_24dp,options);
canvas.drawBitmap(b,source.getWidth()/2,source.getHeight()/2,paint);
source.recycle();
return mutableBitmap;
}
The problem is that I see different overlay image size when canvas size is different. How I can avaid bitmap scaling depends on canvas size? I want to see same GIF size with different canvases size.
On first image canvas has w = 1280, h = 854, second image w = 480, h = 270
UPDATE
I found solution to scale bitmap depends on canvas size, it works well, but I wish to find out better solution
BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.ic_gif_white_24dp);
Bitmap drawableBitmap = drawable.getBitmap();
double size = source.getWidth() * 0.15;
Bitmap scaled = Bitmap.createScaledBitmap(drawableBitmap, (int) size, (int) size, true);
I need to test a simple task. I want to create scaled bitmap by setting postScale to the Matrix and using it in the creation, here's a code:
Matrix matrix = new Matrix();
matrix.postScale(5.0f, 5.0f);
Bitmap bitmap = Bitmap.createBitmap(bitmapSrc, 500, 500, 50, 50, matrix, true);
I thought this code supposed to crop 50x50 bitmap from the source scaled in 5 times, but when i'm using this bitmap to show the result in ImageView
imageView.setImageBitmap(bitmap);
The scaling doesn't seem to work and i'm getting 50x50 bitmap from original source bitmap(without scaling).
I think i'm missing something, but i can't quite figure out what. Any help highly appreciated
Edit: I've also tried to set last parameter to false and it didn't help, but if i'm using postRotate in matrix i'm getting rotated bitmap
Android contains the function Bitmap.createScaledBitmap()...
You can use this as follows:
public Bitmap getScaledBitmap(Bitmap bitmap, float scale) {
Integer originalHeight = bitmap.getHeight();
Integer originalWidth = bitmap.getWidth();
Integer requiredHeight = Math.round(originalHeight * scale);
Integer requiredWidth = Math.round(originalWidth * scale);
return Bitmap.createScaledBitmap(bitmap, requiredWidth, requiredHeight, true);
}
You can checkout this for other relevant functions here.
It might be that the scaling happens too late and the crop area is out of bounds because of it. Did you try it with preScale instead of postScale?
If that does not work, you can try using coordinates within the small bitmap first, like this:
Bitmap bitmap = Bitmap.createBitmap(bitmapSrc, 100, 100, 10, 10, matrix, true);
I trying to achieve water reflection effect on bitmap. As I saw some apps called water reflection. I know how to do the reflection of the image but the wave on the image is what making me confused on how it is done.
see this image for example
I did many apps on bitmap manipulation but this is quite hard to achieve.
So any idea on where to start. Just an idea to start can be helpful.
For any one needed, I tried some simple tricks to get as closer as water reflection effect. It is not great but it looks fine to me.
I used two methods
Bitmap reflection method (give bitmap as a parameter)
public static Bitmap Reflection(Bitmap imageBitmap) {
int width = imageBitmap.getWidth();
int height = imageBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(imageBitmap, 0,
0, width, height , matrix, false);
Bitmap newbit=Bitmap.createScaledBitmap(reflectionImage, reflectionImage.getWidth()/8, reflectionImage.getHeight()/8, true);
Bitmap newbit1=Bitmap.createScaledBitmap(newbit, newbit.getWidth()*8, newbit.getHeight()*8, true);
Bitmap scalednew=Bitmap.createScaledBitmap(newbit1, width, height-(height/4), true);
Bitmap newscaledone=overlay(scalednew);
reflectionImage=newscaledone;
Bitmap reflectedBitmap = Bitmap.createBitmap(width,
(height + height), Config.ARGB_8888);
Canvas canvas = new Canvas(reflectedBitmap);
canvas.drawBitmap(imageBitmap, 0, 0, null);
Paint defaultPaint = new Paint();
canvas.drawRect(0, height, width, height, defaultPaint);
canvas.drawBitmap(reflectionImage, 0, height , null);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, reflectedBitmap.getHeight()
, paint);
return reflectedBitmap;
}
Bitmap overlay method. I am taking a wave bitmap with some opacity to overlay on the reflected image. So that it may look like water.
Bitmap wavebitmap=BitmapFactory.decodeResource(getResources(), R.drawable.waves1);
private static Bitmap overlay( Bitmap bmp2) {
Bitmap bmp1=WaterReflectionMainActivity.wavebitmap;
Bitmap bmp1new =Bitmap.createScaledBitmap(bmp1, bmp2.getWidth(), bmp2.getHeight(), true);
Bitmap bmOverlay = Bitmap.createBitmap(bmp1new.getWidth(), bmp1new.getHeight(), bmp1new.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp2, new Matrix(), null);
canvas.drawBitmap(bmp1new, new Matrix(), null);
return bmOverlay;
}
Well this is my version of water effect, I know this looks shit.
So if anyone still got some better effect please share your code .
thank you
Tutorial related to this: http://www.xaraxone.com/webxealot/workbook34/page_4.htm
Also have a read at this question: Add water effect on bitmap android.
Have a read at both of them, i hope you will get an idea from this
You may also want to look through these: 1, 2, 3
This is just an idea but basically, what you need is to apply a deformation on the bottom part of the image, meaning that for each pixel on the bottom half, you compute a position to get it's color from the top picture.
Here's a pseudo code to give you a hint :
for (int x = 0; x < width; x++) {
for (int y = 0; y < img.height; y++) {
// Compute a position on the original image
// tweak the values heres to get the effect you want
sourceX = x + (int) (cos(10000.0 / y) * 20);
sourceY = img.height - y - 1 +(int)( sin(y* 0.5) * 20);
// Maybe check the range of sourceX and source Y
int color = img.getColor(sourceX, sourceY)
outptut.setColor(x, y + img.height, color);
}
}
you can achieve this by masking may this code will help you
http://www.seeques.com/22527681/how-can-do-this-effect-in-android-may-be-android-bitmap-masking-effect.html
EDIT
also see this for reference
http://code.google.com/p/android-ripple-demo/source/browse/#svn%2Ftrunk%2Fsrc%2Fcom%2Fkesalin%2FRippleDemo
https://github.com/esteewhy/whater
http://code.google.com/p/waterrippleeffect/source/browse/trunk/src/com/example/android/watereffect/WaterEffectView.java?r=3
android noise effect on bitmap
I have some question about water mark within android code!
Following code showed my idea about WaterMark!
However,It does not work normally.
e.g. only the image end with .png can be watered mark
Is there a scheme about water mark(.jpeg, .jpg, .wbmp, .bmp, .png or others)
protected static Bitmap getDrmPicture(Context context,String path){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap originMap = BitmapFactory.decodeFile (path,options);
Bitmap waterMark = BitmapFactory.decodeResource(context.getResources(), R.drawable.close);
InputStream input;
byte[] b;
Bitmap waterMark = null;
try {
input = context.getResources().openRawResource(R.drawable.lock);
b = new byte[input.available()];
input.read(b);
waterMark = DecodeUtils.requestDecode(jc, b, null);
}catch(IOException e){
}
int w = originMap.getWidth();
int h = originMap.getHeight();
int ww = waterMark.getWidth();
int wh = waterMark.getHeight();
Bitmap newb = Bitmap.createBitmap(w, h,Bitmap.Config.ARGB_8888;);
Canvas cv = new Canvas(newb);
cv.drawBitmap(originMap, 0, 0, null);
cv.drawBitmap(waterMark, w - ww, h - wh, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newb;
}
Thanks !
This is the code I use to apply watermark to a jpeg, it should work for you too,
public Bitmap applyWatermarkColorFilter(Drawable drawable) {
Bitmap image = ((BitmapDrawable)drawable).getBitmap();
Bitmap result = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(image, 0, 0, null);
Bitmap watermark = BitmapFactory.decodeResource(getResources(), R.drawable.watermark);
canvas.drawBitmap(watermark, image.getWidth()/2 - watermark.getWidth()/2,
image.getHeight()/2 - watermark.getHeight()/2,
null);
return result;
}
Basically after this u have to use Bitmap.compress(<arguments>) to get a jpg out of it.
Din't try for the other formats. May be it might be possible if you can extract the Bitmap out of them like how we do for jpg and png.
https://stackoverflow.com/questions/6756975/draw-multi-line-text-to-canvas
Measure height of multiline text
To center text vertically we need to know text height. Instantiate StaticLayout with text width according to your needs, for us it is simple the width of Bitmap/Canvas minus 16dp padding. The getHeight() then returns height of text.
Positioning text on Canvas
There are four simple steps to position text on Canvas:
Save the current matrix and clip with Canvas.save().
Apply translation to Canvas matrix with Canvas.translate(x,y).
Draw StaticLayout on Canvas with StaticLayout.draw(canvas).
Revert matrix translation with Canvas.restore() method.
I have a LayerDrawable that I am creating in code because I have to do some color filtering to individual components of the layered image. On top of the layered image I have to ImageViews on top of one another and one is set to invisible. When the visible ImageView is clicked it rotates to 90 and made invisible and the invisible ImageView is made visible and rotated from -90 to 0.
My issue is when I added the LayerDrawable which is made up of 8 pngs to the Activity the animation didn't work as well as before. It got very choppy or didn't appear to rotate at all.
Does anyone have any ideas regarding this?
I am not sure that this is the best way but what I have ended up doing was compositing the image myself. By drawing all 8 images onto one bitmap and applying that single bitmap to the ImageView I was able to get my animation to perform as I wanted. Now when I want to update a section of the composited image I have to make the change to the constituent bitmap and apply that to my Image View.
final BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeResource( this.getResources(), this.houseMatrix[0][0], opt );
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final Bitmap result = Bitmap.createBitmap( width, height, Config.ARGB_8888 );
final Paint paint = new Paint();
paint.setColorFilter( new PorterDuffColorFilter( this.houseMatrix[0][1], PorterDuff.Mode.MULTIPLY ) );
final Canvas canvas = new Canvas( result );
canvas.drawBitmap( bitmap, 0, 0, paint );
bitmap.recycle();
for ( int i = 1; i < 8; i++ ) {
paint.setColorFilter( new PorterDuffColorFilter( this.houseMatrix[i][1], PorterDuff.Mode.MULTIPLY ) );
bitmap = BitmapFactory.decodeResource( this.getResources(), this.houseMatrix[i][0], opt );
canvas.drawBitmap( bitmap, 0, 0, paint );
bitmap.recycle();
}
this.house.setImageBitmap( result );