2014-02-24 5 views
2

당김 변경 색상으로 회전 :안드로이드 형태의 XML은이 삼각형 모양의 XML입니다 프로그래밍

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:id="@+id/shape_id"> 
     <rotate 
      android:fromDegrees="45" 
      android:toDegrees="45" 
      android:pivotX="-40%" 
      android:pivotY="87%" > 
      <shape android:shape="rectangle" > 
       <stroke android:width="10dp"/> 
      </shape> 
     </rotate> 
    </item> 
</layer-list> 

을 그리고 이것은 텍스트 뷰

<TextView 
    android:id="@+id/headlineSelect_TXT2" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_weight="1" 
    android:background="@drawable/category_triangle_shape1" 
    android:visibility="invisible" /> 

의 배경입니다 그리고 프로그래밍 모양의 색상을 변경하려면 . 나는 이것을 시도했지만 널 포인터 예외가 발생했다.

LayerDrawable bgDrawable = (LayerDrawable) getActivity() 
    .getResources() 
    .getDrawable(R.drawable.category_triangle_shape1); 
final GradientDrawable shape = (GradientDrawable) bgDrawable 
    .findDrawableByLayerId(R.id.shape_id); 
shape.setStroke(10,Color.GREEN); 

어떻게 할 수 있습니까? 도와 주셔서 감사합니다.

답변

0

XML 잊고 그런 식으로 코드를 수행

TextView txtView = (TextView)findViewById(R.id. headlineSelect_TXT2); 
    ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() { 
     @Override 
     public Shader resize(int width, int height) { 
      LinearGradient lg = new LinearGradient(0, 0, 0, txtView.getHeight(), 
       new int[] { 
        Color.LIGHT_GREEN, 
        Color.WHITE, 
        Color.MID_GREEN, 
        Color.DARK_GREEN }, 
       new float[] { 
        0, 0.45f, 0.55f, 1 }, 
       Shader.TileMode.REPEAT); 
      return lg; 
     } 
    }; 
    PaintDrawable p = new PaintDrawable(); 
    p.setShape(new RectShape()); 
    p.setShaderFactory(sf); 
    txtView.setBackgroundDrawable((Drawable)p); 
+0

감사 답변을하지만이 사각형 모양입니다. 삼각형이 필요해. – skhizein

11

이 다소 까다 롭습니다 및 캐스트를 많이 포함 :

TextView view = (TextView) findViewById(R.id.my_text_view); 

// Get the drawable from the view, if you're using an imageview src 
// element you'll go with view.getDrawable() 
LayerDrawable layers = (LayerDrawable) view.getBackground(); 

// Now get your shape by selecting the id 
RotateDrawable rotate = (RotateDrawable) layers.findDrawableByLayerId(R.id.shape_id); 

// Finally you can access the underlying shape 
GradientDrawable drawable = (GradientDrawable) rotate.getDrawable(); 

// ... and do you fancy things 
drawable.setColor(...); 
+3

감사! 정말 도움이되는 조언. 그것은 완벽하게 작동합니다. – saturov

+0

당신의 소중한 도움을 주신 덕분에 @Taig이 일은 잘 작동하고 내 프로젝트에서 사용할 수 있습니다. https://github.com/harsh159357/InstaTag/commit/ded64bd112adb71aca0c1f7eb0aca0c1f7eb07a2d339c8059a –

0

당신은 입술 내부의 XML 파일에 RotateDrawable이있는 경우/drawable 그러면 drawable 회전 색상을 변경하기 위해 아래 코드를 시도 할 수 있습니다.

LayerDrawable layers = (LayerDrawable) getResources().getDrawable(R.drawable.triangle); 
RotateDrawable rotateShape = (RotateDrawable) (layers.findDrawableByLayerId(R.id.user_color)); 
GradientDrawable shape = (GradientDrawable) rotateShape.getDrawable(); 
shape.setColor(Color.parseColor("#FF0000")); 
TextView NotchTV = (TextView) view.findViewById(R.id.bubble_notch); 
NotchTV.setBackgroundDrawable(layers); 

`