-
Notifications
You must be signed in to change notification settings - Fork 0
/
BorderTransformation.java
88 lines (68 loc) · 2.76 KB
/
BorderTransformation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.example.cosmic.androidappclasses;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
/**
* Created by ${cosmic} on 12/28/17.
*/
public class BorderTransformation {
public static Bitmap applyCirclarBorder(Bitmap bmp, int color) {
Bitmap sbmp;
int radius = bmp.getWidth();
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
float factor = smallest / radius;
sbmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() / factor), (int)(bmp.getHeight() / factor), false);
} else {
sbmp = bmp;
}
Bitmap output = Bitmap.createBitmap(radius, radius,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
//color = 0xffa19774;
final Paint paint = new Paint();
final Paint stroke = new Paint();
final Rect rect = new Rect(0, 0, radius, radius);
paint.setAntiAlias(true);
stroke.setAntiAlias(true);
paint.setFilterBitmap(true);
stroke.setFilterBitmap(true);
paint.setDither(true);
stroke.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.RED);
stroke.setColor(color); //border color
stroke.setStyle(Paint.Style.STROKE);
stroke.setStrokeWidth(14f); //border width
canvas.drawCircle(radius / 2 + 0.7f,
radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
canvas.drawCircle(radius / 2 + 0.7f,
radius / 2 + 0.7f, radius / 2 - stroke.getStrokeWidth()/2 +0.1f, stroke);
return output;
}
public Bitmap applyRoundCornerEffect(Bitmap src, float round) {
// image size
int width = src.getWidth();
int height = src.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawARGB(0, 0, 0, 0);
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
final Rect rect = new Rect(0, 0, width, height);
final RectF rectF = new RectF(rect);
canvas.drawRoundRect(rectF, round, round, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(src, rect, rect, paint);
// return final image
return result;
}
}