欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义Drawable实现圆形和圆角

程序员文章站 2023-12-14 23:31:16
本文实例为大家分享了自定义drawable实现圆形和圆角的具体代码,供大家参考,具体内容如下 圆形 package com.customview.widget;...

本文实例为大家分享了自定义drawable实现圆形和圆角的具体代码,供大家参考,具体内容如下

圆形

package com.customview.widget;

import android.graphics.bitmap;
import android.graphics.bitmapshader;
import android.graphics.canvas;
import android.graphics.colorfilter;
import android.graphics.paint;
import android.graphics.pixelformat;
import android.graphics.shader;
import android.graphics.drawable.drawable;
import android.support.annotation.intrange;
import android.support.annotation.nonnull;
import android.support.annotation.nullable;

/**
 * 自定义drawable 实现圆形图片
 * created by hust_twj on 2017/9/26.
 */

public class circledrawable extends drawable {
  private paint mpaint;
  private int mwidth; //宽/高,直径
  private bitmap mbitmap;

  public circledrawable(bitmap bitmap) {
    this.mbitmap = bitmap;
    bitmapshader bitmapshader = new bitmapshader(mbitmap, shader.tilemode.clamp, shader.tilemode.clamp);//着色器 水平和竖直都需要填充满
    mpaint = new paint();
    mpaint.setantialias(true);
    mpaint.setshader(bitmapshader);
    mwidth = math.min(mbitmap.getwidth(), mbitmap.getheight()); //宽高的最小值作为直径
  }

  @override
  public void draw(@nonnull canvas canvas) {
    canvas.drawcircle(mwidth/2, mwidth/2, mwidth/2, mpaint); //绘制圆形
  }

  @override
  public void setalpha(@intrange(from = 0, to = 255) int i) {
    mpaint.setalpha(i);
  }

  @override
  public void setcolorfilter(@nullable colorfilter colorfilter) {
    mpaint.setcolorfilter(colorfilter);
  }

  @override
  public int getopacity() {
    return pixelformat.translucent; //设置系统默认,让drawable支持和窗口一样的透明度
  }

  //还需要从重写以下2个方法,返回drawable实际宽高
  @override
  public int getintrinsicwidth() {
    return mwidth;
  }

  @override
  public int getintrinsicheight() {
    return mwidth;
  }


}

圆角:

package com.customview.widget;

import android.graphics.bitmap;
import android.graphics.bitmapshader;
import android.graphics.canvas;
import android.graphics.colorfilter;
import android.graphics.paint;
import android.graphics.pixelformat;
import android.graphics.rectf;
import android.graphics.shader;
import android.graphics.drawable.drawable;
import android.support.annotation.intrange;
import android.support.annotation.nonnull;
import android.support.annotation.nullable;

/**
 * 自定义drawable 实现圆角图片
 * created by hust_twj on 2017/9/26.
 */

public class roundcornerdrawable extends drawable {
  private paint mpaint;
  private rectf rectf;//定义矩形区域
  private bitmap mbitmap;

  public roundcornerdrawable(bitmap bitmap) {
    this.mbitmap = bitmap;
    bitmapshader bitmapshader = new bitmapshader(mbitmap, shader.tilemode.clamp, shader.tilemode.clamp);//着色器 水平和竖直都需要填充满
    mpaint = new paint();
    mpaint.setantialias(true);
    mpaint.setshader(bitmapshader);
    //rectf = new rectf(0, 0, bitmap.getwidth(), bitmap.getheight());
  }

  @override
  public void draw(@nonnull canvas canvas) {
    //rectf:圆角矩形区域
    canvas.drawroundrect(new rectf(0, 0, mbitmap.getwidth(), mbitmap.getheight()),100, 100, mpaint);//rx:x方向上的圆角半径; ry:y方向上的圆角半径。
  }

/*  @override
  public void setbounds(int left, int top, int right, int bottom) {
    super.setbounds(left, top, right, bottom);
    rectf = new rectf(left, top, right, bottom);
  }*/

  @override
  public void setalpha(@intrange(from = 0, to = 255) int i) {
    mpaint.setalpha(i);
  }

  @override
  public void setcolorfilter(@nullable colorfilter colorfilter) {
    mpaint.setcolorfilter(colorfilter);
  }

  @override
  public int getopacity() {
    return pixelformat.translucent; //设置系统默认,让drawable支持和窗口一样的透明度
  }

  //还需要从重写以下2个方法,返回drawable实际宽高
  @override
  public int getintrinsicwidth() {
    return mbitmap.getwidth();
  }

  @override
  public int getintrinsicheight() {
    return mbitmap.getheight();
  }

}

mainactivity中:

bitmap bitmap = bitmapfactory.decoderesource(getresources(), r.drawable.xxx);
imageview.setimagedrawable(new roundcornerdrawable(bitmap));

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: