import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapShader;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.Shader;import android.graphics.drawable.BitmapDrawable;import android.util.AttributeSet;import android.widget.ImageView;import androidx.annotation.Nullable;/** * Created by DengDongQi on 2019/9/3 * 解决ImageView  setRotationX....等旋转时出现锯齿问题 * * 原理 : 某大佬研究出锯齿规律会出现为ImageView的边界 1-2 px处,解决办法是: * 重写onDraw且不走super方法,自己缩小ImageView内容1-2 px并居中绘制 * * 出处: (FQ)https://medium.com/@elye.project/smoothen-jagged-edges-of-rotated-image-view-1e56f6d8b5e9 */public class RotationImageView extends ImageView {    public RotationImageView(Context context) {        super(context);    }    public RotationImageView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    public RotationImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onDraw(Canvas canvas) {        if (getDrawable() instanceof BitmapDrawable) {            //super.onDraw(canvas); 不走super            Paint paint = new Paint();            paint.setAntiAlias(true);            paint.setDither(true);            Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap();            BitmapShader shader = new BitmapShader(bitmap,                    Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);            Matrix matrix = new Matrix();            float scale;            /* Note: this piece of code handling like Centre-Crop scaling */            if (bitmap.getWidth() > bitmap.getHeight()) {                scale = (float) canvas.getHeight() / (float) bitmap.getHeight();                matrix.setScale(scale, scale);                matrix.postTranslate((canvas.getWidth() - bitmap.getWidth() * scale) * 0.5f, 0);            } else {                scale = (float) canvas.getWidth() / (float) bitmap.getWidth();                matrix.setScale(scale, scale);                matrix.postTranslate(0, (canvas.getHeight() - bitmap.getHeight() * scale) * 0.5f);            }            shader.setLocalMatrix(matrix);            paint.setShader(shader);            /* this is where I shrink the image by 1px each side,                move it to the center */            canvas.translate(2, 2);            canvas.drawRect(                    0.0f, 0.0f, canvas.getWidth() - 4, canvas.getHeight() - 4, paint);        }    }}

 

更多相关文章

  1. 别人花了6000元学的Android高级技术,全都在这里
  2. 别人花了几万元学的Android高级技术,我帮你们免费弄来了!
  3. Android开发(十一)——ImageView的尺寸设置scaleType
  4. Android(安卓)图片的浏览、缩放、拖动和自动居中
  5. Android中改变Dialog背景透明度
  6. RelativeLayout布局,以及部分常用属性介绍scaleType/gravity/layo
  7. RelativeLayout相对布局方法总结
  8. RelativeLayout相对布局中的属性
  9. ImageView的scaletype属性

随机推荐

  1. Jquery 传json格式数据到后台,后台反序列
  2. 在asp.net中使用JavaScript的下拉菜单[复
  3. jqueryui autocomplete 插件 点击 显示选
  4. ajax回调json数组对象,jquery中$.each()循
  5. 获取背景图像的最终大小
  6. 推荐一款好用的jquery弹出层插件——wbox
  7. 更改粘性标题航点或偏移的位置。
  8. jquery 如何获取json指定键为指定值的另
  9. 动态生成 ul 的li jquery 点击事件无法获
  10. how to style text after ?