简体   繁体   English

Android:椭圆形看起来很糟糕

[英]Android: oval shape looks bad

I have a button with a oval background and it doesn't look like a perfect circle, I can notice at every distance "breaks" in the circunfrence, this is how it looks我有一个带有椭圆形背景的按钮,它看起来不像一个完美的圆形,我可以注意到在每个距离处都在 circunfrence 中“断裂”,这就是它的外观

在此处输入图片说明

the background:背景:

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

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

Edit: Creating the button编辑:创建按钮

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);

Is this normal?这是正常的吗? If not how can i fix it?如果不是,我该如何解决?

Note: I have more than one button created like this注意:我创建了不止一个这样的按钮

This might be an anti-aliasing issue.这可能是一个抗锯齿问题。 You have to disable hardware acceleration for it.您必须为其禁用硬件加速。 Try 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>

UPDATE:更新:

You will also have to set the layer type of button to software.您还必须将按钮的图层类型设置为软件。

theButton1.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

after you set the layout params.设置布局参数后。

Wrong question leads to wrong answers !!!错误的问题会导致错误的答案!!!

By reading your comments, i understand you want to achieve such a thing at last通过阅读您的评论,我知道您最终想要实现这样的目标

在此处输入图片说明

A circle with a text on it .一个带有文字的圆圈。

you can easily reach your aim by using TextDrawable Library您可以使用TextDrawable 库轻松实现目标

Now lets back to your problem, the problem is Surely just because Anti Aliasing.现在让我们回到你的问题,问题肯定只是因为抗锯齿。 Your problem become even worse on l-dpi devices.您的问题在 l-dpi 设备上变得更糟。

The trick is making a bitmap and pass it as background of your desired view.诀窍是制作位图并将其作为所需视图的背景传递。

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;
}

Now you have a SHARP circle bitmap and can easily use it.现在你有了一个 SHARP 圆位图并且可以很容易地使用它。

Update just update your Button in the xml file like this:更新只需像这样在 xml 文件中更新您的Button

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

and Keep in your activity :保持您的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);

i dont think something is wrong with your implementation.我不认为你的实施有什么问题。 but try doing it in xml, because it might be causing issues while doing all this at run time.但尝试在 xml 中执行此操作,因为在运行时执行所有这些操作时可能会导致问题。 create a button and set its background as your oval shape drawable创建一个按钮并将其背景设置为可绘制的椭圆形

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

There is no issue with drawable. drawable 没有问题。

ovelshape.xml ovelshape.xml

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

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

In your XML you need to provide the height & width in dp.在您的 XML 中,您需要以 dp 为单位提供高度和宽度。 And use View instead of Button.并使用视图而不是按钮。 I don't no why but it works for me.我不知道为什么,但它对我有用。

<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