android Paint 简单使用

工作上不忙,于是在学习一下知识点 以作储备

今天学习了一下 paint的用法不多说上api

ARGB解释

//        mPaint.setColor(Color.RED);//设置画笔颜色
//        mPaint.setARGB(255, 138, 43, 226); // 设置 Paint对象颜色 0-255 参数1 透明度 255完全不透明
        mPaint.setAntiAlias(true);//是否抗锯齿
//        mPaint.setAlpha(127);//设置透明度
        mPaint.setStyle(Paint.Style.FILL);//FILL设置只绘制图形内容  STROKE设置只绘制图形的边 FILL_AND_STROKE设置都绘制
//        mPaint.setStrokeWidth(4);//设置描边宽度
//        mPaint.setStrokeCap(Paint.Cap.ROUND);//圆角效果
//        mPaint.setStrokeJoin(Paint.Join.BEVEL);//拐角风格

//        mPaint.setShader(new SweepGradient(250, 250, Color.BLUE, Color.RED)); //设置环形渲染器
//        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN)); //设置图层混合模式
//        mPaint.setColorFilter(new LightingColorFilter(0x00ffff, 0x000000)); //设置颜色过滤器
//        mPaint.setFilterBitmap(true); //设置双线性过滤
//        mPaint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL));//设置画笔遮罩滤镜 ,传入度数和样式
//        mPaint.setTextScaleX(2);// 设置文本缩放倍数
//        mPaint.setTextSize(38);// 设置字体大小
//        mPaint.setTextAlign(Paint.Align.LEFT);//对其方式
//        mPaint.setUnderlineText(true);// 设置下划线
//
//        String str = "Android小菜鸡";
//        Rect rect = new Rect();
//        mPaint.getTextBounds(str, 0, str.length(), rect); //测量文本大小,将文本大小信息存放在rect中
//        mPaint.measureText(str); //获取文本的宽
//        mPaint.getFontMetrics(); //获取字体度量对象

这些都是paint的一些基本使用 点进去paint类去看源码 发现里面会有好多 native方法,直接调用底层去操作的。

说一下新学习的几个api

Shader各个用法

各种颜色连接

线性渲染

上个图

上代码

  /**
         * 1.线性渲染,LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int colors[], @Nullable float positions[], @NonNull TileMode tile)
         * (x0,y0):渐变起始点坐标
         * (x1,y1):渐变结束点坐标
         * color0:渐变开始点颜色,16进制的颜色表示,必须要带有透明度
         * color1:渐变结束颜色
         * colors:渐变数组
         * positions:位置数组,position的取值范围[0,1],作用是指定某个位置的颜色值,如果传null,渐变就线性变化。
         * tile:用于指定控件区域大于指定的渐变区域时,空白区域的颜色填充方法
         */
//        mShader = new LinearGradient(0, 0, 500, 500, new int[]{Color.RED, Color.BLUE, Color.WHITE}, new float[]{0.f, 0.5f, 1}, Shader.TileMode.REPEAT);
圆环渲染

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中的 Paint 类可以用来绘制各种形状和线条。下面是一个简单的示例,演示如何使用 Paint 类在 Android 中绘制直线。 首先,在 XML 布局文件中添加一个 View: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:id="@+id/drawing_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> ``` 然后,在 Activity 中获取该 View 并绘制直线: ```java public class MainActivity extends AppCompatActivity { private View drawingView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawingView = findViewById(R.id.drawing_view); drawingView.setBackground(new CustomView(this)); } private class CustomView extends View { private Paint paint; public CustomView(Context context) { super(context); init(); } private void init() { paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(5f); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int startX = 0; int startY = 0; int stopX = getWidth(); int stopY = getHeight(); canvas.drawLine(startX, startY, stopX, stopY, paint); } } } ``` 在这个示例中,我们创建了一个 CustomView 类,继承自 View 类,并在该类中实现了 onDraw() 方法。onDraw() 方法用于绘制 View 上的内容,我们在这里使用 Canvas 类的 drawLine() 方法绘制了一条直线。 Paint 类的实例被创建在 CustomView 类的构造函数中,我们使用该实例设置了直线的颜色和线宽。如果需要绘制其他形状,也可以使用 Paint 类的其他方法来设置笔刷、阴影、渐变等效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值