public class TriangleView extends View {
private Paint mPaint;
private Context context;
public TriangleView(Context context) {
super(context);
this.context = context;
init();
}
public TriangleView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public TriangleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
private void init(){
mPaint = new Paint();
mPaint.setAntiAlias(true); //抗锯齿
mPaint.setColor(getResources().getColor(R.color.black));//画笔颜色
mPaint.setStyle(Paint.Style.FILL); //画笔风格
mPaint.setTextSize(36); //绘制文字大小,单位px
mPaint.setStrokeWidth(5); //画笔粗细
}
//重写该方法,在这里绘图
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
WindowManager wManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wManager.getDefaultDisplay().getMetrics(dm);
canvas.drawColor(Color.parseColor("#FFEB3B")); //设置画布颜色
Path path = new Path();
path.moveTo(dm.widthPixels/2-20, 60);
path.lineTo(dm.widthPixels/2+20, 60);
path.lineTo(dm.widthPixels/2,20);
path.close(); //关闭当前path轮廓
canvas.drawPath(path,mPaint); //绘制path
}
}
07-15
1360
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交