Android的简易圆形头像
Posted nikitave
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android的简易圆形头像相关的知识,希望对你有一定的参考价值。
package com.example.administrator.test.Five.Second;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
import com.example.administrator.test.R;
/**
* Created by Administrator on 2017/7/21.
*/
public class AnomalousPhotoView extends View {
private Bitmap bmpCat;
private Bitmap bmpMask;
private Bitmap bmpresult;
Canvas c_bmpcat;
Canvas c_bmpmask;
Canvas c_result;
private Paint paint;
private static final int OFFSET=10;
public AnomalousPhotoView(Context context, AttributeSet attrs){
super(context,attrs);
bmpCat= BitmapFactory.decodeResource(context.getResources(), R.drawable.luoluo);
bmpMask= Bitmap.createBitmap(bmpCat.getWidth(),bmpCat.getHeight(), Bitmap.Config.ARGB_8888);
c_bmpmask=new Canvas(bmpMask);
paint=new Paint(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
int id=canvas.saveLayer(0,0,bmpCat.getWidth(),bmpCat.getHeight(),null,Canvas.ALL_SAVE_FLAG);
canvas.drawBitmap(bmpCat,0,0,null);
paint.setColor(Color.BLUE);
c_bmpmask.drawCircle(bmpCat.getWidth()/2,bmpCat.getHeight()/2,200,paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
//canvas.drawCircle(100,100,100,paint);
canvas.drawBitmap(bmpMask,0,0,paint);
canvas.restoreToCount(id);
}
}
以上是关于Android的简易圆形头像的主要内容,如果未能解决你的问题,请参考以下文章
Android自定义控件实例,圆形头像(图库 + 裁剪+设置),上传头像显示为圆形,附源码