Android圆角图片

小咪咪 2022-06-14 09:07 416阅读 0赞

自定义ImageView

  1. package com.interjoy.testdemo.views;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Bitmap.Config;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.Path;
  9. import android.graphics.PorterDuff;
  10. import android.graphics.PorterDuffXfermode;
  11. import android.graphics.RectF;
  12. import android.util.AttributeSet;
  13. import android.util.Log;
  14. import android.widget.ImageView;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Date;
  17. /** * 主要有两种方式实现,Xfermode方式和bitmapShader方式 * Xfermode * * @author ylwang */
  18. public class RoundCornerImageView extends ImageView {
  19. private Paint mPaint;
  20. private Paint mPaint2;
  21. private int roundHeight = 300;
  22. private int roundWidth = 300;
  23. private SimpleDateFormat simpleDateFormat;
  24. public RoundCornerImageView(Context context, AttributeSet attrs,
  25. int defStyle) {
  26. super(context, attrs, defStyle);
  27. }
  28. public RoundCornerImageView(Context context, AttributeSet attrs) {
  29. super(context, attrs);
  30. init();
  31. }
  32. public RoundCornerImageView(Context context) {
  33. super(context);
  34. init();
  35. }
  36. private void init() {
  37. mPaint = new Paint();
  38. mPaint.setColor(Color.WHITE);
  39. mPaint.setAntiAlias(true);
  40. //16种状态
  41. mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
  42. mPaint2 = new Paint();
  43. mPaint2.setXfermode(null);
  44. simpleDateFormat = new SimpleDateFormat("'[秒:毫秒] 'ss:SSS");
  45. }
  46. int i = 0;
  47. @Override
  48. public void onDraw(Canvas canvas) {
  49. String time = simpleDateFormat.format(new Date());
  50. int width = getWidth();
  51. Log.d("&&&&", "onDraw 执行次数 = " + (++i) + ", width = " + width + ", 执行时刻->" + time);
  52. if (width == 0) {
  53. super.onDraw(canvas);
  54. return;
  55. }
  56. Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
  57. Canvas canvas2 = new Canvas(bitmap);
  58. super.onDraw(canvas2);
  59. drawLeftUp(canvas2);
  60. drawRightUp(canvas2);
  61. drawLeftDown(canvas2);
  62. drawRightDown(canvas2);
  63. canvas.drawBitmap(bitmap, 0, 0, mPaint2);
  64. bitmap.recycle();
  65. }
  66. private void drawLeftUp(Canvas canvas) {
  67. Path path = new Path();
  68. path.moveTo(0, roundHeight);
  69. path.lineTo(0, 0);
  70. path.lineTo(roundWidth, 0);
  71. //arcTo的第二个参数是以多少度为开始点,第三个参数-90度表示逆时针画弧,正数表示顺时针
  72. path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90);
  73. path.close();
  74. canvas.drawPath(path, mPaint);
  75. }
  76. private void drawLeftDown(Canvas canvas) {
  77. Path path = new Path();
  78. path.moveTo(0, getHeight() - roundHeight);
  79. path.lineTo(0, getHeight());
  80. path.lineTo(roundWidth, getHeight());
  81. path.arcTo(new RectF(0, getHeight() - roundHeight * 2, 0 + roundWidth * 2, getHeight()), 90, 90);
  82. path.close();
  83. canvas.drawPath(path, mPaint);
  84. }
  85. private void drawRightDown(Canvas canvas) {
  86. Path path = new Path();
  87. path.moveTo(getWidth() - roundWidth, getHeight());
  88. path.lineTo(getWidth(), getHeight());
  89. path.lineTo(getWidth(), getHeight() - roundHeight);
  90. path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), 0, 90);
  91. path.close();
  92. canvas.drawPath(path, mPaint);
  93. }
  94. private void drawRightUp(Canvas canvas) {
  95. Path path = new Path();
  96. path.moveTo(getWidth(), roundHeight);
  97. path.lineTo(getWidth(), 0);
  98. path.lineTo(getWidth() - roundWidth, 0);
  99. path.arcTo(new RectF(getWidth() - roundWidth * 2, 0, getWidth(), 0 + roundHeight * 2), -90, 90);
  100. path.close();
  101. canvas.drawPath(path, mPaint);
  102. }
  103. }

布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.interjoy.testdemo.Activities.ClipActivity">
  3. <com.interjoy.testdemo.views.RoundCornerImageView android:layout_margin="20dp" android:src="@drawable/yy6" android:id="@+id/iv_clip" android:layout_width="200dp" android:layout_height="200dp" />
  4. <Button android:id="@+id/btn_show_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示另外一张图片" />
  5. </LinearLayout>

Activity调用

  1. package com.interjoy.testdemo.Activities;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.ImageView;
  7. import com.interjoy.testdemo.R;
  8. import com.interjoy.testdemo.views.RoundCornerImageView;
  9. public class ClipActivity extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_clip);
  14. initViews();
  15. }
  16. private void initViews() {
  17. final ImageView imageView = new RoundCornerImageView(this);
  18. addContentView(imageView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
  19. findViewById(R.id.btn_show_image).setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. imageView.setImageResource(R.drawable.yy6);
  23. }
  24. });
  25. }
  26. }

运行效果图
这里写图片描述

这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,416人围观)

还没有评论,来说两句吧...

相关阅读