1

我必须以编程方式将 ImageView 添加到 RelativeLayout,使其在中心右侧 100dp。我为特定的屏幕尺寸和分辨率做了这个,但我希望它适用于任何尺寸的手机和密度。我为此尝试如下:

代码 :

    float xCord, yCord;
    float xCenter, yCenter;
    float radius;
    float angle;
    double x, y;

    TextView tv;
    RelativeLayout container;
    ImageView view;
    RelativeLayout.LayoutParams viewParams;
    DisplayMetrics metrics ;
    float density;
    int dps,pxs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_circular_animation);

        metrics = getApplicationContext().getResources().getDisplayMetrics();
        float width = metrics.widthPixels;
        float height = metrics.heightPixels;



        tv = (TextView)findViewById(R.id.tv);
        container = (RelativeLayout)findViewById(R.id.container);

        xCenter = convertToDP(width)/2;
        yCenter = convertToDP(height)/2;
        radius = 100;
        angle = 0;

        x = xCenter + radius * Math.cos(angle * Math.PI / 180);
        y = yCenter + radius * Math.sin(angle * Math.PI / 180);

        view = new ImageView(this);
        viewParams = new RelativeLayout.LayoutParams(50,50);
        viewParams.leftMargin = (int) x;
        viewParams.topMargin = (int) y;
        view.setLayoutParams(viewParams);
        view.setImageResource(R.drawable.square);
        container.addView(view);

        tv.setText(String.valueOf(xCenter) + " " + String.valueOf(yCenter));
    }

    public int convertToDP(float px) {

        density = getApplicationContext().getResources().getDisplayMetrics().density;       
        dps = (int) ((px/density) + 0.5f);
        return dps;
    }

}

我所做的是,获取屏幕尺寸和密度,然后将其除以 2 以获得中心线。一旦我有了绳子,我就使用公式在 0 度的圆周上得到点,但我没有得到正确的点。任何帮助都会很棒。

4

1 回答 1

0

试试这个:

public class sampleMediaPlayer extends Activity {
    // Toast mToast;
    // int i = 0;
    // private boolean click;
    // private Camera camera;
    Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainf);

    }

    @Override
    protected void onResume() {

        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);

                float r = DPtoPX(100);
                float mScreenWidth = rl.getWidth() / 2; // DPtoPX(100);
                float mScreenHeight = rl.getHeight() / 2;// DPtoPX(100);

                for (int i = 0; i < 360; i = i + 30) {
                    addView(rl, (int) (mScreenWidth + r * Math.cos(i * Math.PI / 180F)),
                            (int) (mScreenHeight + (int) r * Math.sin(i * Math.PI / 180F)));

                }

                addView(rl, (int) mScreenWidth, (int) ((mScreenHeight)));

            }
        }, 50);

        super.onResume();
    }

    void addView(ViewGroup rl, int x, int y) {
        Log.i("NIMISH", x + " = " + y);
        ImageView CenterImage = new ImageView(this);
        RelativeLayout.LayoutParams viewParamsCenter = new RelativeLayout.LayoutParams(10, 10);
        // viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
        viewParamsCenter.leftMargin = x;
        viewParamsCenter.topMargin = y;
        CenterImage.setLayoutParams(viewParamsCenter);
        CenterImage.setImageResource(R.drawable.ic_launcher);

        rl.addView(CenterImage);
    }

    float DPtoPX(float dp) {
        Resources r = getResources();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
    }
}

解释:

   float width = metrics.widthPixels;
   float height = metrics.heightPixels;

返回包括 TitleBar 和 StatusBar 在内的屏幕大小,但是当您绘制相对于 RelativeLayout 计算的图像时。因此,如果您使用高度“metrics.heightPixels”绘制图像,您将观察到图像超出屏幕。这种现象会影响真实中心的计算。

还有其他一些计算方法,但每种方法都需要在屏幕上绘制该视图。因此 onResume()( 使用处理程序) 之后的任何调用。正如你所看到的,计算标题栏高度的函数在调用表单 onResume() 时返回 0。

private float Correction() {
    Rect rectgle = new Rect();
    Window window = getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int StatusBarHeight = rectgle.top;
    int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
    int TitleBarHeight = contentViewTop - StatusBarHeight;

    Log.i("NIMISH", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);
    return contentViewTop;
}

如果您希望用户输入 X,Y 坐标,您可以通过以下方式更改第一个程序

 float mScreenWidth   = DPtoPX(100);
 float mScreenHeight  = DPtoPX(100);

这样做,它将参考相对布局绘制图像,因此您可以删除处理程序。

于 2013-01-22T12:04:03.057 回答