2016-06-14 8 views
0

Ich benutzte etwas Code von dieser Site, um auf einer Bitmap mit Canvas zu zeichnen, und ich testete es auf meinem Telefon und es schien lächerlich langsam und es fühlte sich an, als ob ich schnell zeichnete das Ganze nicht registriert Bewegung, aber nur Teile davon. Außerdem möchte ich eine Schaltfläche an eine Methode innerhalb einer Klasse binden, die nicht die Hauptaktivitätsklasse ist. Wie mache ich das? Code: MainActiviy.java (genannt MySecondDrawingSpace.java)Leistungsprobleme mit Canvas

public class MySecondDrawingSpace extends Activity 
    { 
    DrawView myDrawView; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     // myDrawView = new MyDrawView(this, null); 
     setContentView(R.layout.activity_my_second_drawing_space); 
     myDrawView = (DrawView)findViewById(R.id.draw); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 



     getMenuInflater().inflate(R.menu.menu_my_second_drawing_space, menu); 
     return true; 
    } 
} 

die XML-Datei

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MySecondDrawingSpace" 
    android:background="@drawable/kariert"> 
    <com.drawingspace.dakson.myseconddrawingspace.DrawView 
     android:id ="@+id/draw" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Clear" 
     android:onClick="clear" 
     android:id="@+id/button" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

    <Button 
     android:id ="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="save" 
     android:layout_alignParentBottom="true" 
     /> 



</RelativeLayout> 

The View Klasse

public class DrawView extends View { 
    public Bitmap mBitmap; 
    public Canvas mCanvas; 
    private Path mPath; 
    private Paint mBitmapPaint; 
    private Paint mPaint; 


    public DrawView(Context c, AttributeSet attrs) { 
     super(c, attrs); 

     mPath = new Path(); 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 

     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(0xFF000000); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(9); 

    } 


    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     mCanvas = new Canvas(mBitmap); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 

     canvas.drawPath(mPath, mPaint); 


    } 

    private float mX, mY; 
    private static final float TOUCH_TOLERANCE = 4; 

    private void touch_start(float x, float y) { 
     mPath.reset(); 
     mPath.moveTo(x, y); 
     mX = x; 
     mY = y; 
    } 
    private void touch_move(float x, float y) { 
     float dx = Math.abs(x - mX); 
     float dy = Math.abs(y - mY); 
     if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
      mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
      mX = x; 
      mY = y; 
     } 
    } 
    private void touch_up() { 
     mPath.lineTo(mX, mY); 
     // commit the path to our offscreen 
     mCanvas.drawPath(mPath, mPaint); 
     // kill this so we don't double draw 
     mPath.reset(); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       touch_start(x, y); 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       touch_move(x, y); 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_UP: 
       touch_up(); 
       invalidate(); 
       break; 
     } 
     return true; 
    } 
    public void clear(){ 
     mBitmap.eraseColor(Color.GREEN); 
     invalidate(); 
     System.gc(); 

    } 

} 

Antwort

0

Wenn Touch-Ereignisse schneller gehen, dann onDraw, dann Sie müssen nicht jedes Mal zeichnen, wenn Sie einen neuen Punkt haben, sollten Sie zeichnen, wenn es tatsächlich gefragt wird - in onDraw erfüllt Hod. Obwohl Skia (die auf Android-Canvas basiert) nicht so schnell ist, so viele Quads zu zeichnen. Sie können eine einfache Reihe von Linien zeichnen, aber diese Linien als zentripetale Catmull-Rom-Splines oder Splines bilden, was für Sie besser ist.

Und mehr kann Path speichern nicht so viel Linien

+0

Ist nicht die OnDraw-Methode aufgerufen, wenn i invalidate() aufrufen? Also, wie könnte ich das ändern, da ich immer Ungültig anrufen muss, wenn ich den Bildschirm berühre. Weiterhin meinst du, dass alle meine gezeichneten 'Pfade' grundsätzlich als Array gespeichert sind und es zu groß wird? Danke für Ihre Antwort BTW –