我只是想達到什麼是上述image.Red是我的圖標,我需要地方上的圓形圖像borders.Each和每一個紅色圖標有自己的function.Say頂部它的一種評級按鈕。在這裏我需要幫助,如何使XML設計來實現這一點。我不知道如何做到這一點,所以我不能在這個問題中添加代碼。需要幫助提前感謝。
1
A
回答
1
請看看這堂課。它應該工作,只要你想:
public class CircleLayout extends ViewGroup {
private static final float DEFAULT_FROM_DEGREES = -90f;
private static final float DEFAULT_TO_DEGREES = 270f;
private static final int MIN_RADIUS_DP = 100;
private static final float DEFAULT_CHILD_PADDING_DP = 5f;
private final Rect tempChildFrame = new Rect();
private final float fromDegrees;
private final float toDegrees;
private final int childPadding;
private final int minRadius;
private int radius;
private int childSize;
private boolean expanded;
public CircleLayout(Context context) {
this(context, null);
}
public CircleLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CircleLayout(Context context, AttributeSet attrs, int def) {
super(context, attrs);
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CircleLayout, 0, 0);
try {
childSize = Math.max(a.getDimensionPixelSize(R.styleable.CircleLayout_childSize, 0), 0);
fromDegrees = a.getFloat(R.styleable.CircleLayout_fromDegrees, DEFAULT_FROM_DEGREES);
toDegrees = a.getFloat(R.styleable.CircleLayout_toDegrees, DEFAULT_TO_DEGREES);
final float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_CHILD_PADDING_DP, getResources().getDisplayMetrics());
childPadding = (int) a.getDimension(R.styleable.CircleLayout_childPadding, px);
expanded = a.getBoolean(R.styleable.CircleLayout_expandedChild, true);
minRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MIN_RADIUS_DP, getResources().getDisplayMetrics());
} finally {
a.recycle();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
radius = computeRadius(Math.abs(toDegrees - fromDegrees), getChildCount(), childSize, childPadding, minRadius);
final int size = radius * 2 + childSize + childPadding;
setMeasuredDimension(size + getPaddingLeft() + getPaddingRight(), size + getPaddingTop() + getPaddingBottom());
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY));
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int centerX = getWidth()/2;
final int centerY = getHeight()/2;
final int radius = expanded ? this.radius : 0;
final int childCount = getChildCount();
final float perDegrees = (toDegrees - fromDegrees)/childCount;
float degrees = fromDegrees;
for (int i = 0; i < childCount; i++) {
computeChildFrame(centerX, centerY, radius, degrees, childSize, tempChildFrame);
degrees += perDegrees;
getChildAt(i).layout(tempChildFrame.left, tempChildFrame.top, tempChildFrame.right, tempChildFrame.bottom);
}
}
private int computeRadius(float arcDegrees, int childCount, int childSize, int childPadding, int minRadius) {
if (childCount < 2) {
return minRadius;
}
final float perDegrees = arcDegrees/childCount;
final float perHalfDegrees = perDegrees/2;
final int perSize = childSize + childPadding;
final int radius = (int) ((perSize/2)/Math.sin(Math.toRadians(perHalfDegrees)));
return Math.max(radius, minRadius);
}
private void computeChildFrame(int centerX, int centerY, int radius, float degrees, int size, Rect result) {
final double childCenterX = centerX + radius * Math.cos(Math.toRadians(degrees));
final double childCenterY = centerY + radius * Math.sin(Math.toRadians(degrees));
final float halfSize = size * .5f;
result.left = (int) (childCenterX - halfSize);
result.top = (int) (childCenterY - halfSize);
result.right = (int) (childCenterX + halfSize);
result.bottom = (int) (childCenterY + halfSize);
}
public void expand() {
expanded = true;
requestLayout();
}
public void shrink() {
expanded = false;
requestLayout();
}
public void setChildSize(int size) {
if (childSize == size || size < 0) {
return;
}
childSize = size;
requestLayout();
}
public boolean isExpanded() {
return expanded;
}
@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
final SavedState result = new SavedState(superState);
result.childSize = childSize;
result.expanded = expanded;
return result;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
final SavedState ss = (SavedState) state;
childSize = ss.childSize;
expanded = ss.expanded;
super.onRestoreInstanceState(ss.getSuperState());
}
public static class SavedState extends BaseSavedState {
@SuppressWarnings("hiding")
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
private int childSize;
private boolean expanded;
SavedState(Parcelable superState) {
super(superState);
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(childSize);
out.writeInt(expanded ? 1 : 0);
}
private SavedState(Parcel in) {
super(in);
childSize = in.readInt();
expanded = in.readInt() != 0;
}
}
}
您還必須添加樣式這一觀點:
<declare-styleable name="CircleLayout">
<attr name="childSize" format="dimension"/>
<attr name="childPadding" format="dimension"/>
<attr name="fromDegrees" format="integer"/>
<attr name="toDegrees" format="integer"/>
<attr name="expandedChild" format="boolean"/>
</declare-styleable>
示例使用:
<your.package.CircleLayout
android:clipChildren="false"
android:id="@+id/test_circle_view"
android:layout_width="match_parent"
app:childSize="50dp"
app:childPadding="25dp"
app:expandedChild="true"
android:layout_height="match_parent">
<ImageView
android:id="@+id/circle_item_1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/some_circle"/>
<ImageView
android:id="@+id/circle_item_2"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/some_circle"/>
<ImageView
android:id="@+id/circle_item_3"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/some_circle"/>
<ImageView
android:id="@+id/circle_item_4"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/some_circle"/>
<ImageView
android:id="@+id/circle_item_5"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/some_circle"/>
<ImageView
android:id="@+id/circle_item_6"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/some_circle"/>
</your.package.CircleLayout>
+0
而不是圓形佈局我將使用普通圖像視圖,但src圖像將與padding.HOP你得到我試着g在這裏說 –
+0
但是你想如何爲你的圖標實現沒有視圖的功能?我的解決方案對你有幫助嗎? –
相關問題
- 1. 如何將圖像視圖放置在surfaceview頂部android
- 2. 如何將圖像放在圓形圖像視圖中
- 3. 如何將自定義標記放置在多邊形Google地圖頂部Android
- 4. 在android中,如何將標籤放在包含頂部圖像的視圖中?
- 5. 如何在MATLAB的圖形頂部放置座標軸對象?
- 6. 長方形按鈕,頂部的圓形圖標中的Android
- 7. 如何將圖形放在Android相機預覽的頂部?
- 8. 如何在android中的圖像的頂部放置文本?
- 9. Android:環形內部的圓形圖像
- 10. 如何將圖像放在另一個圖像的頂部
- 11. 如何將矩形圖像設置爲圓形圖像
- 12. 如何在圖像頂部繪製圓形
- 13. Android的圓形邊框圓形圖像
- 14. 在圖像視圖頂部繪製圓形
- 15. 在圖像頂部放置一個div
- 16. 如何在多邊形頂部放置圓?
- 17. 如何將圖標放在JPanel的頂部和底部
- 18. 在圖像的頂部放置一個圖像
- 19. 將頂點標籤自由放置在頂點之外用於R中的圓形圖形
- 20. 條形圖頂部的圓圈d3.js
- 21. 如何在圓形物體上放置圖像?
- 22. Android Image圓形頂部
- 23. 如何在銷圖像頂部顯示用戶位置圖標?
- 24. 如何在android中設置圓形圖像邊界的顏色?
- 25. 如何裁剪圓形圖像openvc android
- 26. Perl Image :: Magick:將圖像放在其他圖像的頂部
- 27. 如何在HTML中將一個圖像放在另一個圖像的頂部?
- 28. 如何在網頁上的背景圖像頂部放置圖像
- 29. 圖像頂部的位置標籤
- 30. 如何將視頻放在Wordpress上的圖像頂部?
你必須使用相對佈局,你可以試試約束佈局以及 –
@AsifSb看看我的解決方案 –