android绘图Paint.setAntiAlias()和Paint.setDither()方法的作用

本文深入解析了自定义View中Paint.setAntiAlias()与Paint.setDither()两个方法的功能。前者用于实现抗锯齿效果,使图形边界更平滑;后者则用于防抖动,让图像呈现更加细腻柔和。通过对比图示,直观展示了两种效果的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在自定义View中,这两个Paint.setAntiAlias()和Paint.setDither()方法用的很多,都只有一个boolean值,作用大家未必清楚,今天抽了点时间研究下,终于搞清楚了,希望给大家点帮助!

Paint.setAntiAlias()

该方法作用是抗锯齿,什么意思呢,我们看下效果图,就知道了
Paint.setAntiAlias()的作用

左边是没有这只抗锯齿的,右边是设置了抗锯齿的,边界明显变模糊了。

Paint.setDither()

该方法是设置防抖动。 
我们先看下没有设置防抖动的绘制出来的图 

Paint.setDither()方法的作用

然后我们看下设置了防抖动的绘制出来的图 

Paint.setDither()方法的作用

第二个是不是比第一个图柔和点,这就是防抖动的效果。 
大家是不是对这两个方法的作用一目了然了!!!

 

原文链接:https://blog.youkuaiyun.com/lovexieyuan520/article/details/50732023

 

 

 

import android.content.Context; import android.graphics.*; import android.util.AttributeSet; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import androidx.appcompat.app.AlertDialog; import java.util.ArrayList; import java.util.List; import java.util.Random; public class DrawingView extends View { private List<Shape> shapes = new ArrayList<>(); private Shape selectedShape; private Paint paint; private GestureDetector gestureDetector; private static final int[] COLORS = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.CYAN}; // 构造函数 public DrawingView(Context context) { super(context); init(); } public DrawingView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL); gestureDetector = new GestureDetector(getContext(), new GestureListener()); } // 形状基类 private abstract static class Shape { int color; RectF bounds; abstract boolean contains(float x, float y); } // 具体形状实现 private static class Circle extends Shape { @Override boolean contains(float x, float y) { float cx = bounds.centerX(), cy = bounds.centerY(); return Math.hypot(x - cx, y - cy) <= bounds.width() / 2; } } private static class Rect extends Shape { @Override boolean contains(float x, float y) { return bounds.contains(x, y); } } private static class Triangle extends Shape { @Override boolean contains(float x, float y) { Path path = new Path(); path.moveTo(bounds.left, bounds.bottom); path.lineTo(bounds.centerX(), bounds.top); path.lineTo(bounds.right, bounds.bottom); path.close(); Region region = new Region();
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值