MyView 就是定义在<declare-styleable name="MyView "></declare-styleable> 里的 名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法。
private void init(AttributeSet attributeSet, int defStyle) { //首先判断attributeSet是否为null //if(attributeSet != null){ //获取当前MyView所在的Activity的theme Resources.Theme theme = getContext().getTheme(); //通过theme的obtainStyledAttributes方法获取TypedArray对象 TypedArray typedArray = theme.obtainStyledAttributes(attributeSet, R.styleable.SwitchToggleView, 0, 0); //获取typedArray的长度 mColor = typedArray.getColor(R.styleable.SwitchToggleView_CurrentState,Color.GREEN); //在使用完typedArray之后,要调用recycle方法回收资源 typedArray.recycle(); // } }
---------------------------------------------------------------------
package com.a_sq.utils; import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; import com.a_sq.R; /** * Created by root on 17-3-5. */ public class RoundImageView extends ImageView { private Paint mPaintBitmap = new Paint(Paint.ANTI_ALIAS_FLAG); private Bitmap mRawBitmap; private BitmapShader mShader; private Matrix mMatrix = new Matrix(); private Canvas canvas; private int mColor = Color.GREEN; public RoundImageView(Context context, AttributeSet attrs) { super(context, attrs); init(attrs,0); } public RoundImageView(Context context, AttributeSet attrs,int defStyleAttr) { super(context, attrs); init(attrs,defStyleAttr); } private void init(AttributeSet attributeSet, int defStyle) { //首先判断attributeSet是否为null //if(attributeSet != null){ //获取当前MyView所在的Activity的theme Resources.Theme theme = getContext().getTheme(); //通过theme的obtainStyledAttributes方法获取TypedArray对象 TypedArray typedArray = theme.obtainStyledAttributes(attributeSet, R.styleable.SwitchToggleView, 0, 0); //获取typedArray的长度 mColor = typedArray.getColor(R.styleable.SwitchToggleView_CurrentState,Color.GREEN); //在使用完typedArray之后,要调用recycle方法回收资源 typedArray.recycle(); // } } @Override protected void onDraw(Canvas canvas) { float radix = getWidth()/2f; float x = getWidth()/2f; float y = getHeight()/2f; float red_length=10f; Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //paint.setColor(Color.RED); paint.setColor(mColor); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(red_length); canvas.drawCircle(x,y,radix-red_length/2f,paint); radix = radix-red_length; float blue_lenght = 10f; paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(blue_lenght); canvas.drawCircle(x,y,radix-blue_lenght/2f,paint); radix = radix-blue_lenght; Bitmap rawBitmap = getBitmap(getDrawable()); if (rawBitmap != null){ if (mShader == null || !rawBitmap.equals(mRawBitmap)){ mRawBitmap = rawBitmap; mShader = new BitmapShader(mRawBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); } if (mShader != null){ float scale = Math.min(radix*2/rawBitmap.getWidth(),radix*2 / rawBitmap.getHeight()); mMatrix.setScale(scale, scale,rawBitmap.getWidth()/2f,rawBitmap.getHeight()/2f);//等比缩放 //mMatrix.setScale(scale, scale);//等比缩放 mShader.setLocalMatrix(mMatrix); } mPaintBitmap.setShader(mShader); canvas.drawCircle(x, y, radix, mPaintBitmap); } else { super.onDraw(canvas); } } private Bitmap getBitmap(Drawable drawable){ if (drawable instanceof BitmapDrawable){ return ((BitmapDrawable)drawable).getBitmap(); } else if (drawable instanceof ColorDrawable){ Rect rect = drawable.getBounds(); int width = rect.right - rect.left; int height = rect.bottom - rect.top; int color = ((ColorDrawable)drawable).getColor(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawARGB(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color)); return bitmap; } else { return null; } } }