android 圆角ImageView类,可设置弧度

  1 public class RoundImageView extends ImageView {
  2     private Paint paint;
  3     private int roundWidth = 50;
  4     private int roundHeight = 50;
  5     private Paint paint2;
  6
  7     public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
  8         super(context, attrs, defStyle);
  9         init(context, attrs);
 10     }
 11
 12     public RoundImageView(Context context, AttributeSet attrs) {
 13         super(context, attrs);
 14         init(context, attrs);
 15     }
 16
 17     public void SetRoundValue(float roundValue) {
 18         roundWidth = (int) roundValue;
 19         roundHeight = (int) roundValue;
 20     }
 21
 22     public RoundImageView(Context context) {
 23         super(context);
 24         init(context, null);
 25     }
 26
 27     @SuppressLint("Recycle")
 28     private void init(Context context, AttributeSet attrs) {
 29
 30         if (attrs != null) {
 31             TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView);
 32             roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);
 33             roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);
 34         } else {
 35             float density = context.getResources().getDisplayMetrics().density;
 36             roundWidth = (int) (roundWidth * density);
 37             roundHeight = (int) (roundHeight * density);
 38         }
 39
 40         paint = new Paint();
 41         paint.setColor(Color.WHITE);
 42         paint.setAntiAlias(true);
 43         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
 44
 45         paint2 = new Paint();
 46         paint2.setXfermode(null);
 47     }
 48
 49     @Override
 50     public void draw(Canvas canvas) {
 51         Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
 52         Canvas canvas2 = new Canvas(bitmap);
 53         super.draw(canvas2);
 54         drawLiftUp(canvas2);
 55         drawRightUp(canvas2);
 56         drawLiftDown(canvas2);
 57         drawRightDown(canvas2);
 58         canvas.drawBitmap(bitmap, 0, 0, paint2);
 59         bitmap.recycle();
 60         bitmap = null;
 61     }
 62
 63     private void drawLiftUp(Canvas canvas) {
 64         Path path = new Path();
 65         path.moveTo(0, roundHeight);
 66         path.lineTo(0, 0);
 67         path.lineTo(roundWidth, 0);
 68         path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90);
 69         path.close();
 70         canvas.drawPath(path, paint);
 71     }
 72
 73     private void drawLiftDown(Canvas canvas) {
 74         Path path = new Path();
 75         path.moveTo(0, getHeight() - roundHeight);
 76         path.lineTo(0, getHeight());
 77         path.lineTo(roundWidth, getHeight());
 78         path.arcTo(new RectF(0, getHeight() - roundHeight * 2, 0 + roundWidth * 2, getWidth()), 90, 90);
 79         path.close();
 80         canvas.drawPath(path, paint);
 81     }
 82
 83     private void drawRightDown(Canvas canvas) {
 84         Path path = new Path();
 85         path.moveTo(getWidth() - roundWidth, getHeight());
 86         path.lineTo(getWidth(), getHeight());
 87         path.lineTo(getWidth(), getHeight() - roundHeight);
 88         path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), 0,
 89                 90);
 90         path.close();
 91         canvas.drawPath(path, paint);
 92     }
 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, paint);
102     }
103
104 }
时间: 2024-08-25 12:47:16

android 圆角ImageView类,可设置弧度的相关文章

android activity ImageView全屏设置

开始接触android也有1月有余了,看了一小部分的教学视频+刚哥的疯狂讲义.总是看着视频做一些Demo,有些木讷.今天尝试终于进入项目中,在项目中巩固知识点. 功能1 project启动 显示欢迎页面而后跳转 主页面: 问题点 1 实现Imange 全屏显示: 解决方案: 1.设置image scaleType 属性: android:scaleType="fitXY" 2.设置 activity theme 属性: <activity android:name=".

Android自己定义圆角ImageView

我们常常看到一些app中能够显示圆角图片.比方qq的联系人图标等等,实现圆角图片一种办法是直接使用圆角图片资源,当然假设没有圆角图片资源.我们也能够自己通过程序实现的,以下介绍一个自己定义圆角ImageView的方法: package com.yulongfei.imageview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; impo

Android自定义圆角ImageView

我们经常看到一些app中可以显示圆角图片,比如qq的联系人图标等等,实现圆角图片一种办法是直接使用圆角图片资源,当然如果没有圆角图片资源,我们也可以自己通过程序实现的,下面介绍一个自定义圆角ImageView的方法: package com.yulongfei.imageview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; impor

【Android】 ImageView.ScaleType设置图解

ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1. 在layout xml中定义android:scaleType="CENTER" 2. 或在代码中调用imageView.setScaleType(ImageView.ScaleType.CENTER); 接下来,将对ScaleType的值和对应的显示效果用最直观的方式--真图演示的方法,来进行说明. 首先,是测试使用的原始图片: (

Android 圆角的效果实现

Android 自定义ImageView实现圆角图片昨天给学生布置作业,写微信首页,也就是聊天的界面,listView里的item中联系人的头像是圆角的,图形界面如下: 那么我就仔细研究了圆角的具体实现.那么首先,我想到了第一种实现方案:1.就是给ImageView定义shape.xml文件,然后用src指定组件背景.那么想到这个方案的时候,我首先了解了一下ImageView的src和background属性.background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是

17 自定义属性及圆角imageView

1 public class RoundAngleImageView extends ImageView { 2 //cn.dpocket.moplusand.uinew是app的包名 manifest文件里面 3 private static final String NAMESPACE = "http://schemas.android.com/apk/res/cn.dpocket.moplusand.uinew"; 4 private Paint paint; 5 //如果不在x

android : ImageView 属性

正文 一.结构 java.lang.Object android.view.View android.widget.ImageView 已知直接子类: ImageButton, QuickContactBadge 已知间接子类: ZoomButton 二.类概述 显示任意图像,例如图标.ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项. 三.XML属性 属性名称 描述 android:adjus

Android中如何为ListView设置静态数据

有的时候我们需要为一个listview设置固定的数据,下边就是如何设置静态的数据 布局文件listview 的主页面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

android自定义刷新类控件

android虽然定义了种类非常丰富的控件,但是有的时候这些自定义的控件还是不能满足我的要求,为了能够适配更多的需求,我们需要在原有的基础上进行自定义控件. 今天我向大家介绍的就是android中最常见的刷新类控件.因为我们最近正在参加一个项目,在项目组长的带领下,我学到了很多的东西,这对我的android技术的提升非常大,定义一个自定义控件可能不是很难,但是如何让这个自定义控件更加有效.更加快速地运行. 首先我们需要建立一个自定义控件类: package com.example.ui.widg