【问题标题】:android shape xml rotated drawable change color programmaticallyandroid shape xml以编程方式旋转可绘制更改颜色
【发布时间】:2014-03-26 22:17:47
【问题描述】:

这是一个三角形的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);

我该怎么做?感谢您的帮助。

【问题讨论】:

    标签: android xml layer-list layerdrawable


    【解决方案1】:

    这有点棘手,涉及很多演员:

    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( ... );
    

    【讨论】:

    【解决方案2】:

    如果您在 res/drawable 内的 xml 文件中有 RotateDrawable,那么您可以尝试以下代码来更改旋转可绘制对象的颜色。

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

    `

    【讨论】:

      【解决方案3】:

      忘记 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);
      

      【讨论】:

      • 感谢您的回答,但这是一个矩形。我需要一个三角形。
      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      相关资源
      最近更新 更多