Android Circular image with pointer - java

I am developing Android application, in my application I have to show the user profile image as shown in this image. I had used CircularImage but I need to have the pointer in the bottom, can anyone help me how to create circular image with pointer the bottom

you can set your pointer(blue background) as background for ImageView and set user image as src in bitmap
also you need to round your bitmap,by this method you can earn circular bitmap:
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2.0f;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2.0f;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
}
and set round bitmap as src for your ImageView:
imageView.setImageBitmap(toRoundBitmap(mBitMap));
for better view set android:scaleType="fitStart" in your ImageView tag in xml

Related

How do I draw two lines that intersect on Bitmap - Java android

So I want to draw two lines that cross in the middle (like an x), and I want to draw this onto my randomly generated bitmap. Does anyone know how I can make the line diagonl?
public static Bitmap randomImage(int width, int height) {
Bitmap ranImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Random rand = new Random(System.currentTimeMillis());
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
int color = Color.rgb(r, g, b);
ranImage.setPixel(x, y, color);
}
//initialise a new canvas instance
Canvas canvas = new Canvas(ranImage);
//intialise a new paint instance to draw the line
Paint paint = new Paint();
//line color
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
//line width in pixels
paint.setStrokeWidth(3);
paint.setAntiAlias(true);
//set a pizel value to offset the line from the canvas edge
int offset = 50;
//draw a line on canvas at the center postion ....for now
canvas.drawLine(
offset,
canvas.getHeight()/2,
canvas.getWidth()-offset,
canvas.getHeight()/2,
paint
);
return ranImage;
}

Trying to draw custom view How to Detect which slice is clicked

