自定义ImageView
package com.interjoy.testdemo.views;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 主要有两种方式实现,Xfermode方式和bitmapShader方式
* Xfermode
*
* @author ylwang
*/
public class RoundCornerImageView extends ImageView {
private Paint mPaint;
private Paint mPaint2;
private int roundHeight = 300;
private int roundWidth = 300;
private SimpleDateFormat simpleDateFormat;
public RoundCornerImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public RoundCornerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RoundCornerImageView(Context context) {
super(context);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias(true);
//16种状态
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mPaint2 = new Paint();
mPaint2.setXfermode(null);
simpleDateFormat = new SimpleDateFormat("'[秒:毫秒] 'ss:SSS");
}
int i = 0;
@Override
public void onDraw(Canvas canvas) {
String time = simpleDateFormat.format(new Date());
int width = getWidth();
Log.d("&&&&", "onDraw 执行次数 = " + (++i) + ", width = " + width + ", 执行时刻->" + time);
if (width == 0) {
super.onDraw(canvas);
return;
}
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
Canvas canvas2 = new Canvas(bitmap);
super.onDraw(canvas2);
drawLeftUp(canvas2);
drawRightUp(canvas2);
drawLeftDown(canvas2);
drawRightDown(canvas2);
canvas.drawBitmap(bitmap, 0, 0, mPaint2);
bitmap.recycle();
}
private void drawLeftUp(Canvas canvas) {
Path path = new Path();
path.moveTo(0, roundHeight);
path.lineTo(0, 0);
path.lineTo(roundWidth, 0);
//arcTo的第二个参数是以多少度为开始点,第三个参数-90度表示逆时针画弧,正数表示顺时针
path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90);
path.close();
canvas.drawPath(path, mPaint);
}
private void drawLeftDown(Canvas canvas) {
Path path = new Path();
path.moveTo(0, getHeight() - roundHeight);
path.lineTo(0, getHeight());
path.lineTo(roundWidth, getHeight());
path.arcTo(new RectF(0, getHeight() - roundHeight * 2, 0 + roundWidth * 2, getHeight()), 90, 90);
path.close();
canvas.drawPath(path, mPaint);
}
private void drawRightDown(Canvas canvas) {
Path path = new Path();
path.moveTo(getWidth() - roundWidth, getHeight());
path.lineTo(getWidth(), getHeight());
path.lineTo(getWidth(), getHeight() - roundHeight);
path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), 0, 90);
path.close();
canvas.drawPath(path, mPaint);
}
private void drawRightUp(Canvas canvas) {
Path path = new Path();
path.moveTo(getWidth(), roundHeight);
path.lineTo(getWidth(), 0);
path.lineTo(getWidth() - roundWidth, 0);
path.arcTo(new RectF(getWidth() - roundWidth * 2, 0, getWidth(), 0 + roundHeight * 2), -90, 90);
path.close();
canvas.drawPath(path, mPaint);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<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">
<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" />
<Button
android:id="@+id/btn_show_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示另外一张图片" />
</LinearLayout>
Activity调用
package com.interjoy.testdemo.Activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.interjoy.testdemo.R;
import com.interjoy.testdemo.views.RoundCornerImageView;
public class ClipActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clip);
initViews();
}
private void initViews() {
final ImageView imageView = new RoundCornerImageView(this);
addContentView(imageView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
findViewById(R.id.btn_show_image).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageResource(R.drawable.yy6);
}
});
}
}
运行效果图