【问题标题】:Scale drawable circle according to screen size根据屏幕尺寸缩放可绘制的圆圈
【发布时间】:2016-08-18 09:13:44
【问题描述】:

我已经有一段时间被困住了。我是一个新手安卓开发者。

我有一个可绘制的圆圈。

circle_shape.xml

<?xml version="1.0" encoding="utf-8"?>    
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:useLevel="false"
    android:shape="ring"
    android:thickness="0.8dp">
    <solid android:color="@android:color/holo_blue_dark"/>
</shape>

现在我想在我的活动布局中使用这个圆圈,使其覆盖整个屏幕。也就是说,屏幕的宽度应该等于圆的直径。 注意我没有设置圆的半径。

example_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/exampleLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.anil.raceorganizer.ExampleActivity">
    <ImageView
        android:id="@+id/race_field"
        android:src="@drawable/circular_shape"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:padding="0dp"
        android:layout_margin="0dp"
        ></ImageView>
</FrameLayout>

请注意,我尝试将填充和边距设置为 0,但没有成功。 我也尝试通过代码设置ImageView的大小,但它只是改变了整个ImageView的大小,而不是单独的圆圈。

这就是我用这段代码得到的。

非常感谢任何帮助。

【问题讨论】:

  • 如果你想用Canvas比xml画图,我可以举个例子
  • 如果你愿意,我可以教你如何用指南针画画
  • @Stallion - 请务必展示
  • @lelloman - 请务必展示

标签: android xml android-layout drawable shape


【解决方案1】:

你可以试试这个代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:thickness="0.8dp"
    android:useLevel="false"
    android:innerRadiusRatio="2.1">
    <solid android:color="@android:color/holo_blue_dark" />
</shape>

【讨论】:

  • 我不认为 innerradiusratio 与屏幕大小有任何关系
  • 您可以更改屏幕大小来测试我的代码。我保证它将正常工作。 :))。
    默认情况下,innerRadiusRatio = 1,这意味着你的圆 = 1/2 你的宽度。
  • 恐怕我无法改变我的客户手机屏幕的大小来匹配你的价值2.1!!这是一个现场项目!
  • this answer 表示 InnerRadiusRatio = RingWidth/Radius。其中 RingWidth 是视图的宽度。所以是的。您的回答确实正确,对我很有用。谢谢。
【解决方案2】:

使用 Canvas,我们可以绘制与屏幕宽度相匹配的形状。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);

    Canvas c = new Canvas(bmp);

    RectF rect = new RectF(0,0,width,width);
    drawCircle(rect, c, width, height);

}

private void drawCircle(RectF rect, Canvas c, int width, int height) {
        Paint paint = new Paint();
        paint.setARGB(255, 255 , 10, 21);
        paint.setStrokeWidth(8);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.BUTT);
        paint.setStyle(Paint.Style.STROKE);
        int radius;
        if(width < height)
            radius = width/2;
        else 
            radius = height/2;

        radius-= 4;
        c.drawCircle(width/2, height/2, radius, paint);
    }

不确定它是否对您有帮助。如果不是,请告诉我。我宁愿删除也不愿投反对票!

【讨论】:

  • 好吧,我不喜欢你的解决方案,你可能会做同样的事情来扩展 Drawable 并且恕我直言,它看起来会更好......但我没有足够的勇气对 Rambo 投反对票
  • @lelloman 在回答之前我询问是否可以将其作为答案。根据 anil_pulikoden 的回复,只有我发布了。
  • 它有效。但我认为我的活动不需要画布。谢谢!
【解决方案3】:

试试这个 你可以在drawable中使用这个标签

android:innerRadius="150dp"

相应地改变半径

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

使用上面的宽度

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:useLevel="false"
        android:shape="ring"
        android:innerRadius="150dp"
        android:thickness="0.8dp">
        <solid android:color="@android:color/holo_blue_dark"/>
    </shape>

【讨论】:

  • 但我希望它是屏幕的大小。不是 150dp。你知道如何动态设置这个半径吗?
  • 如何设置半径?那是我的问题1
【解决方案4】:

用这种方法得到环状结构。

使用尺寸标签保持 1:1 的纵横比

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<size android:width="50dp"
 android:height="50dp"/>

<stroke android:color="#000" android:width="0.1dp"/>

</shape>

【讨论】:

  • 我把这段代码。但是圈子没有变化。我的问题是如何将圆直径更改为屏幕宽度。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多