0

I'm trying to call onDraw method on Button click ,button the view is updated only once also onDraw method is called . But no change in the position of line.

I have this custom view

public LineSeekbar(Context context) {
    super(context);
    this.setWillNotDraw(false);
    setNewX(130);       
}

@Override
protected void onDraw(Canvas canvas) {  
    super.onDraw(canvas);
    Log.e("GRAPH","draw");      
    paint = new Paint(); 
    paint.setARGB(225, 215, 10, 20); 
    paint.setStrokeWidth(2); 
    paint.setStyle(Style.FILL); 
    canvas.drawLine(130,900,getNewX(),100, paint);
    setNewX(getNewX()+15);
}

Calling this from activity class

final Bitmap mBackgroundImage = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
    cv =new Canvas(mBackgroundImage);
    LineView = new LineSeekbar(LineActivity.this);
    LineView.setLayoutParams(new LayoutParams(500,500));
    LineView.onMeasure(500,500);
    LineView.invalidate();
    LineView.draw(cv);
    //  LineView = null;
    ImageView mImageView = new ImageView(this);
    mImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,    LayoutParams.FILL_PARENT));
    mImageView.setBackgroundColor(android.R.color.white);
    mImageView.setImageBitmap( mBackgroundImage );
    LinearLayout ll =(LinearLayout) findViewById(R.id.linearLayout1);
    ll.addView(mImageView);

    Button inc = (Button) findViewById(R.id.increase);
    inc.setOnClickListener(new OnClickListener() {          
        @Override
        public void onClick(View v) {
            LineView.invalidate();
            LineView.draw(cv);              
        }
    });
voidRy
  • 674
  • 9
  • 20

3 Answers3

1

You should call an instance with small letter because it is not a class: lineView insted of LineView.

Since you are calling it from another thread (UI) you should say

LineView.postInvalidate();

When you create LineView instance for the first time, onDraw will be executed on its own and show the first line.

It is not clear what is LineView.draw(cv); You can draw the background image in onDraw just before drawing the line. You can use onSizeChange method in the custom view to find its real dimensions and resize your bitmap...

In onDraw insert a line

Log.e("LineView", Integer.toString(getNewX()));

and then in DDMS - LogCat watch the output when you press the button.

Lumis
  • 21,517
  • 8
  • 63
  • 67
  • I used postInvalidate() , no change :( view is updated twice. Then i tried to change to size of my view , canvas . still Y it udates view only twice , though the onDraw() method is called evry time i click button. – voidRy Jan 05 '12 at 11:38
  • If you can detect that onDraws execute every time you click the button then it is up to the drawing bit of code, are you sure that getNewX() brings diffrent value and try to make a bigger difference, also the pain style FILL is used when you draw a rectangle or circle, I don't think you can fill the line. The paint should be defined in the constructor so that is not created every time you do onDraw – Lumis Jan 05 '12 at 11:52
  • change paint into Paint paint = new Paint (Paint.ANTI_ALIAS_FLAG); – Lumis Jan 05 '12 at 11:58
  • Yes getNewX() brings new value. Tried changing Paint object too. The X position changes 2 time and a new line is drawn . Can it be a problem related to view size? After 2 click on button nothing is drawn. – voidRy Jan 05 '12 at 12:05
  • I cannot help without seeing all the code... but yes you may be drawing outside of your view. In onSize change you can find its real size see this example: http://stackoverflow.com/questions/4938822/how-can-i-use-the-animation-framework-inside-the-canvas/4946893#4946893 – Lumis Jan 05 '12 at 12:13
  • I placed custom view in xml layout ,its working fine. still couldnt get it whats the difference ! :O – voidRy Jan 05 '12 at 12:29
  • I think, when you added your custom view in xml the layout size adjusted itself differently. – Lumis Jan 05 '12 at 12:43
0

Try this:

You didn't made an object of the LineView class like you should. You should use small letters for instances.

final Bitmap mBackgroundImage = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
cv =new Canvas(mBackgroundImage);
LineView lineView = new LineSeekbar(LineActivity.this);
lineView.setLayoutParams(new LayoutParams(500,500));
lineView.onMeasure(500,500);
lineView.invalidate();
lineView.draw(cv);
//  LineView = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,    LayoutParams.FILL_PARENT));
mImageView.setBackgroundColor(android.R.color.white);
mImageView.setImageBitmap( mBackgroundImage );
LinearLayout ll =(LinearLayout) findViewById(R.id.linearLayout1);
ll.addView(mImageView);

Button inc = (Button) findViewById(R.id.increase);
inc.setOnClickListener(new OnClickListener() {          
    @Override
    public void onClick(View v) {
        lineView.invalidate();
        lineView.draw(cv);              
    }
});
J. Maes
  • 6,862
  • 5
  • 27
  • 33
  • ya my mistake.. i corrected the naming .Still no changes in output. View is updated only twice , but onDraw methods is called everytime i click button. – voidRy Jan 05 '12 at 11:34
0

Solved it ,I placed custom view in xml layout ,its working fine.

voidRy
  • 674
  • 9
  • 20