用了混合模式花了一个圆形图片,并在外围加了个白边;
具体实现Demo:
package com.example.view;
import com.example.circleview.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.widget.ImageView;
public class CircleView extends ImageView{
//自定义属性
private int mCWidth;
private int mCColor;
public CircleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CirCleView);
mCWidth = array.getDimensionPixelSize(R.styleable.CirCleView_c_width, 0);
mCColor = array.getColor(R.styleable.CirCleView_c_color, Color.BLUE);
array.recycle();
}
@Override
public void setImageBitmap(Bitmap bm) {
Bitmap bitmap =Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Config.ARGB_8888);
//画布
Canvas canvas = new Canvas(bitmap);
//画笔,并且设置抗锯齿
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
//是指圆的宽,高,半径
int radius = (Math.min(bitmap.getWidth(), bitmap.getHeight()))/2;
canvas.drawCircle(bitmap.getWidth()/2, bm.getHeight()/2, radius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bm, 0,0,paint);
setScaleType(ScaleType.CENTER);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(mCWidth);
paint.setColor(mCColor);
canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, radius-2, paint);
super.setImageBitmap(bitmap);
}
}
Layout布局
使用自定义属性,要引入包,要不系统找不到:
xmlns:myapp="http://schemas.android.com/apk/res/com.example.circleview"
总结:其中:xmlns:后面的名字随便起,导入的包是你AndroidManifest.xml中的package=”com.example.circleview”这个包要一样
具体使用的代码:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.circleview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:gravity="center" >
<com.example.view.CircleView
android:id="@+id/circleView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/pic2"
myapp:c_width="3dp"<!--自定义属性-->
myapp:c_color="#ff669900"
/>
</RelativeLayout>
自定义属性:
在values目录下建一个attrs.xml文件,名字不能随便写,里面的属性是提示不出来的,而且不能写错
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CirCleView">
<attr name="c_width" format="dimension"></attr>
<attr name="c_color" format="color"></attr>
</declare-styleable>
</resources>
总结:目前阶段学到的一个小东西,都是最基础的东西。也是学习的笔记,还望多多指教,分享学习!如果存在不足,还望指出!!