当前位置:   article > 正文

Android实现图片圆角显示的几种方式_android 圆角图片

android 圆角图片

在实际开发中我们经常会对图片进行圆角处理,这里做一个小总结,文章结尾还有一个不到100行实现的,支持圆形,圆角和TransitionDrawable的ImageView

首先是效果图:



实现这种效果有两种方式:

在图片上做

把图片裁剪成圆角图片

优点:节约内存

  1. public Bitmap getRoundRectBitmap(Bitmap bitmap, int radius) {
  2. Paint paint = new Paint();
  3. paint.setAntiAlias(true);
  4. paint.setFilterBitmap(true);
  5. int bmWidth = bitmap.getWidth();
  6. int bmHeight = bitmap.getHeight();
  7. final RectF rectF = new RectF(0, 0, width, height);
  8. Canvas canvas = new Canvas(bitmap);
  9. paint.setXfermode(null);
  10. canvas.drawRoundRect(rectF, radius, radius, paint);
  11. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  12. canvas.drawBitmap(bitmap, rect, rectF, paint);
  13. return bitmap;
  14. }


控件绘制的时候,控制显示区域在控件上做

优点:方便省事,适合各种图片框架

ImageView

  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. int saveCount = canvas.getSaveCount();
  4. canvas.save();
  5. super.onDraw(canvas);
  6. mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
  7. canvas.drawRoundRect(rectF, radius, radius, mPaint);
  8. canvas.restoreToCount(saveCount);
  9. }


支持圆形 圆角矩形ShapedImageView

  • 支持TransitionDrawable
好了,直接展示代码:

首先是MainActivity.java

  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. }
  7. }

然后是布局文件activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:test="http://schemas.android.com/apk/res/com.example.shapeimage"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:gravity="center" >
  7. <com.example.shapeimage.ShapedImageView
  8. android:id="@+id/image1"
  9. android:layout_width="200dp"
  10. android:layout_height="200dp"
  11. android:layout_centerInParent="true"
  12. android:src="@drawable/an"
  13. test:shape_mode="circle" />
  14. <com.example.shapeimage.ShapedImageView
  15. android:id="@+id/image1"
  16. android:layout_width="200dp"
  17. android:layout_height="200dp"
  18. android:layout_marginTop="20dp"
  19. android:layout_centerInParent="true"
  20. test:round_radius="20dp"
  21. android:src="@drawable/an"
  22. test:shape_mode="round_rect" />
  23. </LinearLayout>

一个属性资源文件attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="ShapedImageView">
  4. <attr name="shape_mode" format="enum">
  5. <enum name="round_rect" value="1" />
  6. <enum name="circle" value="2" />
  7. </attr>
  8. <attr name="round_radius" format="dimension" />
  9. </declare-styleable>
  10. </resources>


最重要的就是ShapedImageView.java这个类

  1. import java.util.Arrays;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.PorterDuffXfermode;
  8. import android.graphics.PorterDuff;
  9. import android.graphics.drawable.shapes.RoundRectShape;
  10. import android.graphics.drawable.shapes.Shape;
  11. import android.os.Build;
  12. import android.util.AttributeSet;
  13. import android.widget.ImageView;
  14. public class ShapedImageView extends ImageView {
  15. private static final int SHAPE_MODE_ROUND_RECT = 1;
  16. private static final int SHAPE_MODE_CIRCLE = 2;
  17. private int mShapeMode = 0;
  18. private float mRadius = 0;
  19. private Shape mShape;
  20. private Paint mPaint;
  21. public ShapedImageView(Context context) {
  22. super(context);
  23. init(null);
  24. }
  25. public ShapedImageView(Context context, AttributeSet attrs) {
  26. super(context, attrs);
  27. init(attrs);
  28. }
  29. public ShapedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
  30. super(context, attrs, defStyleAttr);
  31. init(attrs);
  32. }
  33. private void init(AttributeSet attrs) {
  34. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  35. setLayerType(LAYER_TYPE_HARDWARE, null);
  36. }
  37. if (attrs != null) {
  38. TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ShapedImageView);
  39. mShapeMode = a.getInt(R.styleable.ShapedImageView_shape_mode, 0);
  40. mRadius = a.getDimension(R.styleable.ShapedImageView_round_radius, 0);
  41. a.recycle();
  42. }
  43. mPaint = new Paint();
  44. mPaint.setAntiAlias(true);
  45. mPaint.setFilterBitmap(true);
  46. mPaint.setColor(Color.BLACK);
  47. mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
  48. }
  49. @Override
  50. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  51. super.onLayout(changed, left, top, right, bottom);
  52. if (changed) {
  53. switch (mShapeMode) {
  54. case SHAPE_MODE_ROUND_RECT:
  55. break;
  56. case SHAPE_MODE_CIRCLE:
  57. int min = Math.min(getWidth(), getHeight());
  58. mRadius = (float) min / 2;
  59. break;
  60. }
  61. if (mShape == null) {
  62. float[] radius = new float[8];
  63. Arrays.fill(radius, mRadius);
  64. mShape = new RoundRectShape(radius, null, null);
  65. }
  66. mShape.resize(getWidth(), getHeight());
  67. }
  68. }
  69. @Override
  70. protected void onDraw(Canvas canvas) {
  71. int saveCount = canvas.getSaveCount();
  72. canvas.save();
  73. super.onDraw(canvas);
  74. switch (mShapeMode) {
  75. case SHAPE_MODE_ROUND_RECT:
  76. case SHAPE_MODE_CIRCLE:
  77. if (mShape != null) {
  78. mShape.draw(canvas, mPaint);
  79. }
  80. break;
  81. }
  82. canvas.restoreToCount(saveCount);
  83. }
  84. }



如果图片展示出来不全或者变形,可以参考<ImageView的Scaletype的属性>这篇文章去根据自己的需要设置响应的值(推荐使用scaleType="centerCrop")

好了,基本到这里就实现我们要的效果了,最后放上源码下载地址:

首先是github上面的项目源码地址点击跳转到项目源码地址

然后是Eclipse工程格式的demo下载地址点击下载demo


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/816404
推荐阅读
相关标签
  

闽ICP备14008679号