I am creating custom view, which is like screen below. I can draw views but I am unable to create separate click events for each view. How to set separate click events for each arc view?. Thanks in advance.
Here is the code:
ArcView.java
public class ItemView extends View {
Utils utils;
int left, right, top, bottom;
private int color;
private int start;
private int sweep;
private boolean inner;
private RectF oval;
public float center_x, center_y;
int divOn;
public float radius;
public ItemView(Context context, int color, int start, int sweep) {
super(context);
this.color = color;
this.start = start;
this.sweep = sweep;
utils = Utils.getInstance(getContext());
}
public ItemView(Context context, int color, int start, int sweep, boolean inner, int divOn) {
super(context);
this.color = color;
this.start = start;
this.sweep = sweep;
this.inner = inner;
this.divOn = divOn;
utils = Utils.getInstance(getContext());
}
public RectF getOval() {
return oval;
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//There is another flag for inner white circle
if (inner) {
float width = (float) getWidth();
float height = utils.getScreenHeight() - utils.getActionBarSize();
radius = (width - utils.dpTopixel(50)) / divOn;
Path path = new Path();
path.addCircle((width - utils.dpTopixel(50)) / divOn,
(height - utils.dpTopixel(50)) / divOn, radius,
Path.Direction.CW);
Paint paint = new Paint();
paint.setColor(ContextCompat.getColor(getContext(), color));
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
final RectF oval = new RectF();
paint.setStyle(Paint.Style.FILL);
left = -(int) radius;
right = (int) radius;
top = (getHeight() / 2) - (int) radius;
bottom = (getHeight() / 2) + (int) radius;
oval.set(left,
top,
right,
bottom);
canvas.drawArc(oval, start, sweep, true, paint);
} else {
float width = (float) getWidth();
float height = utils.getScreenHeight() - utils.getActionBarSize();
float radius;
radius = (width - utils.dpTopixel(50)) / divOn;
Path path = new Path();
path.addCircle((width - utils.dpTopixel(50)) / 1,
(width - utils.dpTopixel(50)) / 1, radius,
Path.Direction.CW);
Paint paint = new Paint();
paint.setColor(ContextCompat.getColor(getContext(), color));
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
oval = new RectF();
paint.setStyle(Paint.Style.FILL);
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
textPaint.setColor(ContextCompat.getColor(getContext(), R.color.white));
textPaint.setTextAlign(Paint.Align.CENTER);
/*Paint.FontMetrics metrics = paint.getFontMetrics();
float textheight = Math.abs(metrics.top - metrics.bottom);
float x = getWidth() / 2;
float y = (getHeight() / 2) + (height / 2);
canvas.drawText("My Text", x, y, paint);*/
left = -(int) radius;
right = (int) radius;
top = (getHeight() / 2) - (int) radius;
bottom = (getHeight() / 2) + (int) radius;
center_x = width / 2;
center_y = utils.getOffset();
int xPos = (getWidth() / 2);
int yPos = (int) ((getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
//((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center.
oval.set(left,
top,
right,
bottom);
canvas.drawArc(oval, start, sweep, true, paint);
}
}
}
utils class to detect height and width and convert to px
i want equation to check i click which circle
Create a slice selected listener, override the onTouchEvent in your custom class, calculate which slice the touch event (with the action ACTION_UP) is on using the x,y values. You can then call the function from your slice selected listener and pass details about the selected slice.

Crop Bitmap image to custom sguare

Image sample
I have this method written for cropping my image, but HEIGHT dimension doesnt work on the cropping, only weight is done right. I cant find the issue. I want to use my screen width and height as a dynamic width and height.
public Bitmap cropToSquare(Bitmap bitmap){
int width = mScreenWidth ;
int height = mScreenHeight;
int newWidth = width - 2 * 10;
int newHeight = (height- newWidth) / 2;
Bitmap cropImg = Bitmap.createBitmap(bitmap, 0, 0, newWidth, newHeight);
return cropImg; }
}
Here is my solution:
public static Bitmap getResizedClippedBitmap (Bitmap bm, int newWidth, int newHeight) {
Bitmap targetBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
float originalWidth = bm.getWidth();
float originalHeight = bm.getHeight();
float scale, xTranslation = 0.0f, yTranslation = 0.0f;
if (originalWidth > originalHeight) {
scale = newHeight/originalHeight;
xTranslation = (newWidth - originalWidth * scale)/2.0f;
}
else {
scale = newWidth / originalWidth;
yTranslation = (newHeight - originalHeight * scale)/2.0f;
}
Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(bm, transformation, paint);
return targetBitmap;
}
You can find some other utils here: BitmapUtils.java
Hope it helps.

image watermark on an image in android

i have an image in imageview , how can i add an image/logo watermark on that image.?
i have used following code to add text watermark
String watermark ="Sari collage";
int x =1500;
int y =1200;
int color = Color.parseColor("#ff720b");
int alpha=80;
int size = 70;
boolean underline= false;
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
paint.setColor(color);
paint.setAlpha(alpha);
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawText(watermark, x, y, paint);
I guess:
String watermark ="Sari collage";
int x =1500;
int y =1200;
int color = Color.parseColor("#ff720b");
int alpha=80;
int size = 70;
boolean underline= false;
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
canvas.drawBitmap(src, w/2, h/2, null);
Paint paint = new Paint();
paint.setColor(color);
paint.setAlpha(alpha);
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawText(watermark, x, y, paint);

Android Will not display circle where a variable is one of the parameters

public class MainActivity extends Activity {
ImageView drawingImageView;
Canvas canvas;
Paint paint;
float t;
private Handler handler = new Handler();
Display display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = getWindowManager().getDefaultDisplay();
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
float width = size.x;
float height = size.y;
drawingImageView = (ImageView) this.findViewById(R.id.imageView);
Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);
// Path
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.GREEN);
handler.postDelayed(runnable, 0);
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
canvas.drawPath(DrawCircle(1f, (t*0.05f), 100), paint); //<------Does not work
canvas.drawPath(DrawCircle(1f, 0.1f, 100), paint); //<----- works
t++;
handler.postDelayed(this, 1000);
}
};
public Path DrawCircle(float PercOfScreen, float Amount, int vertexCount){
Point size = new Point();
display.getSize(size);
float width = size.x;
float height = size.y;
Path p = new Path();
int center_x = (int) width/2;
int center_y = (int) height/2;
int bound;
if (height > width){
bound = (int) width;
}
else{
bound = (int) height;
}
int radius = (int) (PercOfScreen*bound/2);
float Pi = (float) Math.PI;
p.moveTo(center_x, center_y);
for (int i = 0; i < vertexCount*Amount; ++i){
float percent = (i / (float) (vertexCount-1));
double rad = percent * 2f*Pi;
//vertex position
float outer_x = center_x + radius * (float)Math.cos(rad);
float outer_y = center_y + radius * (float)Math.sin(rad);
p.lineTo(outer_x, outer_y);
}
return p;
}
}
The above code will not display part of a circle if there is a variable in the parameters, but will if there is no variable. Why is this?
Also, for anyone who wants one, simple function to display a circle or part of a circle.

Categories

Resources