如何在android中的任何设备的中心画一个圆圈

时间:2012-08-30 06:07:13

标签: android canvas

我想在任何设备的中心画一个圆圈或任何物体。但是我无法准确地在所有设备中获得设备的中心。我能够在某些设备上工作,但最大限度地不能准确地绘图。请帮助..谢谢你。我也在这里发布我的代码

 package com.app.maxcircle;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class CircleWithMaxRadiusActivity extends Activity {
    /** Called when the activity is first created. */

    float pixelCenterX, pixelCenterY;
    DrawCanvasCircle pcc;
    LinearLayout ll;
    private Canvas canvas;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        ll = (LinearLayout) findViewById(R.id.ll);
        float centerx = width / 2;
        float centery = height / 2;
        pixelCenterX = convertDpToPixel(centerx, this);
        pixelCenterY = convertDpToPixel(centery, this);
        System.out.println("px..." + pixelCenterX);
        System.out.println("py..." + pixelCenterY);

        pcc = new DrawCanvasCircle(this);
        Bitmap result = Bitmap.createBitmap(25, 25, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(result);
        pcc.draw(canvas);
        pcc.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        ll.addView(pcc);
    }

    public static float convertDpToPixel(float dp, Context context) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return px;
    }

    /**
     * This method converts device specific pixels to device independent pixels.
     * 
     * @param px
     *            A value in px (pixels) unit. Which we need to convert into db
     * @param context
     *            Context to get resources and device specific display metrics
     * @return A float value to represent db equivalent to px value
     */
    public static float convertPixelsToDp(float px, Context context) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float dp = px / (metrics.densityDpi / 160f);
        return dp;

    }

    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, 2,
                    2 }, (float) 1.0);

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

            for (int i = 0; i < 25; i++) {
                canvas.drawCircle(pixelCenterX, pixelCenterY, pixelCenterY - 20
                        * i, p);
            }

            invalidate();
        }
    }

}

2 个答案:

答案 0 :(得分:0)

试试这个

public class TestSO extends Activity {
    /** Called when the activity is first created. */

    float pixelCenterX, pixelCenterY;
    DrawCanvasCircle pcc;
    LinearLayout ll;
    private Canvas canvas;
    private float centerX, centerY; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
        ll = (LinearLayout) findViewById(R.id.ll);
        pixelCenterX = convertPixelsToDp(width, this) / 2;
        pixelCenterY = convertPixelsToDp(height, this) / 2;

        centerX = convertDpToPixel(pixelCenterX, this);
        centerY = convertDpToPixel(pixelCenterY, this);

        System.out.println("px..." + pixelCenterX);
        System.out.println("py..." + pixelCenterY);

        pcc = new DrawCanvasCircle(this);
        Bitmap result = Bitmap.createBitmap(25, 25, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(result);
        pcc.draw(canvas);
        pcc.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        ll.addView(pcc);
    }

    public static float convertDpToPixel(float dp, Context context) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return px;
    }

    /**
     * This method converts device specific pixels to device independent pixels.
     * 
     * @param px
     *            A value in px (pixels) unit. Which we need to convert into db
     * @param context
     *            Context to get resources and device specific display metrics
     * @return A float value to represent db equivalent to px value
     */
    public static float convertPixelsToDp(float px, Context context) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float dp = px / (metrics.densityDpi / 160f);
        return dp;
    }

    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, 2,
                    2 }, (float) 1.0);

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

            for (int i = 0; i < 25; i++) {
                canvas.drawCircle(centerX, centerY, pixelCenterY - 20
                        * i, p);
            }

            invalidate();
        }
    }

}

答案 1 :(得分:0)

好的是link,你可以获得状态栏和标题栏大小。

之后,你需要用:

来减去它们
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight() - statusHeight - titleHeight ;

ll = (LinearLayout) findViewById(R.id.ll);
float centerx = width / 2;
float centery = height / 2;

// you don't actually need to convert to Dp though, but if that is what you want then do it.
pixelCenterX = convertPixelToDp(centerx, this);
pixelCenterY = convertPixelToDp(centery, this);
祝你好运。