canvas教程

Android中圆角矩形和圆形的多种实现方式(2)

字号+ 作者:H5之家 来源:H5之家 2017-02-24 12:03 我要评论( )

设置渲染器的变换矩阵 /** * 这个函数为设置BitmapShader的Matrix参数,设置最小缩放比例,平移参数。 * 作用:保证图片损失度最小和始终绘制图片正中央的那部分 */ private void updateShaderMatrix() { float sca

 设置渲染器的变换矩阵

/** * 这个函数为设置BitmapShader的Matrix参数,设置最小缩放比例,平移参数。 * 作用:保证图片损失度最小和始终绘制图片正中央的那部分 */ private void updateShaderMatrix() { float scaleX = 1.0f; float scaleY = 1.0f; float scale = 1.0f; float dx = 0; float dy = 0; // 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值 if (type == TYPE_CIRCLE) { scaleX = mWidth * 1.0f / mBitmapWidth; scaleY = mWidth * 1.0f / mBitmapHeight; scale = Math.max(scaleX, scaleY); } else if (type == TYPE_ROUND) { scaleX = getWidth() * 1.0f / mBitmapWidth; scaleY = getHeight() * 1.0f / mBitmapHeight; scale = Math.max(scaleX, scaleY); } if (scaleX > scaleY) { // x轴缩放 y轴平移 使得图片的x轴方向的边的尺寸缩放到图片显示区域(mDrawableRect)一样) dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f; } else { // y轴缩放 x轴平移 使得图片的y轴方向的边的尺寸缩放到图片显示区域(mDrawableRect)一样) dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f; } mShaderMatrix.set(null); //缩放 mShaderMatrix.setScale(scale, scale); // 平移 mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth); // 设置变换矩阵 mBitmapShader.setLocalMatrix(mShaderMatrix); }

 onDraw

@Override protected void onDraw(Canvas canvas) { //如果图片不存在就不画 if (getDrawable() == null) return; if (type == TYPE_ROUND) { //绘制内圆角矩形,参数矩形区域,圆角半径,图片画笔为mBitmapPaint canvas.drawRoundRect(mDrawableRect, mRoundRadius, mRoundRadius, mBitmapPaint); if (mBorderWidth != 0) { //如果圆形边缘的宽度不为0 我们还要绘制带边界的外圆角矩形 参数矩形区域,圆角半径,边界画笔为mBorderPaint canvas.drawRoundRect(mBorderRect , mRoundRadius + mBorderWidth / 2, mRoundRadius + mBorderWidth / 2, mBorderPaint); } } else if (type == TYPE_CIRCLE) { //绘制内圆形,参数圆心坐标,内圆半径,图片画笔为mBitmapPaint canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint); //如果圆形边缘的宽度不为0 我们还要绘制带边界的外圆形 参数圆心坐标,外圆半径,边界画笔为mBorderPaint if (mBorderWidth != 0) { canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint); } } }

而且,我们给自定义View添加了几个接口,可以用来直接设置类型、边缘颜色、边缘宽度和图片信息等。

使用CircleImageView
布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:attr="http://schemas.android.com/apk/res/com.hx.circleimageview" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#CDCDC1" android:orientation="vertical" > <com.hx.circleimageview.CircleImageView android:id="@+id/image1" android:layout_width="150dp" android:layout_height="150dp" android:layout_margin="10dp" android:src="@drawable/crazy_1" attr:type="circle" attr:border_color="#FFffffff" attr:border_width="2dp" /> <com.hx.circleimageview.CircleImageView android:id="@+id/image2" android:layout_width="150dp" android:layout_height="150dp" android:layout_margin="10dp" android:src="@drawable/crazy_2" attr:type="round" attr:border_width="2dp" /> <com.hx.circleimageview.CircleImageView android:id="@+id/image3" android:layout_width="250dp" android:layout_height="150dp" android:layout_margin="10dp" android:src="@drawable/crazy_3" attr:type="round" attr:round_Radius="20dp" attr:border_color="#9400D3" attr:border_width="5dp" /> </LinearLayout>

我们在JAVA中对三个ImageView添加点击事件

@Override public void onClick(View v) { switch (v.getId()) { case R.id.image1: image1.setBorderColor(Color.BLACK); break; case R.id.image2: image2.setImageResource(R.drawable.crazy_3); break; case R.id.image3: int type = image3.getType() == CircleImageView.TYPE_CIRCLE ? CircleImageView.TYPE_ROUND : CircleImageView.TYPE_CIRCLE; image3.setType(type); break; }

运行后效果图如下:

以上就是Android中圆角矩形和圆形的多种实现方式,希望对大家的学习有所帮助,也希望大家多多支持五二脚本。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Canvas版画图、圆角矩形、圆形、矩形、图形填充、mvc模式重新整

    Canvas版画图、圆角矩形、圆形、矩形、图形填充、mvc模式重新整

    2016-05-20 12:00

  • html5 Canvas画图教程(10)―把面拆成线条模拟出圆角矩形

    html5 Canvas画图教程(10)―把面拆成线条模拟出圆角矩形

    2016-03-02 14:26

  • canvas绘制圆角矩形

    canvas绘制圆角矩形

    2016-02-07 19:00

  • HTML5 Canvas自定义圆角矩形与虚线示例代码

    HTML5 Canvas自定义圆角矩形与虚线示例代码

    2015-10-07 14:29

网友点评
$