圆形头像实现

ganshenml 2016-03-28 11:45:29

RectF rectF = new RectF(400, 400, 800, 800);
canvas.drawBitmap(backgroundBm, null, rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//paint要在canvas.draw前面 才有效果

canvas.drawRoundRect(rectF, 200, 200, paint);





为什么我画个圆形头像都这么。。。痛苦
...全文
535 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
头发还没秃a 2016-04-05
  • 打赏
  • 举报
回复
引用 17 楼 zhumj_zhumj 的回复:
[quote=引用 3 楼 ganshenml 的回复:] [quote=引用 1 楼 zhumj_zhumj 的回复:] 实现圆形外剪切
具体到我上面的代码缺省到了哪步?因为我自身感觉好像应该是这么做的就会有效果。[/quote] 自定义一个圆形ImageView控件吧。。。。http://download.csdn.net/detail/nuptboyzhb/7284553[/quote] 你如果非要自己画的话,你需要定义两层画布,一层画布用来把原图片画到Bitmap上面,一层画布画个圆形Bitmap,然后Paint设置setXfermode(),注意setXfermode这个属性很重要:

private Xfermode sXfermode1 = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);//裁剪重合区域外面部分
private Xfermode sXfermode2 = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);//裁剪重合区域
头发还没秃a 2016-04-05
  • 打赏
  • 举报
回复
引用 3 楼 ganshenml 的回复:
[quote=引用 1 楼 zhumj_zhumj 的回复:] 实现圆形外剪切
具体到我上面的代码缺省到了哪步?因为我自身感觉好像应该是这么做的就会有效果。[/quote] 自定义一个圆形ImageView控件吧。。。。http://download.csdn.net/detail/nuptboyzhb/7284553
ganshenml 2016-04-01
  • 打赏
  • 举报
回复
引用 12 楼 xingzhong128 的回复:
Bitmap photo = BitmapFactory.decodeResource(getResources(), R.drawable.pic); Bitmap dst = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888); // 定义dst图片 Bitmap src = dst.copy(Bitmap.Config.ARGB_8888, true); // 定义src图片 Bitmap b3 = dst.copy(Bitmap.Config.ARGB_8888, true); // 定义合成结果图片 Canvas canvas1 = new Canvas(dst); Canvas canvas2 = new Canvas(src); Canvas canvas3 = new Canvas(b3); Paint paint1 = new Paint(); Rect rect1 = new Rect(0, 0, photo.getWidth(), photo.getHeight()); Rect rect2 = new Rect(0, 0, 300, 300); canvas1.drawBitmap(photo, rect1, rect2, paint1); // 首先在dst图片上画上头像 Paint paint2 = new Paint(); paint2.setStrokeWidth(2); paint2.setStyle(Paint.Style.FILL); paint2.setColor(Color.RED); canvas2.drawCircle(150, 150, 150, paint2); // 在src图片上画上一个填充的圆 Paint paint = new Paint(); int layer = canvas3.saveLayer(0, 0, 300, 300, paint, Canvas.ALL_SAVE_FLAG); // 定义一个图层,同时也是定义了PorterDuff效果的Dst区域,如果没有用图层相当于没有定义Dst区域,那么PorterDuff效果就展现的有问题 canvas3.drawBitmap(dst, 0, 0, paint); // 绘制dst图片 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); canvas3.drawBitmap(src, 0, 0, paint); // 绘制圆src图片 canvas3.restoreToCount(layer); //恢复图层到画布上 paint.setXfermode(null); // 重置画笔 canvas.drawBitmap(b3, 0, 0, paint); PorterDuff效果需要两幅Bitmap图片才能正确展示,lz可能是对这个效果的内涵没有理解清楚    canvas.drawRoundRect(rectF, 200, 200, paint); 这个方法画出的表面上是圆,实际上它是一个矩形,圆外面的地方还是属于长方形的。
感谢,我懂你的意思。我自己也实现了效果。有点疑问的是下面这个: 以下是自定义View:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CircleView extends ImageView {
    Bitmap bitmap;
    Context context;

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleView(Context context, Bitmap bitmap) {
        super(context);
        this.context = context;
        this.bitmap = bitmap;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);

        RectF rectF = new RectF(0, 0, 200, 200);
        canvas.drawRoundRect(rectF, 100, 100, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, null, rectF, paint);

    }
}
然后是Activity中setContentView:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo_person);
CircleView circleView = new CircleView(this, bitmap);
setContentView(circleView);
出来的效果是正方形的图片,所以这点是有些疑问
乐逍遥二 2016-04-01
  • 打赏
  • 举报
