欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2020-10-07 14:27:53|栏目:Android代码|点击:

本文实例为大家分享了自定义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));

上一篇:Android中Activity之间跳转和参数传递的实例

栏    目:Android代码

下一篇:条件数据库Android:sqllite的简单使用

本文标题:Android自定义Drawable实现圆形和圆角

本文地址:http://www.codeinn.net/misctech/8131.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有