我必须以编程方式将ImageView添加到RelativeLayout,使其距离中心右侧100dp。我这样做是为了特定的屏幕大小和分辨率,但我希望它适用于任何尺寸的手机和desnity。我为此尝试的内容如下:
代码:
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度,但我得不到正确的点。任何帮助都会很棒。
答案 0 :(得分: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);
这样做,它将参考相对布局绘制图像,因此您可以删除处理程序。