1
我必須以編程方式將ImageView添加到RelativeLayout,使其位於中心右側100dp。我爲了一個特定的屏幕尺寸和分辨率做了這個,但我希望它能適用於任何尺寸的手機和平板電腦。我試過這個如下:獲取屏幕的中心線並根據它對齊視圖
CODE:
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得到中央線。一旦我有繩索,我用公式來獲得0deg圓周上的點,但我沒有得到正確的觀點。任何幫助都會很棒。
你想把圖像放在圓圈上還是隻放100dp到中心 –
我想把它們放置成100dp或200dp任何東西(userinput)關於他/她屏幕的中心。如果用戶輸入是X:200和Y:400,半徑:100,角度:50,那麼應該通過x = xCenter + radius * Math.cos(angle * Math.PI/180)獲得簾線。 y = yCenter + radius * Math.sin(angle * Math.PI/180); 這裏xCenter和yCenter是屏幕中心點的簾線。 – ark
這是一個循環路徑,如果這就是你要求的。 – ark