Binary XML file line #54: Error inflating class MainActivity.CanvasView - java

I'm using this class to draw a CanvasOval by inflating this class from my xml file.
public class CanvasView extends View {
int height, width;
public CanvasView(Context context) {
super(context);
}
public CanvasView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CanvasView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
DisplayMetrics metrics = new DisplayMetrics();
((Activity)getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
height = metrics.heightPixels;
width = metrics.widthPixels;
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
draw(canvas, paint);
}
public void draw(Canvas canvas, Paint paint) {
}
I'm getting the oval as needed when I've created a separate file for this class but when I create this class as a subclass in my MainActivity class, I'm getting error saying
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shashank.mApp.MainActivity}: android.view.InflateException: Binary XML file line #54: Binary XML file line #54: Error inflating class com.shashank.mApp.MainActivity.CanvasView

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

Draw square box using the coordinates on image in android(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);
}
}

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 .

Categories

Resources