2012-07-19 57 views
1

這是main.xml文件,它將屏幕分成兩半。在下半部分,它具有經度和緯度標籤,並且與每個標籤對應,它具有文本框,該文本框將顯示文本框中當前的緯度和經度值。在Android屏幕的上半部分使用畫布繪製圓圈

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@android:color/black" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <!-- currently empty --> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

      <!-- Latitude Label --> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Latitude" 
       android:textColor="#FFFFFF" /> 

      <EditText 
       android:id = "@+id/lat1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="20dip" 
       android:layout_marginTop="5dip" 
       android:layout_weight="0.35" 
       android:singleLine="true" 
       android:textColor="#FFFFFF" 
       /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

      <!-- Longitude Label --> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="Longitude" 
       android:textColor="#FFFFFF" /> 

      <EditText 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dip" 
       android:layout_weight="0.35" 
       android:singleLine="true" 
       android:textColor="#FFFFFF" 
       android:id = "@+id/lat2" /> 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 

但我的問題是 - 我怎麼能在上半部分畫一個圓。以下是我在paint中創建的示例。我需要在上半部分使用畫布繪製一個圓圈。下半部分對我來說很好。 enter image description here

我創造了另一個類文件繪製圓的canvas-

public class DrawCanvasCircle extends View{ 
    public DrawCanvasCircle(Context mContext) { 
     super(mContext); 
    } 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Paint p = new Paint(); 
     p.setColor(Color.WHITE); 
     DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, (float)1.0); 

     p.setPathEffect(dashPath); 
     p.setStyle(Style.STROKE); 


     for (int i = 0; i < 7; i ++) { 
      canvas.drawCircle(100, 100, 50+(i*10), p); 
     } 


     invalidate(); 
    } 


} 

而且下面是主類,其中我想補充以上,我創建到主類的畫布。

@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ll = (LinearLayout) findViewById(R.id.lc); 
     DrawCanvasCircle pcc = new DrawCanvasCircle (this); 
     Bitmap result = Bitmap.createBitmap(25, 25, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(result); 
     pcc.draw(canvas); 
     pcc.setLayoutParams(new LayoutParams(1000, 1000)); 
     ll.addView(pcc); 
} 

但它沒有得到正確顯示。任何錯誤,我正在做。

+0

? – 2012-07-19 06:14:58

+0

我不確定當前考慮我的佈局繪製圓的最佳方式是什麼?由於我是Android新手,因此根據您的經驗,在Android Screen Screen – ferhan 2012-07-19 06:17:56

+0

的上半部分繪製圓圈的最佳方式是創建自定義視圖以繪製圓。 – 2012-07-19 06:20:47

回答

1

更改customView類像

public class DrawCanvasCircle extends View 
{ 
    Context context; 

    public DrawCanvasCircle(Context mContext) 
    { 
     super(mContext); 
     context = mContext; 
    } 

    protected void onDraw(Canvas canvas) 
    { 
     super.onDraw(canvas); 

     Paint paint = new Paint(); 
     paint.setColor(0xFF0000); 
     paint.setAlpha(255); 
     paint.setStrokeWidth(2.0f); 
     paint.setStyle(Paint.Style.STROKE); 
     WindowManager mWinMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); 
     int displayWidth = mWinMgr.getDefaultDisplay().getWidth(); 
     int displayHeight = mWinMgr.getDefaultDisplay().getHeight(); 
     int circleRadius = 100; 
     canvas.drawCircle(displayWidth/2, displayHeight/4, circleRadius, paint); 
     invalidate(); 
    } 

} 

,並在下面寫一行代碼到您的onCreate方法

LinearLayout ll = (LinearLayout) findViewById(R.id.lc); 
DrawCanvasCircle pcc = new DrawCanvasCircle (this); 
ll.addView(pcc); 
你想要的代碼/ customview繪製圓