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 .
Related
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);
}
}
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.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I'm initialing imageview at DailyCardView.java which are called by a fragment. But it keeps showing nullPointerException. I really don't know where went wrong... Guys help me><
I tried getRootView() to get view of Fragment so i can code:iv=(ImageView)getRootView().findViewById(R.id.iv);
But it still not work.
**//Fragment**
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment, container, false);
dailyStackLayout = (DailyStackLayout)view.findViewById(R.id.dailyStackLayout);
dailyStackLayout.setDatas(dailyFoodList);
return view;
}
**//DailyStackLayout**
public DailyStackLayout(Context context) {
this(context, null);
}
public DailyStackLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DailyStackLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init() {
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
scaleY = DensityUtil.dip2px(getContext(), BASESCALE_Y_VALUE);
}
public void setDatas(List<DailyFood> dailyFoodList) {
this.dailyFoodList = dailyFoodList;
if (dailyFoodList == null) {
return;
}
for (int i = index; index < i + STACK_SIZE; index++) {
dc = new DailyCardView(getContext());
dc.bind(dailyFoodList.get(index));
}
}
**//DailyCardView**
public DailyCardView(Context context) {
super(context, null);
}
public DailyCardView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public DailyCardView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init(context);
}
public void init(Context context) {
if (!isInEditMode()) {
inflate(context, R.layout.item_daily_card, this);
iv = (ImageView)findViewById(R.id.iv);
}
}
public void bind(DailyFood dailyFood) {
if (dailyFood == null) {return;}
if (!TextUtils.isEmpty(dailyFood.getId())) {
Glide.with(**iv.getContext()**) // **NullPointerException**!
.load(Constant.Daily_URL+dailyFood.getId()+".jpg")
.into(iv);
}
I expect to load the image right but it went wrong at getContext().
dc = new DailyCardView(getContext());
This calls the DailyCardView(Context) constructor that does not call init():
public DailyCardView(Context context) {
super(context, null);
}
The init() call would init iv. And then you call bind() that assumes that iv is initialised. Bang, NPE.
Either change the super in the constructor to this to delegate to another constructor that calls init(), or just add a call to init().
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
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);