xml 설명에서 드로어 블을 회전 할 수 있습니까? 수 있다는 것을 알고 Matrix있지만 이미 설명했듯이

재사용 할 수있는 리소스로 앱을 만들고 있습니다 (버튼은 항상 동일하지만 미러링되거나 회전되기 때문입니다). 동일한 리소스를 사용하고 싶으므로 원본과 똑같지 만 회전 된 리소스를 3 개 더 추가 할 필요가 없습니다. 그러나 나는 또한 코드를 XML로 선언 할 수있는 것들과 혼합하거나 처리 시간을 소모하는 행렬로 변환을 만들고 싶지 않습니다.

XML에 선언 된 두 개의 상태 버튼이 있습니다.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/and_card_details_button_down_left_onclick" /> <!-- pressed -->
    <item android:drawable="@drawable/and_card_details_button_down_left" /> <!-- default -->
</selector>

드로어 블은 동일하지만 90º 및 45º 회전하므로 다시 사용하고 싶습니다. 버튼에 드로어 블로 할당합니다.

<Button android:id="@+id/Details_Buttons_Top_Left_Button"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/details_menu_large_button" />

a RotateDrawable또는 a를 사용하여 회전 할 수 있다는 것을 알고 Matrix있지만 이미 설명했듯이 그 접근 방식이 마음에 들지 않습니다.

XML에서 직접 달성 할 수 있습니까? 아니면 이것이 최선의 방법이라고 생각하십니까? 모든 리소스를 회전하지만 코드에서 회전 하시겠습니까?

— 편집 — @dmaxi의 대답은 훌륭하게 작동합니다. 이것은 항목 목록과 결합하는 방법입니다. 🙂

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

    <item android:state_pressed="true">
        <rotate
        android:fromDegrees="90"
        android:toDegrees="90"
        android:pivotX="50%"
        android:pivotY="50%"
        android:drawable="@drawable/and_card_details_button_up_onclick"/>
    </item>

    <item>
        <rotate
        android:fromDegrees="90"
        android:toDegrees="90"
        android:pivotX="50%"
        android:pivotY="50%"
        android:drawable="@drawable/and_card_details_button_up_onclick"/>
    </item>

</selector>



답변

XML로 회전 할 수 있습니다 .

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="90"
        android:toDegrees="90"
        android:pivotX="50%"
        android:pivotY="50%"
        android:drawable="@drawable/mainmenu_background">
</rotate>

fromDegrees중요하다.

기본적으로 이것은 XML로 정의 된 회전 애니메이션입니다. 함께 fromDegrees하면 초기 회전 상태를 정의합니다. 는 toDegrees애니메이션 시퀀스에서 당김의 최종 회전 된 상태이지만 애니메이션을 사용하지 않으려면 아무것도 할 수있다.

애니메이션으로로드 할 필요가 없기 때문에 애니메이션에 리소스를 할당하지 않는다고 생각합니다. 드로어 블로서 초기 상태로 렌더링되며 drawable리소스 폴더에 넣어야 합니다. 애니메이션으로 사용하려면 anim리소스 폴더에 넣어야하며 다음 과 같이 애니메이션을 시작할 수 있습니다 (예제 일뿐).

Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
rotation.setRepeatCount(Animation.INFINITE);
myView.startAnimation(rotation);


답변

XML에서 다음과 같이 왼쪽 화살표를 오른쪽으로 회전 할 수 있습니다.

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="180"
    android:toDegrees="0"
    android:drawable="@drawable/left">
</rotate>

참고를 위해 첨부 된 이미지.

여기에 이미지 설명 입력


답변

ImageView , 스타일 및 색상 상태 목록 과 함께 벡터 기반 드로어 블을 사용하는 경우 버튼을 다음과 같이 리팩터링 할 수 있습니다.

참고 : 벡터 드로어 블은 이미지보다 훨씬 작기 때문에 추가 명시 적 정의는 많은 오버 헤드를 발생시키지 않으며 명확하고 명시적인 코드를 만듭니다. 하나에 변형을 갖는 것보다 몇 개의 파일을 업데이트하는 오버 헤드) :

참고 : Android Studio는 벡터 자산을위한 훌륭한 소스입니다.

res \ values ​​\ styles.xml

<!--ImageView-->
<style name="Details_Buttons_Top_Left_Button">
  <item name="android:layout_width">match_parent</item>
  <item name="android:layout_height">match_parent</item>
  <item name="android:tint">@color/button_csl</item>
</style>

res \ color \ button_csl.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:color="@color/grey_disabled"/>
  <item android:state_pressed="true" android:color="@color/orange_hilite"/>
  <item android:color="@color/black"/>
</selector>

details_menu_large_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true"
        android:drawable="@drawable/and_card_details_button_down_left_onclick" /> <!-- pressed -->
  <item android:drawable="@drawable/and_card_details_button_down_left" /> <!-- default -->
</selector>

Details_Buttons_Top_Left_Button

<ImageView android:id="@+id/Details_Buttons_Top_Left_Button"
           style="@style/Details_Buttons_Top_Left_Button"
           android:src="@drawable/details_menu_large_button" />

and_card_details_button_down_left.xml (ic_play_arrow_black_24dp.xml)

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
  <path
        android:fillColor="#FF000000"
        android:pathData="M8,5v14l11,-7z"/>

</vector>

and_card_details_button_down_left_onclick.xml (ic_play_arrow_black_24dp.xml 수정 됨)

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
  <group android:name="rotationGroup"
         android:pivotX="12"
         android:pivotY="12"
         android:rotation="90" >
    <path
          android:fillColor="#FF000000"
          android:pathData="M8,5v14l11,-7z"/>
  </group>
</vector>


답변

당신이 원하는 경우 rotation에 당김 xml파일 다음 간단한 추가 android:rotation="180"ImageView

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_dropdown"
    android:rotation="180"/>


답변