Drawing shapes on changing image in android - android

I need to draw shapes on an image in my layout. This image needs to be able to change to another image programically, and I also need to draw shapes (rectangles and circles) over top of this image programically. The shapes will also change. I have an existing xml layout and would like to use this layout with the programmed image view in it. What's the easiest way to do this? Would it be possible to see a short example?

I figured out how to do this:
Here's how:
ImageView image = (ImageView) findViewById(R.id.mainImageView);
Bitmap bMap = BitmapFactory.decodeFile(imageFileString);
bMap = bMap.copy(Bitmap.Config.ARGB_8888 , true);
Canvas canvas = new Canvas(bMap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.BLUE);
canvas.drawCircle(x, y, radius, paint);
image.setImageBitmap(bMap);
Explanation: The first line gets the ImageView from the layout. I then grab the new image I want displayed using BitmapFactory.decodeFile, where imageFileString is the file location of the new image I want to display. After that I create a new canvas using the bitmap and draw on it. I then display the bitmap to the ImageView using image.setImageBitmap(bMap);.

Related

Android change bitmap transparency on canvas

I want to be able to slowly make a bitmap image more and more transparent on my canvas.
Currently I am drawing the .png file (stored in drawables) like this:
//I setup the Bitmap in my constructor.
heartSymb = BitmapFactory.decodeResource(getResources(),R.drawable.heartsymbol);
//This is in on draw.
canvas.drawBitmap(heartSymb,0,0,null);
How would I be able to slowly change the bitmap's transparency until it becomes fully transparent?
You can use a Paint object to modify the alpha of the bitmap to be drawn:
Paint alphaPaint = new Paint();
alphaPaint.setAlpha(alpha);
canvas.drawBitmap(heartSymb, 0, 0, alphaPaint);
Then you just have to modify alpha value and perform update periodically, maybe by using a Handler.

How to Crop Image in Hexagon,Octagon and in circle shape in Android?

I want to crop shape in Hexagon,Octagon and in circle shape.I have used Custom Image view class and use in xml for showing image. It works properly for different shapes.
Now i need to crop the image in user selected shape in next activity.i get image in next activity.
I tried this example:
Masking(crop) image in frame But the image didn't fit in shape.only part of the image get masked.
How i can achieve this?
Read this blog post by Romain Guy on how to make images with rounded corners. Using this technique you can create a variety of shapes.
How would this work?
If you have a predefined number of shapes, I'd suggest you create a class with a few Shapes defined by Paths. Now whenever your user asks for a different shape to be used on an ImageView, you create a Bitmap and return a bitmap from that class and put it on your ImageView.
For example a octagon shape would be something like this:
public static Bitmap drawOctagonShapedBitmap(Bitmap src) {
Bitmap dst = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
//Create an output as big as the actual bitmap.
Path octagon = null; //Create an octagon shape, it should be big enough to crop enough of the bitmap.
Canvas canvas = new Canvas(dst);
Paint mPaint = new Paint();
BitmapShader mBitmapShader = new BitmapShader(src, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
mPaint.setAntiAlias(true);
mPaint.setShader(mBitmapShader);
canvas.drawPath(octagon, mPaint);
return dst;
}

Generate image with dynamic string in android

How could I programatically generate an image (ie output_file.png) which is a combination of user input (ie. strings) overlaid on top of another image file?
Hopefully the image below can illustrate it better
To write text directly into a bitmap you can do something similar to the following:
int textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, context.getResources().getDisplayMetrics());
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(textSize);
paint.setColor(Color.WHITE);
Canvas myCanvas = new Canvas(myBitmap);
myCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
myCanvas.drawText(string, x, y, paint);
To write the bitmap to a file you can read the answer to this question here: Convert Bitmap to File
Do you actually need to create an image? Are you trying to save it/share it or just display text overlaying an image?
If you just need to overlay, you can set a textview to overlay the image using a RelativeLayout.
If you want to save and share the image, you should take a look at
How to programmatically take a screenshot in Android?

transform a sqaure shape drawable in to circular shape drawable withaout affecting the pixel quality

I have an application which takes picture from camera .And normally this pictures have either rectangle shapes or square shapes.But due to make these pictures circular in shape i limited the shape to only square.So now my problem is to make this square picture into a circular shape having radius of half of width or height.And the clipped part should be removed from the picture.
Its same like making a circle from the square which touches midpoint of all of the faces of square.
I don't have any experience with canvas or other in built drawing functions.
For example i have square at first now i want to make image circular such that parts pointed by arrow should get clipped from image.
Note:as i have to work with camera images .quality is crucial factor.So please suggest the possible ways that will not affect the pixel quality and other important factors.
You can do it by masking the image with your desired shape, see this post for detailed info.
example code:
Resources resources = context.getResources();
Bitmap original = BitmapFactory.decodeResource(resources,R.drawable.original);
Bitmap mask = BitmapFactory.decodeResource(resources,R.drawable.mask);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas c = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
c.drawBitmap(original, 0, 0, null);
c.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
imageView.setImageBitmap(result);

Moving shape over imageview?

I'm trying to find a way to implement a shape (a rectangle) that can be moved over an image. The user would drag the rectangle to an area in the image, as to eventually select it.
How can this be done? What should I use? Any help would be appreciated!
Use canvas
choosenImageView = (ImageView) this.findViewById(R.id.ChoosenImageView);
canvas = new Canvas(bitmap); //Bitmap
mBitmapPaint = new Paint();
mBitmapPaint.setAntiAlias(true);
mBitmapPaint.setStyle(Paint.Style.STROKE);
mBitmapPaint.setStrokeWidth(5);
choosenImageView.setOnTouchListener(this);

Categories

Resources