zoukankan      html  css  js  c++  java
  • 图片圆角处理

    package com.storm.ui;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.PorterDuff.Mode;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.RectF;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    
    /**
     * 圆角图片
     * 
     * @author Chao
     * */
    
    public class RoundedCornerImageView extends ImageView {
    
            private final float density = getContext().getResources()
                            .getDisplayMetrics().density;
            private float roundness;
    
            public RoundedCornerImageView(Context context) {
                    super(context);
    
                    init();
            }
    
            public RoundedCornerImageView(Context context, AttributeSet attrs) {
                    super(context, attrs);
    
                    init();
            }
    
            public RoundedCornerImageView(Context context, AttributeSet attrs,
                            int defStyle) {
                    super(context, attrs, defStyle);
    
                    init();
            }
    
            @Override
            public void draw(Canvas canvas) {
                    final Bitmap composedBitmap;
                    final Bitmap originalBitmap;
                    final Canvas composedCanvas;
                    final Canvas originalCanvas;
                    final Paint paint;
                    final int height;
                    final int width;
    
                    width = getWidth();
    
                    height = getHeight();
    
                    composedBitmap = Bitmap.createBitmap(width, height,
                                    Bitmap.Config.ARGB_8888);
                    originalBitmap = Bitmap.createBitmap(width, height,
                                    Bitmap.Config.ARGB_8888);
    
                    composedCanvas = new Canvas(composedBitmap);
                    originalCanvas = new Canvas(originalBitmap);
    
                    paint = new Paint();
                    paint.setAntiAlias(true);
                    paint.setColor(Color.BLACK);
    
                    super.draw(originalCanvas);
    
                    composedCanvas.drawARGB(0, 0, 0, 0);
    
                    composedCanvas.drawRoundRect(new RectF(0, 0, width, height),
                                    this.roundness, this.roundness, paint);
    
                    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    
                    composedCanvas.drawBitmap(originalBitmap, 0, 0, paint);
    
                    canvas.drawBitmap(composedBitmap, 0, 0, new Paint());
            }
    
            public float getRoundness() {
                    return this.roundness / this.density;
            }
            
            public void setRoundness(float roundness) {
                    this.roundness = roundness * this.density;
            }
            private void init() {
                    setRoundness(5);
            }
    }
  • 相关阅读:
    通过 Ansible role 安装 Jenkins Server
    常见 Bash 内置变量介绍
    Ansible 简介
    为容器化的 Go 程序搭建 CI
    Bash Shebang 小结
    Docker Compose 引用环境变量
    Docker Compose 之进阶篇
    Docker Compose 原理
    WEB程序调用客户端程序
    读书笔记2014第5本:《乔纳森传》
  • 原文地址:https://www.cnblogs.com/yangcong/p/3747985.html
Copyright © 2011-2022 走看看