回复
楼主你好,我是那个小女孩,我要收版权。。。
ganshenml 2016-03-31
  • 打赏
  • 举报
回复
引用 10 楼 assky124 的回复:

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 圆形图片
 * Created by Coder.Yan on 2015/11/10.
 */
public class CircleImageView extends ImageView {

    private Paint paint = null;
    private float cx = 0;
    private float cy = 0;
    private float radius = 0;
    private Path clipPath = null;

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        clipPath = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
        canvas.restore();
        paint.setColor(0xffffffff);
        canvas.drawCircle(cx,cy,radius,paint);
        paint.setColor(0xff808080);
        canvas.drawCircle(cx,cy,radius-1,paint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int w = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        int h = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
        int r = Math.min(w,h)/2;
        cx = getPaddingLeft() + r;
        cy = getPaddingTop() + r;
        radius = r;
        clipPath.reset();
        clipPath.addCircle(cx,cy,r, Path.Direction.CW);
    }
}
我写的圆形图片控件,参考下
谢谢!
怀君 2016-03-31
  • 打赏
  • 举报
回复
http://download.csdn.net/detail/u013290250/9425955 直接用。
xingzhong128 2016-03-31
  • 打赏
  • 举报
回复
如果想实现的简单点可以用clippath,这样就只会在path指定的位置绘图,所以为了不影响后面的绘图就需要对canvas保存和恢复 canvas.save(); Path path = new Path(); path.addCircle(300, 300, 200, Path.Direction.CCW); canvas.clipPath(path); RectF rectF = new RectF(100, 100, 500, 500); canvas.drawBitmap(photo, null, rectF, paint); canvas.restore();
xingzhong128 2016-03-31
  • 打赏
  • 举报
回复
Bitmap photo = BitmapFactory.decodeResource(getResources(), R.drawable.pic);

Bitmap dst = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888); // 定义dst图片
Bitmap src = dst.copy(Bitmap.Config.ARGB_8888, true); // 定义src图片
Bitmap b3 = dst.copy(Bitmap.Config.ARGB_8888, true); // 定义合成结果图片

Canvas canvas1 = new Canvas(dst);
Canvas canvas2 = new Canvas(src);
Canvas canvas3 = new Canvas(b3);

Paint paint1 = new Paint();
Rect rect1 = new Rect(0, 0, photo.getWidth(), photo.getHeight());
Rect rect2 = new Rect(0, 0, 300, 300);
canvas1.drawBitmap(photo, rect1, rect2, paint1); // 首先在dst图片上画上头像

Paint paint2 = new Paint();
paint2.setStrokeWidth(2);
paint2.setStyle(Paint.Style.FILL);
paint2.setColor(Color.RED);
canvas2.drawCircle(150, 150, 150, paint2); // 在src图片上画上一个填充的圆

Paint paint = new Paint();
int layer = canvas3.saveLayer(0, 0, 300, 300, paint, Canvas.ALL_SAVE_FLAG); // 定义一个图层,同时也是定义了PorterDuff效果的Dst区域,如果没有用图层相当于没有定义Dst区域,那么PorterDuff效果就展现的有问题
canvas3.drawBitmap(dst, 0, 0, paint); // 绘制dst图片
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas3.drawBitmap(src, 0, 0, paint); // 绘制圆src图片
canvas3.restoreToCount(layer); //恢复图层到画布上

paint.setXfermode(null); // 重置画笔
canvas.drawBitmap(b3, 0, 0, paint);
PorterDuff效果需要两幅Bitmap图片才能正确展示,lz可能是对这个效果的内涵没有理解清楚



   canvas.drawRoundRect(rectF, 200, 200, paint); 这个方法画出的表面上是圆,实际上它是一个矩形,圆外面的地方还是属于长方形的。
assky124 2016-03-30
  • 打赏
  • 举报
回复

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 圆形图片
 * Created by Coder.Yan on 2015/11/10.
 */
public class CircleImageView extends ImageView {

    private Paint paint = null;
    private float cx = 0;
    private float cy = 0;
    private float radius = 0;
    private Path clipPath = null;

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        clipPath = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
        canvas.restore();
        paint.setColor(0xffffffff);
        canvas.drawCircle(cx,cy,radius,paint);
        paint.setColor(0xff808080);
        canvas.drawCircle(cx,cy,radius-1,paint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int w = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        int h = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
        int r = Math.min(w,h)/2;
        cx = getPaddingLeft() + r;
        cy = getPaddingTop() + r;
        radius = r;
        clipPath.reset();
        clipPath.addCircle(cx,cy,r, Path.Direction.CW);
    }
}
我写的圆形图片控件,参考下
ESC尛蜜蜂 2016-03-29
  • 打赏
  • 举报
回复
/**
	 * 
	 * @param bitmap
	 * @return Bitmap 把图片转化为圆形
	 * 
	 */
	public static Bitmap toRoundBitmap(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;

		if (width <= height) {
			roundPx = width / 2;
			top = 0;
			bottom = width;
			left = 0;
			right = width;
			height = width;
			dst_left = 0;
			dst_top = 0;
			dst_right = width;
			dst_bottom = width;
		}

		else {
			roundPx = height / 2;
			float clip = (width - height) / 2;
			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = height;
			dst_left = 0;
			dst_top = 0;
			dst_right = height;
			dst_bottom = height;
		}

		Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect src = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		final RectF rectF = new RectF(dst);

		paint.setAntiAlias(true);

		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, src, dst, paint);
		return output;
	}
我现在用的图片工具类
boylinux 2016-03-29
  • 打赏
  • 举报
回复
头发还没秃a 2016-03-29
  • 打赏
  • 举报
回复
引用 1 楼 zhumj_zhumj 的回复:
实现圆形外剪切
http://blog.csdn.net/hellochenlian/article/details/38512561这应该是你需要的
ganshenml 2016-03-29
  • 打赏
  • 举报
回复
引用 1 楼 zhumj_zhumj 的回复:
实现圆形外剪切
具体到我上面的代码缺省到了哪步?因为我自身感觉好像应该是这么做的就会有效果。
头发还没秃a 2016-03-29
  • 打赏
  • 举报
回复
实现圆形外剪切
ganshenml 2016-03-29
  • 打赏
  • 举报
回复
引用 4 楼 a511341250 的回复:
/**
	 * 
	 * @param bitmap
	 * @return Bitmap 把图片转化为圆形
	 * 
	 */
	public static Bitmap toRoundBitmap(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;

		if (width <= height) {
			roundPx = width / 2;
			top = 0;
			bottom = width;
			left = 0;
			right = width;
			height = width;
			dst_left = 0;
			dst_top = 0;
			dst_right = width;
			dst_bottom = width;
		}

		else {
			roundPx = height / 2;
			float clip = (width - height) / 2;
			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = height;
			dst_left = 0;
			dst_top = 0;
			dst_right = height;
			dst_bottom = height;
		}

		Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect src = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		final RectF rectF = new RectF(dst);

		paint.setAntiAlias(true);

		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, src, dst, paint);
		return output;
	}
我现在用的图片工具类
感谢,这段代码我在网上查到过很多次。我的那种实现方式也是其中一种的,但是照理说应该是可以正常画出来的,但是不知道是哪里的问题,效果硬是没有出来。
ganshenml 2016-03-29
  • 打赏
  • 举报
回复
引用 6 楼 littlebrain4solving 的回复:
凡是和图片有关的都能采用fresco,它都帮你做了优化和释放的工作了。
额,感谢,恩,网上有些开源库是可以直接用的,但我发这个问题其实就想问,我上面贴的代码实现思路应该是没有错的,但是为什么就画不出来。
  • 打赏
  • 举报
回复
凡是和图片有关的都能采用fresco,它都帮你做了优化和释放的工作了。
  • 打赏
  • 举报
回复
想实现功能不用那么复杂,使用Facebook fresco.

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