Draw multiple points over imageview in android - android

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);
}

Related

Android Crop Bitmap Canvas

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);
}

Instagram Tilt Shift Animation in Android

I would like to create an animation similar to Instagram Tilt Shift ( Radial/Linear), I am trying to create a blurred bitmap from the existing image and change the imageView source on animation start and stop. But its slow and not smooth.
public class BlurMaskHandler extends AsyncTask<Integer,Integer,Bitmap> {
#Override
protected Bitmap doInBackground(Integer... drawable) {
Bitmap bitmapMaster = BitmapFactory.decodeResource(getResources(), drawable[0]);
int w = bitmapMaster.getWidth();
int h = bitmapMaster.getHeight();
RectF rectF = new RectF(0,0,w,h);
float blurRadius = 500.0f;
Bitmap bitmapResult = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvasResult = new Canvas(bitmapResult);
Paint blurPaintOuter = new Paint();
blurPaintOuter.setColor(0xFFffffff);
blurPaintOuter.setMaskFilter(new
BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.INNER));
canvasResult.drawBitmap(bitmapMaster, 0, 0, blurPaintOuter);
Paint blurPaintInner = new Paint();
blurPaintInner.setColor(0x80ffffff);
blurPaintInner.setMaskFilter(new
BlurMaskFilter(blurRadius, BlurMaskFilter.Blur.INNER));
canvasResult.drawRect(rectF, blurPaintInner);
return bitmapResult;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
bitmapView = bitmap;
}
}

Multipoint Cropping Android

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

android when trying drawing a text on it using canvas i get an error

I tried drawing text on an image, but it doesn't work. Here is my code:
ImageView image = (ImageView) findViewById(R.id.imageView1);
Uri uri = Uri.parse("android.resource://mms.first/"+R.drawable.girl);
Bitmap bitmap;
try {
ContentResolver cr = getContentResolver();
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, uri);
//drawTextImage(bitmap);
Canvas c = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(20);
c.drawText("Golden Stag", 30, 200, paint);
image.setImageBitmap(bitmap);
This is the code:
Bitmap bitmap;
Resources resx = getResources();
bitmap = BitmapFactory.decodeResource(resx, R.drawable.girl);
Canvas canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true));

android shadow image effect

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:

Categories

Resources