天天看点

Android 中动态加载shape动态加载shape

Android 中动态加载shape

  • 动态加载shape
    • 类继承关系
      • RoundRectShape
      • ArcShape 绘制圆形或者扇形
      • OvalShape 椭圆

动态加载shape

今天遇到一个动态加载shape的需求,解决后分享给大家.这篇文章参考了

https://www.jianshu.com/p/8b8872b6bc21

类继承关系

Android 中动态加载shape动态加载shape

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);
           
Android 中动态加载shape动态加载shape

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);
           
Android 中动态加载shape动态加载shape