1

これは、画面を半分に分割している main.xml ファイルです。下半分には、緯度と経度のラベルがあり、各ラベルに対応して、テキストボックスに現在の緯度と経度の値を表示するテキストボックスがあります。

<?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> 

しかし、私の質問は、上半分に円を描く方法です。以下は、私がペイントで作成した例です。上半分にキャンバスを使用して円を描く必要があります。そして、下半分の部分はうまく機能しています。 ここに画像の説明を入力

キャンバスに円を描くための別のクラスファイルを作成しました-

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);
}

しかし、それは正しく表示されていません。私がやっている何かが間違っています。

4

1 に答える 1

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);
于 2012-07-19T07:08:07.513 に答える