I've generated a circle bitmap programatically like so:
private Bitmap drawDotCircle() {
int circleSize = 100;
circleBitmap = Bitmap.createBitmap(
circleSize,
circleSize,
Bitmap.Config.ARGB_8888
);
canvas = new Canvas(circleBitmap);
CanvasRadius = Math.min(canvas.getWidth(), canvas.getHeight() / 2);
// Create a Paint object used to paint the circle
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
canvas.drawCircle(
canvas.getWidth() / 2,
canvas.getHeight() / 2,
CanvasRadius - CanvasPadding,
paint
);
return circleBitmap;
}
I would like to place it at an absolute X and Y that I've calculated; let's assume its the absolute center of the screen as follows:
int dotX =getResources().getDisplayMetrics().widthPixels / 2;
int dotY = getResources().getDisplayMetrics().heightPixels / 2;
How do I ensure that the circle is centered in the view? I have tried the following code, but the circle is always off-center for some reason:
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(circleDot.getHeight(), circleDot.getHeight());
// Setting position of our ImageView
layoutParams.leftMargin = dotX;
layoutParams.topMargin = dotY;
// Finally Adding the imageView to RelativeLayout and its position
relativeLayout.addView(dotView, layoutParams);
The leftMargin should be dotX - imageWidth/2 and topMargin should be dotY - imageHeight/2
Try that
Related
I drew 4 lines from the center towards the button as I show you in the photo. I do not know how I can draw curved lines that are in red color in the picture.
[enter image description here]
or
[enter image description here (simpler)]
Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(), (int) getWindowManager()
.getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int x = metrics.widthPixels;
int y = metrics.heightPixels;
Paint paint1 = new Paint () ;
paint1.setStrokeWidth(10);
int margin = 100;
int margin1 = 300;
int top = 0 + margin;
int bottom = canvas.getHeight() - margin;
int left = 0 + margin1;
int right = canvas.getWidth() - margin1;
int centerX = x / 2;
int centerY = y / 2;
canvas.drawLine(centerX, top, centerX, bottom,paint1);
canvas.drawLine(left, centerY, right, centerY,paint1);
You will need to split it in 4 different parts (curves) for easier drawing
Here is my sketch(sorry for quick drawing)
So you need to get 4 points for bezieres and should be something like this
1st move to start (drawing point)
path.moveTo(x1, y1);
then use next for draw path
cubicTo(x2, y2, x3, y3, x4,y4)
and finally
canvas.drawPath(path, paint);
Same procedure make for rest 3 quadrant/parts
hope this will help you to archive your goal
I'm trying to create a Kaleidoscope with an ImageView in Android. I'm struggling to get the rotation and mirroring for each 'segment' correct. I'm new to image manipulation and I'm trying to adapt the code example from here for android.
I have the following code:
private void drawKaleidoscope() {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.cropped_landscape);
Bitmap imageview_bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap matrix_bitmap;
BitmapShader fillShader;
Path triangle_mask = new Path();
RectF r = new RectF(0, 0, bm.getWidth(), bm.getHeight()); // create new rectangle to match the dimensions of our image
this.radius = (int)r.height() / 2;
Canvas c = new Canvas(imageview_bitmap);
c.drawColor(Color.BLACK);
float start_angle = 0;
for (int i = 0; i < this.segments; i++) {
// Create pie-slice shape mask
triangle_mask.reset();
triangle_mask.moveTo(r.centerX(), r.centerY());
triangle_mask.arcTo(r, start_angle, angle);
triangle_mask.close();
// Use odd even check to decide when to mirror the image or not
if (i % 2 == 0) {
Matrix mat = new Matrix();
mat.preTranslate(-radius, -radius);
mat.postRotate(i * angle);
mat.postTranslate(radius, radius);
matrix_bitmap = Bitmap.createBitmap(bm, 0, 0, (int)r.width(), (int)r.height(), mat, true);
}
else {
Matrix mat = new Matrix();
// mirror on x axis
mat.postScale(-1, 1);
mat.postTranslate(-radius, radius);
mat.postRotate((float)-Math.PI);
mat.postRotate(i * angle);
mat.postTranslate(radius, -radius);
matrix_bitmap = Bitmap.createBitmap(bm, 0, 0, (int)r.width(), (int)r.height(), mat, true);
}
fillShader = new BitmapShader(matrix_bitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
// Fill the triangle masked area with our image now
Paint fill = new Paint();
fill.setColor(0xFFFFFFFF);
fill.setStyle(Paint.Style.FILL);
fill.setShader(fillShader);
c.drawPath(triangle_mask, fill);
start_angle += angle;
}
kal.setImageBitmap(imageview_bitmap);
}
The output of the above function looks like so:
If anyone could provide some insight on how to properly do the image rotations/mirroring that would be greatly appreciated.
Okay I ended up going about this a different way. Instead of rotating the source image I simply draw the image mask to the same spot on the canvas, and then rotate the canvas itself. Let's say I have 12 image 'slices'. I draw 6 alternating segments, flip the canvas via canvas.scale(-1, 1) and then draw another 6 segments in where the blank spaces are. Here's the code I ended up with:
private Bitmap generateKaleidoscopeBitmap(float start_angle) {
Canvas canvas = new Canvas(imageview_bitmap);
canvas.drawColor(Color.BLACK);
BitmapShader fillShader;
Path triangle_mask = new Path();
RectF r = new RectF(0, 0, imageview_bitmap.getWidth(), imageview_bitmap.getHeight()); // create new rectangle to match the dimensions of our image
int centerX = imageview_bitmap.getWidth() / 2;
int centerY = imageview_bitmap.getHeight() / 2;
// how much to rotate the canvas by after the image is flipped
float offset = calculateCanvasSymmetryOffset(start_angle);
// Create a pie-slice shaped clipping mask
triangle_mask.moveTo(r.centerX(), r.centerY());
triangle_mask.arcTo(r, start_angle, angle);
triangle_mask.close();
// Fill the triangle masked area with our shader now
Paint fill = new Paint();
fill.setColor(0xFFFFFFFF);
fill.setStyle(Paint.Style.FILL);
fillShader = new BitmapShader(source_image, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
fill.setShader(fillShader);
// Rotate the canvas and draw the clipping mask to the canvas
for (int i = 0; i < this.segments / 2; i++) {
canvas.drawPath(triangle_mask, fill);
canvas.rotate(angle * 2, centerX, centerY);
}
// mirror the canvas and rotate it once to counter the symmetrical offset
canvas.scale(-1, 1, centerX, centerY);
canvas.rotate(offset, centerX, centerY);
// Rotate the now mirrored canvas and draw the clipping mask to it
// This is a cheap and easy way of creating mirrored segments
for (int i = 0; i < this.segments / 2; i++) {
canvas.drawPath(triangle_mask, fill);
canvas.rotate(angle * 2, centerX, centerY);
}
return imageview_bitmap;
}
Could you help me how can I make Android view with images as on the screenshot:
It must fit screen width.
I can transform the first image, from left, like:
ImageView img;
float degrees = 70;
Camera mCamera = new Camera();
final float mCenterX = img.getWidth() / 2.0f;
final float mCenterY = img.getHeight() / 2.0f;
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
Matrix matrix = new Matrix();
camera.save();
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
img.setImageMatrix(matrix);
In the layout imageViews are stored inside Horizontal LinearLayout. But image width isn't recalculated with matrix transformation.
Please, help me to implement this layout, as on the picture.
I've got a drawable I'm applying a filter to using
drawable.setColorFilter (color, PorterDuff.MILTIPLY)
Then I'm taking the bitmap I used to create the drawable and putting it into a canvas so I can write text in the middle of the png. And finishing by writing
drawable.draw(canvas)
I want the text I add to always be white no matter what color filter is used but right now the text becomes whatever color is used for filtering. I was thinking if I could convert the filtered drawable to a bitmap then add the text the it'd work but so far it hasnt. Any help or suggestions would be greatly appreciated.
Here is my code
public void setDrawableFilter(String text, int color) {
mBitmap = scaleBitmap(250, 238);
mDrawable = new BitmapDrawable(mContext.getResources(), mBitmap);
mDrawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
addTextToBitmap(text);
}
public void addTextToBitmap(String text){
Canvas canvas = new Canvas(mBitmap);
mDrawable.draw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(35);
paint.setColor(Color.WHITE);
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
float xPercent = 0.38f;
float yPercent = 0.58f;
int textX = (int) (canvasWidth * xPercent);//
int textY = (int) (canvasHeight * yPercent);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(text, canvasWidth / 2, textY, paint);
}
I am developing Photography apps, and for that I am using this code:
Canvas canvas = new Canvas(bmOverlay);
TextPaint paint = new TextPaint();
paint.setColor(Color.RED);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(50);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
// if the background image is defined in main.xml, omit this line
canvas.drawBitmap(mBitmap, 0, 0, null);
int left = measureTextWidth(paint, InstaTextActivity.CurrentWord);
int top = measureTextHeight(paint, InstaTextActivity.CurrentWord);
left = mBitmap.getWidth() / 2 - InstaTextActivity.textCount / 2;
top = mBitmap.getHeight() / 2 - InstaTextActivity.textCount / 2;
StaticLayout layout = new StaticLayout(InstaTextActivity.CurrentWord, paint, total,
android.text.Layout.Alignment.ALIGN_NORMAL, (float) 1.0, (float) 0.0, true);
//canvas.save();
canvas.translate(left, top);
layout.draw(canvas);
//canvas.restore();
In this code I am using paint.setTextAlign(Align.CENTER). It aligns to centre, but text doesn't align center to left, instead it aligns centre to right. Also, what is the proper way to align text on canvas?
But I want
Im using
StaticLayout mTextLayout = new StaticLayout(text, mTextPaint,
canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
false);
canvas.translate((canvas.getWidth() / 2) - (mTextLayout.getWidth() / 2), (canvas.getHeight() / 2) - ((mTextLayout.getHeight() / 2)));
to center my multi line text on a canvas