3

任意のデバイスの中心に円または任意のオブジェクトを描画したいのですが、すべてのデバイスでデバイスの中心を正確に取得できません。一部のデバイスで作業できますが、最大で正確に描画されません。助けてください。ありがとうございます。事前にコードをここにも投稿しています

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

}
4

2 に答える 2

0

ステータスバーとタイトルバーのサイズを取得するためのリンクは次のとおりです。

その後、それらを次のように減算する必要があります。

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

幸運を。

于 2012-08-30T07:23:12.863 に答える
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();
        }
    }

}
于 2012-08-30T06:17:30.710 に答える