How to zoom in/out an drawable imageView on some condition satisfied - android

helo,
I am new to android and trying to implement some action. I have one drawable image set in imageview. I would like to zoomIn it at some specific Coordinates on ImageView When some condition (specified) is satisfied. And on satisfying some other condition I like to zoom Out that image. how can I achieve this functionality. Can any one help me...please.

You have to create an empty mutable bitmap object as described below and you can do alterations to this bitmap and then setImageBitmap using imageview.
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(alteredBitmap);
Paint paint = new Paint();
Matrix matrix = new Matrix();
matrix.setScale(1.5f, 1.0f,0, 0);
canvas.drawBitmap(bmp, matrix, paint);
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageBitmap(alteredBitmap);

Related

Fit image inside png Image (Frame)

I have a png image which I want to use as a frame for images in ImageView.
Sorry, it's white that's why invisible
Like this but with sides curved
What is the solution?
I've tried by converting png to patch 9, setting background of parent and source of image view, but none worked. Image doesn't fit inside.
I've done it using PorterDuff. Designed an extra image just with curved border and applied PorterDuff.Mode.SRC_OVER inside setImageResource of Custom Image View.
Here is the overriden function
#Override
public void setImageResource(int resId) {
Bitmap rawBmp = BitmapFactory.decodeResource(getContext().getResources(), resId);
Bitmap rawMask = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.image_frame_arc_filled);
Bitmap rawBorderMask = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.image_frame_arc);
Bitmap bmp = Bitmap.createScaledBitmap(rawBmp, rawBmp.getWidth(), rawBmp.getHeight(), true);
Bitmap mask = Bitmap.createScaledBitmap(rawMask, bmp.getWidth(), bmp.getHeight(), true);
Bitmap borderMask = Bitmap.createScaledBitmap(rawBorderMask, mask.getWidth(), mask.getHeight(), true);
Bitmap bitmap = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bmp, 0, 0, null);
Paint maskPaint = new Paint();
maskPaint.setXfermode(
new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(mask, 0, 0, maskPaint);
maskPaint = new Paint();
maskPaint.setXfermode(
new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
canvas.drawBitmap(borderMask, 0, 0, maskPaint);
this.setImageBitmap(bitmap);
}

Android Image Masking with Rotate & Scale

How to mask image such as we can scale/zoom/rotate the background image but not the mask?
I should be able to zoom the image in this mask but it is scaling whole image.
Suggest me a way to achieve this, I'm creating a photo collage app.
Blue color is background of Layout.
The white color is for mask
I'm able to achieve this type of layout with masking, but when I apply MultiTouchListener to scale and zoom, it will scale the whole image with mask and not the image inside it.
private void getMaskedBitmap() {
Bitmap bgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background_drawable);
ImageView bg = (ImageView) findViewById(R.id.bg);
bg.setImageBitmap(bgBitmap);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap emptyBitmap = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), conf);
Canvas canvasBmp = new Canvas(bgBitmap);
ImageView mImageView = (ImageView) findViewById(R.id.troll_face);
Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.random_drawable);
Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask_drawable);
original = Bitmap.createScaledBitmap(original, mask.getWidth(), mask.getHeight(), true);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
mImageView.setImageBitmap(result);
//mImageView.setScaleType(ScaleType.FIT_XY);
mImageView.setBackgroundResource(R.drawable.background_drawable);
bg.setOnTouchListener(new MultiTouchListener());
mImageView.invalidate();
}
Exception on line
Canvas canvasBmp = new Canvas(bgBitmap);
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
I cannot provide you code but here is what you should do:
Get background image as bitmap and draw it on view's canvas.
Create an new (empty) bitmap of same size (use Bitmap.Config.ARGB_8888) and get its canvas, here you will draw mask.
Canvas canvasBmp = new Canvas(bmp);
Now draw bmp on view's canvas. So, that you can get mask effect on image.
Zoom (re-size etc whatever you want) background bitmap image.
Invalidate your view, and follow all steps again

Create new bitmap with black remain region issue

I have to create new bitmap from an original image after user do somethings with it like zooming, rotating, dragging. And i have a frame border, and only part of original image lay on frame is picked. Issue is that remain region of frame is black, and i want it could be transparent or white. How to do that? Thanks in advance.
public Bitmap createBitmap(final Matrix pMatrix, final Bitmap pSourceBitmap){
Bitmap bmp = Bitmap.createBitmap(width, height, pSourceBitmap.getConfig());
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(pSourceBitmap, pMatrix, new Paint());
return bmp;
}
To make the remaining portion transparent
use the following code
public Bitmap createBitmap(final Matrix pMatrix, final Bitmap pSourceBitmap)
{
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp= Bitmap.createBitmap(width, height, conf);
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(pSourceBitmap, pMatrix, new Paint());
return bmp;
}

Add text on different position on the images in Android

I have an image view. I need to display the various text on images.For each image the position of the text gets differ.I will give position value using x,y co ordinates of ImageView.
Any guesses How to perform this?
Create a bitmap
Create a canvas
Draw what you need
Post it to the imageview
Like this:
Bitmap drawingSurface = Bitmap.createBitmap(picBitmap.getWidth(), picBitmap.getHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(drawingSurface);
canvas.drawBitmap(picBitmap, 0, 0, null);
canvas.drawText("Hi!", 10, 10, new Paint());
myImageView.setImageBitmap(drawingSurface);
Encapsulate the image and text in a layout then give coordinates to the views inside their parrent then use theencapsulated view group like your imageview.
Options options = new BitmapFactory.Options();
options.inScaled=false;
Bitmap original;
Bitmap mutable = null;
try{
original = BitmapFactory.decodeResource(this.getResources(), R.drawable.,null);
mutable = original.copy(Bitmap.Config.ARGB_8888, true);
}catch (Exception e) {
// TODO: handle exception
}
Paint paint=new Paint();
paint.setAntiAlias(true);
Canvas c= new Canvas(mutable);
Log.d("density",""+dm.density);
paint.setColor(Color.WHITE);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(42);
c.drawText("text", (mutable.getWidth()/2)-x,(mutable.getHeight()/2)+y, paint);
BitmapDrawable bitmap = new BitmapDrawable(mutable)
imageView set background drawable bitmap

Rotating a drawable in Android

How can a Drawable loaded from a resource be rotated when it is drawn? For example, I would like to draw an arrow and be able to rotate it to face in different directions when it is drawn?
You need to use Bitmap and Canvas Class functions to prepare drawable:
Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.image2);
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(90, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
mImageView.setImageBitmap(bmResult);
In this code sample rotation for 90 degrees over image center occurs.
essentially it can be boiled down to: do a(n inverse) canvas transformation instead of transforming drawable
private BitmapDrawable drawable; // or Drawable
protected void onDraw(Canvas canvas) { // inherited from View
//...
canvas.save();
canvas.rotate(degrees, pivotX, pivotY);
drawable.draw(canvas);
canvas.restore();
//...
}
if you have BitmapDrawable it may be desirable to increase quality of the output by setting antialiasing
drawable.setAntialias(true);
Accepted answer doesn't work for me. I have non square image, so I changed his code a bit.
private Bitmap rotateDrawable(#DrawableRes int resId) {
Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), resId);
Bitmap bmpResult = Bitmap.createBitmap(bmpOriginal.getHeight(), bmpOriginal.getWidth(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmpResult);
int pivot = bmpOriginal.getHeight() / 2;
tempCanvas.rotate(90, pivot, pivot);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
return bmpResult;
}
mImageView.setImageBitmap(rotateDrawable(R.drawable.some_image));
essentially this:
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
source link:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#tween-animation

Categories

Resources