簡體   English   中英

Android:橢圓形看起來很糟糕

[英]Android: oval shape looks bad

我有一個帶有橢圓形背景的按鈕,它看起來不像一個完美的圓形,我可以注意到在每個距離處都在 circunfrence 中“斷裂”,這就是它的外觀

在此處輸入圖片說明

背景:

<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#7eadb6"/>
<size android:height="80dp"
    android:width="80dp"/>

編輯:創建按鈕

static Button theButton1;
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
            (80, 80);

theButton1 = new Button(getActivity());
theButton1.setBackgroundResource(R.drawable.subject_button);
theButton1.setLayoutParams(params);
theButton1.setTextColor(Color.parseColor("#ffffff"));
theButton1.setTextSize(23);
theButton1.setText(prefs.getString("text1", "").substring(0, 1));
theButton1.setOnClickListener(this);

這是正常的嗎? 如果不是,我該如何解決?

注意:我創建了不止一個這樣的按鈕

這可能是一個抗鋸齒問題。 您必須為其禁用硬件加速。 嘗試這個-

<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android"
android:hardwareAccelerated="false"
>

<solid android:color="#7eadb6"/>
<size android:height="80dp"
    android:width="80dp"/>
</shape>

更新:

您還必須將按鈕的圖層類型設置為軟件。

theButton1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

設置布局參數后。

錯誤的問題會導致錯誤的答案!!!

通過閱讀您的評論,我知道您最終想要實現這樣的目標

在此處輸入圖片說明

一個帶有文字的圓圈。

您可以使用TextDrawable 庫輕松實現目標

現在讓我們回到你的問題,問題肯定只是因為抗鋸齒。 您的問題在 l-dpi 設備上變得更糟。

訣竅是制作位圖並將其作為所需視圖的背景傳遞。

public Bitmap getCircleBitmap(int dimenSize,int mColor) {
    Bitmap output = Bitmap.createBitmap(dimenSize, dimenSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    int x = dimenSize;
    int y = dimenSize;
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.TRANSPARENT);
    canvas.drawPaint(paint);
    canvas.drawBitmap(output, 0.0f, 0.0f, paint);
    paint.setColor(mColor);
    canvas.drawCircle(x / 2, y / 2, dimenSize/2, paint);
    return output;
}

現在你有了一個 SHARP 圓位圖並且可以很容易地使用它。

更新只需像這樣在 xml 文件中更新您的Button

<Button
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:text="mytext"
    android:id="@+id/button"
     />

保持您的activity

theButton1 = new Button(getActivity());
theButton1.setBackgroundResource(R.drawable.subject_button);
theButton1.setTextColor(Color.parseColor("#ffffff"));
theButton1.setTextSize(23);
theButton1.setText(prefs.getString("text1", "").substring(0, 1));
theButton1.setOnClickListener(this);

我不認為你的實施有什么問題。 但嘗試在 xml 中執行此操作,因為在運行時執行所有這些操作時可能會導致問題。 創建一個按鈕並將其背景設置為可繪制的橢圓形

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/oval"
android:gravity="center_vertical|center_horizontal"
android:text="button text here"
/>

drawable 沒有問題。

ovelshape.xml

  <shape android:shape="oval"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#7eadb6"/>

在您的 XML 中,您需要以 dp 為單位提供高度和寬度。 並使用視圖而不是按鈕。 我不知道為什么,但它對我有用。

<View android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/ovelshape"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM