2017-11-23 2 views
0

Button 클래스를 재정의하여 텍스트를 거꾸로 그릴 수 있습니까? 다른 라이브러리의 포맷은 버튼 자체가 회전 할 때 사용합니다.하지만 텍스트를 뒤집어서 사용해야합니다.텍스트를 거꾸로 그릴 재정의 버튼

나는 onDraw 메서드에 익숙하지 않으며 어떻게이 서브 클래스를 만들 수 있는지 잘 모르겠습니다.

도움 주시면 감사하겠습니다.

<Button 
    android:id="@+id/test_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="text button" 
    android:rotation="180"/> 

또는 사용자 정의 버튼을 사용하여 선호하는 경우, 당신은이 같은 것을 만들 수 있습니다 :

답변

0

이처럼 버튼 뭔가 회전 속성을 사용할 수 있습니다 당신을

public class FlippedTextButton extends Button { 
    public FlippedTextButton(Context context) { 
     super(context); 
    } 

    public FlippedTextButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public FlippedTextButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.save(); 

     float y = this.getHeight()/2.0f; 
     float x = this.getWidth()/2.0f; 
     canvas.rotate(180, x, y); 

     super.onDraw(canvas); 
     canvas.restore(); 
    } 
} 

을 다음 그것을 다음과 함께 사용할 수 있습니다 :

<!-- Change to your class package name. --> 
<com.example.flippedtext.FlippedTextButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Flipped text"/> 

다음은 github의 샘플 프로젝트입니다 : https://github.com/isnotmenow/FlippedText

+0

정말 고마워요, 그건 ** 정확히 ** 필요한 것입니다! 너 락! – DXHHH101

+0

도움이 되니 기쁩니다! ;) –

관련 문제