我是靠谱客的博主 合适春天,最近开发中收集的这篇文章主要介绍Android 使用自定义Drawable 设置圆角矩形或者圆形图片,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

转自  Android Drawable 那些不为人知的高效用法

本文出自:【张鸿洋的博客】 http://blog.csdn.net/lmj623565791/article/details/43752383

 

调用方式:

imageView.setImageDrawable(new RoundImageDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.login))); 

RoundImageDrawable.java

package de.bvb.rxdemo.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;

/**
 * 设置圆形图片,调用方式为为:
 * imageView.setImageDrawable(new RoundImageDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.login)));
 */
public class RoundImageDrawable extends Drawable {
    private Bitmap bitmap;
    private Paint paint;
    private RectF rectF;

    public RoundImageDrawable(Bitmap bitmap) {
        this.bitmap = bitmap;
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    }

    @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 draw(Canvas canvas) {
        canvas.drawRoundRect(rectF, 40, 40, paint);
    }

    @Override
    public int getIntrinsicHeight() {
        return bitmap.getHeight();
    }

    @Override
    public int getIntrinsicWidth() {
        return bitmap.getWidth();
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

CircleImageDrawable.java

package de.bvb.rxdemo.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;

/**
 *  设置圆形图片,调用方式为为:
 *  imageView.setImageDrawable(new CircleImageDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.login)));
 */
public class CircleImageDrawable extends Drawable {

    private Bitmap bitmap;
    private Paint paint;
    private int radius;

    public CircleImageDrawable(Bitmap bitmap) {
        this.bitmap = bitmap;
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
        radius = Math.min(bitmap.getWidth(), bitmap.getHeight()) / 2;
    }

    @Override
    public int getIntrinsicWidth() {
        return radius * 2;
    }

    @Override
    public int getIntrinsicHeight() {
        return radius * 2;
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawCircle(radius, radius, radius, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

 

转载于:https://www.cnblogs.com/Westfalen/p/7054279.html

最后

以上就是合适春天为你收集整理的Android 使用自定义Drawable 设置圆角矩形或者圆形图片的全部内容,希望文章能够帮你解决Android 使用自定义Drawable 设置圆角矩形或者圆形图片所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部