您好,登錄后才能下訂單哦!
1.新建一個RoundImageView.java文件
package com.jhg.freshman;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;
public class RoundImageView extends android.support.v7.widget.AppCompatImageView {
/**
* 圓形模式
*/
private static final int MODE_CIRCLE = 1;
/**
* 普通模式
*/
private static final int MODE_NONE = 0;
/**
* 圓角模式
*/
private static final int MODE_ROUND = 2;
private Paint mPaint;
private int currMode = 0;
/**
* 圓角半徑
*/
private int currRound = dp2px(10);
public RoundImageView(Context context) {
super(context);
initViews();
}
public RoundImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
obtainStyledAttrs(context, attrs, defStyleAttr);
initViews();
}
private void obtainStyledAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView, defStyleAttr, 0);
currMode = a.hasValue(R.styleable.RoundImageView_type) ? a.getInt(R.styleable.RoundImageView_type, MODE_NONE) : MODE_NONE;
currRound = a.hasValue(R.styleable.RoundImageView_radius) ? a.getDimensionPixelSize(R.styleable.RoundImageView_radius, currRound) : currRound;
a.recycle();
}
private void initViews() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/**
* 當模式為圓形模式的時候,我們強制讓寬高一致
*/
if (currMode == MODE_CIRCLE) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int result = Math.min(getMeasuredHeight(), getMeasuredWidth());
setMeasuredDimension(result, result);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onDraw(Canvas canvas) {
Drawable mDrawable = getDrawable();
Matrix mDrawMatrix = getImageMatrix();
if (mDrawable == null) {
return; // couldn't resolve the URI
}
if (mDrawable.getIntrinsicWidth() == 0 || mDrawable.getIntrinsicHeight() == 0) {
return; // nothing to draw (empty bounds)
}
if (mDrawMatrix == null && getPaddingTop() == 0 && getPaddingLeft() == 0) {
mDrawable.draw(canvas);
} else {
final int saveCount = canvas.getSaveCount();
canvas.save();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if (getCropToPadding()) {
final int scrollX = getScrollX();
final int scrollY = getScrollY();
canvas.clipRect(scrollX + getPaddingLeft(), scrollY + getPaddingTop(),
scrollX + getRight() - getLeft() - getPaddingRight(),
scrollY + getBottom() - getTop() - getPaddingBottom());
}
}
canvas.translate(getPaddingLeft(), getPaddingTop());
if (currMode == MODE_CIRCLE) {//當為圓形模式的時候
Bitmap bitmap = drawable2Bitmap(mDrawable);
mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, mPaint);
} else if (currMode == MODE_ROUND) {//當為圓角模式的時候
Bitmap bitmap = drawable2Bitmap(mDrawable);
mPaint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawRoundRect(new RectF(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(), getHeight() - getPaddingBottom()),
currRound, currRound, mPaint);
} else {
if (mDrawMatrix != null) {
canvas.concat(mDrawMatrix);
}
mDrawable.draw(canvas);
}
canvas.restoreToCount(saveCount);
}
}
/**
* drawable轉換成bitmap
*/
private Bitmap drawable2Bitmap(Drawable drawable) {
if (drawable == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//根據傳遞的scaletype獲取matrix對象,設置給bitmap
Matrix matrix = getImageMatrix();
if (matrix != null) {
canvas.concat(matrix);
}
drawable.draw(canvas);
return bitmap;
}
private int dp2px(float value) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics());
}
}
2.在Value下新建一個attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RoundImageView">
<attr name="type" format="enum">
<enum name="circle" value="1"/>
<enum name="round" value="2"/>
</attr>
<attr name="radius" format="dimension"/>ssss
</declare-styleable>
</resources>
3.在layout布局文件中使用RoundImage控件
<com.jhg.freshman.RoundImageView
android:id="@+id/iv"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:src="@drawable/head"
app:type="circle"
/>
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。