android shape xml旋转可绘制可编程更改颜色
这是三角形形状的xml:android shape xml旋转可绘制可编程更改颜色,android,xml,layer-list,layerdrawable,Android,Xml,Layer List,Layerdrawable,这是三角形形状的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:piv
<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>
我该怎么做?谢谢您的帮助。忘掉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);
这有点棘手,涉及到很多类型转换:
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( ... );
如果您在res/drawable中的xml文件中有RotateDrawable,那么您可以尝试下面的代码来更改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);
`谢谢您的回答,但这是一个矩形。我需要一个三角形。感谢@Taig对我的宝贵帮助。这个东西工作得很好,很棒,我可以在我的项目中使用它
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( ... );
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);