2016-08-18 3 views
4

되었습니다. 나는 초보자 Android 개발자입니다.화면 크기에 따라 그리기 가능 원 크기가

나는 드로어 서클을 가지고 있습니다.

circular_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> 

는 지금은이 화면 전체를 커버하는 방식으로 내 활동 레이아웃이 원을 사용하고 싶습니다. 즉, 화면의 너비는 원의 지름과 같아야합니다. 원의 반지름을 설정하지 않았습니다. 내가 0으로 패딩과 마진을 넣어 시도하지만 작동하지 않았다

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> 

참고. 코드를 통해 ImageView의 크기를 설정하려고했지만 ImageView의 크기 만 변경되었고 전체적으로는 표시되지 않았습니다.

이것은이 코드와 관련이 있습니다.

enter image description here

어떤 도움

이 크게 감사합니다.

+1

당신은 내가 어떻게 그릴하는 방법을 보여줄 수 원한다면, 내가주고 예를 – Stallion

+1

수있는 XML 이상의 이미지를 그릴 캔버스를 사용하려는 경우 나침반 – lelloman

+0

@ Stallion - 표시하십시오 –

답변

1

에서 폭을 사용하는 당신은 그릴 수

android:innerRadius="150dp" 

변화에이 태그를 사용할 수 있습니다이 시도 :
<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>

+0

innerradiusratio는 화면 크기와 관련이 없다고 생각하지 않는다. –

+1

내 코드를 테스트하기 위해 화면 크기를 변경할 수있다. 나는 그것이 올바르게 작동 할 것이라고 확신한다. :)).
기본적으로 innerRadiusRatio = 1 인 경우 원이 너비 1/2입니다. –

+0

귀하의 가치와 일치하도록 내 고객 모바일 화면의 크기를 변경할 수 없습니다. _2.1_ !! 이것은 라이브 프로젝트입니다! –

0

당신이 코드를 시도 할 수 있습니다 반경이 따라

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> 
+0

하지만 나는 그것을 화면의 크기가되고 싶습니다. 150dp가 아닙니다. 이 반경을 동적으로 설정하는 방법을 알고 있습니까? –

+0

예 잠깐만 기다려주십시오. –

+0

반경 설정 방법은? 그게 내 질문 이었어 –

1

캔버스를 사용하여 화면 너비에 맞는 모양을 그릴 수 있습니다.

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

도움이 될지 확실하지 않은 경우. 그렇지 않다면 알려주세요. 내가 downvotes을 받고보다 삭제를 선호합니다!

+0

좋아, 나는 당신의 솔루션을 좋아하지 않아 Drawable과 imho를 확장하는 것과 똑같은 일을 할 수도있다. 더 좋아 보이지만 ... Rambo를 downvote 할만큼 충분히 용감하지 않다. – lelloman

+0

대답하기 전에 @lelloman 내가 이것을 물을 수 있는지 물었다. 대답으로. anil_pulikoden의 회신에 따르면 나는 게시했습니다. – Stallion

+0

그것은 작동합니다. 하지만 나는 내 활동을 위해 캔버스가 필요하다고 생각하지 않는다. 그레이 시어! –

0

이런 종류의 접근법을 사용하여 링과 같은 구조를 만듭니다.

사용 크기 태그 보존하기 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> 
+0

이 코드를 삽입했습니다. 그러나 동그라미에는 변화가 없습니다. 제 질문은 원의 지름을 화면 폭으로 변경하는 방법이었습니다. 어떤 아이디어? –

관련 문제