2013-07-30 9 views
53

Chcę narysować koło przez płótno. Tu jest mój kodu:Jak narysować koło przez płótno w systemie Android?

[MyActivity.java]:

public class MyActivity extends Activity 
{ 
public void onCreate(Bundle savedInstanceState) 
    { 
     ... 
     setContentView(new View(this,w,h)); 
    } 

} 

[View.java]:

public class View extends SurfaceView 
{ 
    public View(Context context, int w, int h) 
    { 
     super(context); 
     Canvas grid = new Canvas(Bitmap.createBitmap(h,w, Bitmap.Config.ARGB_8888)); 
     grid. drawColor(Color.WHITE); 
     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 
     grid.drawCircle(w/2, h/2 , w/2, paint); 
    } 
} 

więc mam tylko czarny ekran bez koła. Dlaczego to nie działa? Jak to naprawić?

Odpowiedz

3
@Override 
public void onDraw(Canvas canvas){ 
    canvas.drawCircle(xPos, yPos,radius, paint); 
} 

Powyżej znajduje się kod do renderowania okręgu. Dostosuj parametry do swojego ubioru.

24
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.os.Bundle; 
import android.view.View; 

public class MainActivity extends Activity 
{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(new MyView(this)); 
    } 

    public class MyView extends View 
    { 
     Paint paint = null; 
     public MyView(Context context) 
     { 
       super(context); 
       paint = new Paint(); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) 
     { 
      super.onDraw(canvas); 
      int x = getWidth(); 
      int y = getHeight(); 
      int radius; 
      radius = 100; 
      paint.setStyle(Paint.Style.FILL); 
      paint.setColor(Color.WHITE); 
      canvas.drawPaint(paint); 
      // Use Color.parseColor to define HTML colors 
      paint.setColor(Color.parseColor("#CD5C5C")); 
      canvas.drawCircle(x/2, y/2, radius, paint); 
     } 
    } 
} 

Edit jeśli chcesz narysować okrąg w środku. Można też tłumaczyć całe płótno do środka, a następnie narysować okrąg na center.using

canvas.translate(getWidth()/2f,getHeight()/2f); 
canvas.drawCircle(0,0, radius, paint); 

Te dwa łącza również pomóc

http://www.compiletimeerror.com/2013/09/introduction-to-2d-drawing-in-android.html#.VIg_A5SSy9o

http://android-coding.blogspot.com/2012/04/draw-circle-on-canvas-canvasdrawcirclet.html

8
public class CircleView extends View { 

    private static final String COLOR_HEX = "#E74300"; 
    private final Paint drawPaint; 
    private  float size; 

    public CircleView(final Context context, final AttributeSet attrs) { 
     super(context, attrs); 
     drawPaint = new Paint(); 
     drawPaint.setColor(Color.parseColor(COLOR_HEX)); 
     drawPaint.setAntiAlias(true); 
     setOnMeasureCallback(); 
    } 

    @Override 
    protected void onDraw(final Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.drawCircle(size, size, size, drawPaint); 
    } 

    private void setOnMeasureCallback() { 
     ViewTreeObserver vto = getViewTreeObserver(); 
     vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       removeOnGlobalLayoutListener(this); 
       size = getMeasuredWidth()/2; 
      } 
     }); 
    } 

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
    private void removeOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener listener) { 
     if (Build.VERSION.SDK_INT < 16) { 
      getViewTreeObserver().removeGlobalOnLayoutListener(listener); 
     } else { 
      getViewTreeObserver().removeOnGlobalLayoutListener(listener); 
     } 
    } 
} 

Xml przykład: będzie produkować krąg 5dp

<com.example.CircleView 
     android:layout_width="10dp" 
     android:layout_height="10dp"/> 
1

Spróbuj

enter image description here

cały kod do rysowania kod źródłowy projektu okrąg lub pobrać i przetestować go na Android studio. Draw circle on canvas programmatically.

import android.graphics.Bitmap; 
    import android.graphics.Canvas; 
    import android.graphics.Color; 
    import android.graphics.Paint; 
    import android.graphics.Path; 
    import android.graphics.Point; 
    import android.graphics.PorterDuff; 
    import android.graphics.PorterDuffXfermode; 
    import android.graphics.Rect; 
    import android.graphics.RectF; 
    import android.widget.ImageView; 


     public class Shape { 

      private Bitmap bmp; 
      private ImageView img; 
      public Shape(Bitmap bmp, ImageView img) { 

       this.bmp=bmp; 
       this.img=img; 
       onDraw(); 
      } 

      private void onDraw(){ 
       Canvas canvas=new Canvas(); 
       if (bmp.getWidth() == 0 || bmp.getHeight() == 0) { 
        return; 
       } 

       int w = bmp.getWidth(), h = bmp.getHeight(); 

       Bitmap roundBitmap = getRoundedCroppedBitmap(bmp, w); 

       img.setImageBitmap(roundBitmap); 

      } 

      public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) { 
       Bitmap finalBitmap; 
       if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) 
        finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, 
          false); 
       else 
        finalBitmap = bitmap; 
       Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(), 
         finalBitmap.getHeight(), Bitmap.Config.ARGB_8888); 
       Canvas canvas = new Canvas(output); 

       final Paint paint = new Paint(); 
       final Rect rect = new Rect(0, 0, finalBitmap.getWidth(), 
         finalBitmap.getHeight()); 

       paint.setAntiAlias(true); 
       paint.setFilterBitmap(true); 
       paint.setDither(true); 
       canvas.drawARGB(0, 0, 0, 0); 
       paint.setColor(Color.parseColor("#BAB399")); 
       canvas.drawCircle(finalBitmap.getWidth()/2 + 0.7f, finalBitmap.getHeight()/2 + 0.7f, finalBitmap.getWidth()/2 + 0.1f, paint); 
       paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
       canvas.drawBitmap(finalBitmap, rect, rect, paint); 

       return output; 
      }