Android 中動态加載shape
- 動态加載shape
-
- 類繼承關系
-
- RoundRectShape
- ArcShape 繪制圓形或者扇形
- OvalShape 橢圓
動态加載shape
今天遇到一個動态加載shape的需求,解決後分享給大家.這篇文章參考了
https://www.jianshu.com/p/8b8872b6bc21
類繼承關系
RoundRectShape
/**
外部矩形
* 八個int 代表四個角的橢圓弧度
* 分别是 左上 右上 右下 左下 弧度,弧度由兩個參數,表示橢圓的兩個半徑
* 這裡的示例是底部左右有圓角,頂部左右沒有圓角
*/
float[] outerRadii = {0,0,0,0,corners,corners,corners,corners};
//内矩形距外矩形,左上角x,y距離, 右下角x,y距離
RectF inset = new RectF(100, 100, 100, 100);
//内矩形 圓角半徑 八個參數,跟外矩形參數含義一樣
float[] innerRadii = {20, 20, 20, 20, 20, 20, 20, 20};
/**
* 這裡有三個參數,外矩形,内矩形距離外矩形的位置,内矩形
* 如果你隻想要一個矩形,那後兩個參數為null就可以了
*/
RoundRectShape roundRectShape = new RoundRectShape(outerRadii,inset,innerRadii);
ShapeDrawable drawable = new ShapeDrawable(roundRectShape);
//指定填充顔色
drawable.getPaint().setColor(ResourceUtils.getColor(R.color.fa_black_25));
// 指定填充模式
drawable.getPaint().setStyle(Paint.Style.FILL);
textView.setBackground(drawable);
ArcShape 繪制圓形或者扇形
//
/**
* ArcShape(float startAngle, float sweepAngle) 有兩個參數,起始弧度,需要跨越的弧度,
* 上面的例子中 寫的是負數,則逆時針畫弧,如果是正數,則順時針畫弧.
* 如果是360度,則是一個圈,圓的半徑即大小你的ImageView本身來決定。
*/
ArcShape shape = new ArcShape(0, -300);
ShapeDrawable drawable = new ShapeDrawable(shape);
drawable.getPaint().setColor(Color.BLACK);
drawable.getPaint().setAntiAlias(true);
drawable.getPaint().setStyle(Paint.Style.STROKE);
mImage.setBackground(drawable);
OvalShape 橢圓
/**
* 畫一個橢圓,同樣橢圓的寬高由你的載體來決定,我這裡是ImageView,
* 需要注意的是如果ImageView的寬和高相等就是一個圓,
*/
OvalShape ovalShape = new OvalShape();
ShapeDrawable drawable = new ShapeDrawable(ovalShape);
drawable.getPaint().setColor(Color.BLACK);
drawable.getPaint().setAntiAlias(true);
drawable.getPaint().setStyle(Paint.Style.STROKE);
mImage.setBackground(drawable);