2016-04-21 20 views
-1

Ich muss durch Klicken auf die Schaltfläche Bild neu zeichnen, aber nach der Methode invalidate onDraw-Methode wird nicht aufgerufen. Der Aufruf onDraw erfolgt jedoch erst nach dem Ausführen der Anwendung.Invalidate ruft nicht auf Draw

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final Draw draw = new Draw(this); 

    Button button = (Button) findViewById(R.id.btn); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      draw.setA(40); 
      draw.setB(300); 
      draw.invalidate(); 
     } 
    }); 

} 
} 

Hier ist der Code Draw.java

public class Draw extends View { 

private Paint mPaint; 
private int a; 
private int b; 

public Draw(Context context) { 
    super(context); 
    init(); 
} 

public Draw(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
} 

public Draw(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(); 
} 

private void init() { 
    mPaint = new Paint(); 
    mPaint.setColor(Color.BLACK); 
    mPaint.setStrokeWidth(5); 
    setWillNotDraw(false); 
    a = 0; 
    b = 0; 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    mPaint.setStyle(Paint.Style.FILL); 
    mPaint.setColor(Color.WHITE); 
    canvas.drawPaint(mPaint); 

    mPaint.setColor(Color.BLACK); 
    mPaint.setAntiAlias(true); 
    canvas.drawLine(80, 50, 80, 500, mPaint); 
    canvas.drawLine(80, 50, 70, 85, mPaint); 
    canvas.drawLine(80, 50, 90, 85, mPaint); 

    canvas.drawLine(80, 500, 500, 500, mPaint); 
    canvas.drawLine(500, 500, 465, 510, mPaint); 
    canvas.drawLine(500, 500, 465, 490, mPaint); 

    mPaint.setTextSize(35); 
    mPaint.setStrokeWidth(2); 
    canvas.drawText("X", 480, 540, mPaint); 
    canvas.drawText("Y", 45, 80, mPaint); 
    canvas.drawText("0", 70, 540, mPaint); 

    drawFunction(canvas, a, b); 
} 

public void drawFunction(Canvas canvas, int a, int b) { 

    mPaint.setColor(Color.BLACK); 
    mPaint.setAntiAlias(true); 
    canvas.drawLine(80, 500, a, b, mPaint); 
} 

public void setA(int a) { 
    this.a = a; 
} 

public void setB(int b) { 

    this.b = b; 
} 
} 

main.xml

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <labs.example.function.Draw 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    </labs.example.function.Draw> 
</RelativeLayout> 

Vielen Dank im Voraus.

+0

schreiben Sie den Code für die Klasse – Raghunandan

+2

Zeichnen wo Sie den Blick auf Ihre Ansicht Hierarchie – Raghunandan

+0

id in XML erstellen Hinzufügen und dann findViewById Methode, um Ansicht zu Zeichnen anstatt die Schaffung neuen – USKMobility

Antwort

3

eine ID

<labs.example.function.Draw 
    android:id="@id/draw" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

</labs.example.function.Draw> 

final Draw draw = new Draw(this); 

von

final Draw draw = (Draw) findViewById(R.id.draw); // need to refer to the view in xml. 

Ersetzen hinzufügen Wenn Sie final Draw draw = new Draw(this); Sie diese Ansicht zu Ihrer Ansicht Hierachie hinzufügen müssen.

0

Haben Sie versucht, setWillNotDraw(false) anzurufen. In diesem post können Sie sehen, dass standardmäßig alle ViewGroup-Unterklassen ihre onDraw-Methode nicht aufrufen.

+0

erstreckt sich nicht Ansicht verwenden Viewgroup – Raghunandan