此篇是我圆形图片学习总结的文章,最终实现的效果图如下
文章参考
Android圆形图片不求人,自定义View实现(BitmapShader使用)
Android - 实现图片圆角显示的几种方式
Android 完美实现图片圆角和圆形(对实现进行分析)
核心代码
class LoadTask1 extends AsyncTask<Void, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Void... params) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
//这里是获取到原图
Bitmap bitmapSource = BitmapFactory.decodeResource(getResources(), R.drawable.z);
//我通过getWidth,getHeight获取到了宽高
Bitmap result = Bitmap.createBitmap(bitmapSource.getWidth(), bitmapSource.getHeight(), Bitmap.Config.ARGB_8888);
//创建一个画布
Canvas canvas = new Canvas(result);
//通过宽高比,获取到最小的那个值
int min = 0;
if (result.getWidth() > result.getHeight()){
min = result.getHeight();
}else if (result.getWidth() < result.getHeight()){
min = result.getWidth();
}else {
min = result.getWidth();
}
canvas.drawCircle(min/2,min/2,min/2,paint);//先画一个圆
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//表示我下一步要取交集的地方
canvas.drawBitmap(bitmapSource, 0, 0, paint);//又画一个图,并且这个图是在圆形的上面,此时就是获取到交集的地方
return result;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
}
核心思想
首先是画了一个圆形图片通过canvas.drawCircle方法
PorterDuff.Mode.SRC_IN:指明了取交集的部分
canvas.drawBitmap:又创建一个图片,这个图片是在圆形图之上的,此时就会有交集部分了
全部代码
public class MainActivity extends AppCompatActivity {
int imageSize, radius;
ImageView imageView;
ImageView imageView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image);
imageView2 = (ImageView) findViewById(R.id.image2);
imageSize = getResources().getDimensionPixelSize(R.dimen.image_size);
radius = getResources().getDimensionPixelSize(R.dimen.radius);
new LoadTask1().execute();
new LoadTask2().execute();
}
class LoadTask1 extends AsyncTask<Void, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Void... params) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
//这里是获取到原图
Bitmap bitmapSource = BitmapFactory.decodeResource(getResources(), R.drawable.z);
//我通过getWidth,getHeight获取到了宽高
Bitmap result = Bitmap.createBitmap(bitmapSource.getWidth(), bitmapSource.getHeight(), Bitmap.Config.ARGB_8888);
//创建一个画布
Canvas canvas = new Canvas(result);
//通过宽高比,获取到最小的那个值
int min = 0;
if (result.getWidth() > result.getHeight()){
min = result.getHeight();
}else if (result.getWidth() < result.getHeight()){
min = result.getWidth();
}else {
min = result.getWidth();
}
canvas.drawCircle(min/2,min/2,min/2,paint);//先画一个圆
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//表示我下一步要取交集的地方
canvas.drawBitmap(bitmapSource, 0, 0, paint);//又画一个图,并且这个图是在圆形的上面,此时就是获取到交集的地方
return result;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
}
class LoadTask2 extends AsyncTask<Void, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Void... params) {
Bitmap result = Bitmap.createBitmap(imageSize, imageSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.an);
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(0, 0, imageSize, imageSize);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
Path path = new Path();
path.addRoundRect(rectF, radius, radius, Path.Direction.CW);
canvas.clipPath(path, Region.Op.INTERSECT);
canvas.drawBitmap(bitmap, rect, rectF, paint);
return result;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
imageView2.setImageBitmap(bitmap);
}
}
}