Draw square box using the coordinates on image in android(JAVA) - java

I have some coordinates which is coming from the backend. I need to draw some rectangles or squares according to the coordinates on image in the (JAVA) android app. The rectangles or squares should be clickable you can see the coordinates in the image
public class DrawView extends View {
Paint paint = new Paint();
private void init() {
paint.setColor(Color.BLACK);
}
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
#Override
public void onDraw(Canvas canvas) {
canvas.drawRoundRect(150,150,300,300,10,10,paint);
canvas.drawRect(300, 300, 600, 600,paint);
}
}

Related

android moving multiple circle on a L shaped path

would try to move multiple points along an L shaped path to simulate water flow from a point to another. I created a view that allow to create a single point and created an animation. Now I'm stuck as I cannot understand how to retrieve values from the animator add points to it and update animation. Can someone try to move me on right path?
public class FlowAnimation extends View implements ValueAnimator.AnimatorUpdateListener {
int radius = 20;
int color = Color.WHITE;
Paint paint = null;
private ValueAnimator mAnimator;
PathMeasure pm;
float point[] = {0f, 0f};
Path path;
public FlowAnimation(Context context) {
super(context);
paint = new Paint();
path = new Path();
path.lineTo(100,150);
path.lineTo(300,500);
path.close();
}
public FlowAnimation(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
}
public FlowAnimation(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public FlowAnimation(Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
paint.setColor(this.color);
canvas.drawCircle(x / 2, y / 2, this.radius, paint);
}
public void startAnim() {
// sets the range of our value
mAnimator = ValueAnimator.ofInt(0, 180);
// sets the duration of our animation
mAnimator.setDuration(1000);
// registers our AnimatorUpdateListener
mAnimator.addUpdateListener(this);
mAnimator.start();
}
#Override
public void onAnimationUpdate(#NonNull ValueAnimator valueAnimator) {
//gets the current value of our animation
int value = (int) valueAnimator.getAnimatedValue();
}
}

Android ImageView Two Width

Can we apply two widths to ImageView in android?
I searched a lot online for the solution but 1 or 2 solutions I got is via drawable file. But I don't want to do it with drawable because I want to change ImageView width at runtime from seekbar values. There is two seekbar in my application, one will change the width from top and other will change the width from the bottom on a ImageView.
I tried following too:
public class CustomImage extends AppCompatImageView {
private Path path;
public CustomImage(Context context) {
super(context);
init();
}
public CustomImage(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomImage(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
path = new Path();
}
#Override
protected void onDraw(Canvas canvas) {
float h = getMeasuredHeight();
float w = getMeasuredWidth();
path.moveTo(0, 0);
path.lineTo(w, 0);
path.lineTo(w * 0.8f, h);
path.lineTo(w * 0.2f, h);
path.lineTo(0, 0);
path.close();
canvas.clipPath(path);
super.onDraw(canvas);
}
}
But it's not working as I want. No drawable solutions please, because it will not work on my app.

How would I align imagebuttons along this semicircle path in android studio?

I want to add an imagebutton along this path that I have created in android studio. I have been trying to find a tutorial that would help me with pragmatically placing a few buttons along this path I have made.
I haven't tried anything but researching tutorials to find out what to do because I am completely lost. I just need someone who has done this or can refer me to something to learn.
public class MyView extends View {
Paint paint;
Path path;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(1);
paint.setStyle(Paint.Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
final RectF oval = new RectF();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GRAY);
;
oval.set(0, 20, 500, 1000);
canvas.drawArc(oval, 270, 180, false, paint);
}
}
I expect to learn how to align a few buttons along this path.

How to pass the parameters custom view to activity class

I have a custom view class which is DrawView where it extends View and has onDraw:
public class DrawView extends View {
Paint paint;
public ArrayList<Line> lines;
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(10);
}
#Override
public void onDraw(Canvas canvas) {
for(Line line : lines){
canvas.drawLine(line.x_start, line.y_start, line.x_stop, line.y_stop, paint);
}
}
}
I want to instantiate DrawView to my MainActivity class which extends Activity. How would I do that? DrawView dv= new DrawView(?)
Btw, I would be calling invalidate() in MainActivity. I would be calling dv.invalidate() that's why I need to instantiate DrawView in my `MainActivity
yes you can . you can instantiate your view in main activity create attributeset if you need it pass it to view constructor and done .
XmlPullParser parser = resources.getXml(myResouce);
AttributeSet attributes = Xml.asAttributeSet(parser);
for more info . and you can use invalidate whenever you want to refresh view .

Animate last added child in Grid View with Adapter

I have a GridView which I am constantly adding views to. When the view adds to the grid I wish for it to do an animation. However, as I have to use setAdapter() to refresh the GridView, it ends up animating all views, as they are all being re-added. Is there anyway around this?
Here is the code for my view I am adding:
public class GridImageView extends ImageView {
public GridImageView(Context context) {
super(context);
}
public GridImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
ScaleAnimation anim = new ScaleAnimation(0,1,0,1);
anim.setDuration(1000);
anim.setFillAfter(true);
this.startAnimation(anim);
}
}
As always, thanks for your help
Thanks to Luksprog's suggestion, I have set a flag in my custom view which will determine if the view should animate when added to the Grid View.
public class GridImageView extends ImageView
{
private boolean _animate = false;
public GridImageView(Context context) {
super(context);
}
public GridImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onAttachedToWindow() {
if(_animate){
super.onAttachedToWindow();
ScaleAnimation anim = new ScaleAnimation(0,1,0,1);
anim.setDuration(1000);
anim.setFillAfter(true);
this.startAnimation(anim);
}
}
public void set_animate(boolean _animate) {
this._animate = _animate;
}
}
and my adapter in its GetView() function checks if it is the last in the array list, and sets the flag to true if so.
if( i == ( _gridDetailsArrayList.size() - 1 ))
holder.gridImage.set_animate(true);

Categories

Resources