I want to implement mutipoint cropping in android ,How to crop the image inside the path lines (white lines as shown in figure) ?
The code is I used is :
private Bitmap image;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
image = BitmapFactory.decodeResource(getResources(), R.drawable.splash);
Bitmap resultingImage = Bitmap.createBitmap(image.getWidth(),
image.getHeight(), image.getConfig());
Canvas canvas = new Canvas(resultingImage);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(3);
Path path = new Path();
path.lineTo(150, 0);
path.lineTo(210, 120);
path.lineTo(150, 240);
path.lineTo(70, 120);
path.lineTo(150, 0);
canvas.drawPath(path, paint);
canvas.drawBitmap(resultingImage, 0, 0, paint);
iv.setImageBitmap(resultingImage);
}
}
The idea is to render a Path and filling it with a bitmap.
See this link for more details : http://www.41post.com/4794/programming/android-rendering-a-path-with-a-bitmap-fill
Related
How to add multiple points over an image view.
this is what i am trying
myView = (ImageView) findViewById(R.id.my_view);
View view = LayoutInflater.from(ZoomTouchActivity.this).inflate(R.layout.layout_custom_view, null);
CircularImageView imgView = view.findViewById(R.id.site_image);
TextView siteName = view.findViewById(R.id.site_text);
imgView.setImageResource(R.drawable.ic_launcher_background);
siteName.setText("est");
Bitmap bitmap = getBitmapFromView(view);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
// canvas.drawCircle(50, 50, 10, paint);
canvas.drawBitmap(bitmap, 50, 50, paint);
myView.setImageBitmap(bitmap);
You can draw on an ImageView by simply putting the image's Bitmap inside a canvas, draw on the canvas and set the drawn canvas to the imageview. You can check this answer for more details. For more details on how to draw, you can check the android documentation here.
Try this:
BitmapFactory.Options myOptions;
Canvas canvas;
Bitmap mutableBitmap;
Bitmap workingBitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_open= findViewById(R.id.btn_open);
image2= findViewById(R.id.imageView);
myOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.image000880,myOptions);
paint= new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
workingBitmap = Bitmap.createBitmap(bitmap);
mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
canvas = new Canvas(mutableBitmap);
private void drawpoint(ImageView imageView,float x,float y, int raduis){
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
// ArrayList<Point> list= new ArrayList<>();
canvas.drawCircle(x,y, raduis, paint);
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
}
I am fetching a Bitmap with Parse on my Android application.
final WeakReference<ImageView> weakImageView = new WeakReference<>(imageViewToMask);
fetchFromParse(getContext(), new Success() {
#Override
public void success(Bitmap image) {
if (weakImageView.get() != null) {
// Here how to make this image a MaskDrawable to mask it?
// MaskedDrawable d = new MaskedDrawable(bitmap, context, [canvas here]?)
// weakImageView.get().setImageDrawable(d);
weakImageView.get().setImageDrawable(image);
}
}
});
Now I want to mask this bitmap with another image, I found this answer in this thread:
public class MaskedDrawable extends Drawable {
public void draw(Bitmap original, Context context, Canvas canvas) {
Bitmap mask = BitmapFactory.decodeResource(context.getResources(), R.drawable.mask_image);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas tempCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
tempCanvas.drawBitmap(original, 0, 0, null);
tempCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
// How do I create this canvas user here?
canvas.drawBitmap(result, 0, 0, new Paint());
}
}
I already have the original bitmap and the context object, but I don't know how to use canvas. What to do?
Thanks for your help!
I want to create the crop bitmap functionality and have referred Android: Free Croping of Image but this sets the bitmap in another imageView.Now I know I can set the cropped bitmap in the canvas but I want to retain the original bitmap and want to do the cropping in onDraw() and not make a different bitmap and then set it in canvas.I have tried to implement the below code in onDraw but there is no cropping and bitmap remains as it is.Pleas help so that I can code this in onDraw() method of the CustomView.
compositeImageView = (ImageView) findViewById(R.id.our_imageview);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(),
R.drawable.gallery_12);
Bitmap resultingImage = Bitmap.createBitmap(widthOfscreen,
heightOfScreen, bitmap2.getConfig());
Canvas canvas = new Canvas(resultingImage);
Paint paint = new Paint();
paint.setAntiAlias(true);
Path path = new Path();
for (int i = 0; i < SomeView.points.size(); i++) {
path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
}
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap2, 0, 0, paint);
compositeImageView.setImageBitmap(resultingImage);
my onDraw() method
#Override
protected void onDraw(Canvas canvas)
{
Bitmap bitmap1 = bitmap.copy(bitmap.getConfig(), true);
canvas.drawBitmap(bitmap1, 0,0, null);
Paint paint = new Paint();
paint.setAntiAlias(true);
Path path = new Path();
for (int i = 0; i < SomeView.points.size(); i++) {
path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
}
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap,0, 0, paint);
}
I am fetching images from the server and they are in the square shape. But I want to show them in a circle in my application. I have tried it by making a Circle shape Drawable. But it is not working. Can anyone suggest me how this can be done.Any help would me much appreciated . Thanks.
try below code:-
Universal loader
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).defaultDisplayImageOptions(options)
.build();
ImageLoader.getInstance().init(config);
DisplayImageOptions options1 = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(60)).build();
ImageLoader.getInstance().displayImage(Uri.parse(imgByURL).toString(), imgThumb, options);
or
imageViewUser.setImageBitmap(getCircleBitmap(bitmap));
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
public Bitmap getCircleBitmap(Bitmap bitmap)
{
Bitmap output;
Canvas canvas = null;
final int color = 0xffff0000;
final Paint paint = new Paint();
Rect rect = null;
if (bitmap.getHeight() > 501)
{
output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
canvas = new Canvas(output);
rect = new Rect(0, 0, 500, 500);
}
else
{
System.out.println("output else =======");
bitmap = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
canvas = new Canvas(output);
rect = new Rect(0, 0, 500, 500);
}
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth((float) 1);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
I'm trying to add shadow effect do an image. I use a mask to draw the image (I need a specific shape for my image). Can you please tell me how to add shadow effect to my image? I've tried something like paint.setShadowLayer(10, 10, 10, Color.RED) but it didn't worked. Here is the source code:
#Override
public void draw(Canvas canvas) {
Rect rect = new Rect(0, 0, getWidth() - 1, getHeight() - 1);
NinePatchDrawable mask = (NinePatchDrawable) getContext().getResources().getDrawable(maskResId);
mask.setBounds(rect);
Bitmap content = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888);
Canvas contentCanvas = new Canvas(content);
super.draw(contentCanvas);
Paint paint = new Paint();
paint.setXfermode(new AvoidXfermode(Color.BLACK, 255, AvoidXfermode.Mode.TARGET));
mask.draw(canvas);
canvas.drawBitmap(content, null, rect, paint);
}
my solution:
class ShadowImage
public class ShadowImage extends BitmapDrawable {
Bitmap bm;
static float shadowRadius = 4f;
static PointF shadowDirection = new PointF(2f, 2f);
int fillColor = 0;
#Override
public void draw(Canvas canvas) {
Rect rect = new Rect(0, 0, bm.getWidth(), bm.getHeight());
Log.i("TEST", rect.toString());
setBounds(rect);
Paint mShadow = new Paint();
mShadow.setAntiAlias(true);
mShadow.setShadowLayer(shadowRadius, shadowDirection.x, shadowDirection.y, Color.BLACK);
canvas.drawRect(rect, mShadow);
if(fillColor != 0) {
Paint mFill = new Paint();
mFill.setColor(fillColor);
canvas.drawRect(rect, mFill);
}
canvas.drawBitmap(bm, 0.0f, 0.0f, null);
}
public ShadowImage(Resources res, Bitmap bitmap) {
super(res, Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth()+shadowRadius*shadowDirection.x), (int) (bitmap.getHeight()+shadowRadius*shadowDirection.y), false));
this.bm = bitmap;
}
public ShadowImage(Resources res, Bitmap bitmap, int fillColor) {
super(res, Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth()+shadowRadius*shadowDirection.x), (int) (bitmap.getHeight()+shadowRadius*shadowDirection.y), false));
this.bm = bitmap;
this.fillColor = fillColor;
}
}
Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout root = (LinearLayout) findViewById(R.id.root_layout);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ShadowImage image = new ShadowImage(getResources(), bmp);
ShadowImage image2 = new ShadowImage(getResources(), bmp, Color.WHITE);
ImageView iv_normal = new ImageView(getApplicationContext());
iv_normal.setPadding(10, 10, 10, 10);
iv_normal.setImageBitmap(bmp);
ImageView iv_shadow = new ImageView(getApplicationContext());
iv_shadow.setPadding(10, 10, 10, 10);
iv_shadow.setImageDrawable(image);
ImageView iv_fill = new ImageView(getApplicationContext());
iv_fill.setPadding(10, 10, 10, 10);
iv_fill.setImageDrawable(image2);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
root.addView(iv_normal, params);
root.addView(iv_shadow, params);
root.addView(iv_fill, params);
root.setGravity(Gravity.CENTER);
}
Image: