android 自定义属性

MyView 就是定义在<declare-styleable name="MyView "></declare-styleable> 里的 名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法。

private void init(AttributeSet attributeSet, int defStyle) {
    //首先判断attributeSet是否为null
    //if(attributeSet != null){
        //获取当前MyView所在的Activitytheme
        Resources.Theme theme = getContext().getTheme();
        //通过themeobtainStyledAttributes方法获取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所在的Activitytheme
            Resources.Theme theme = getContext().getTheme();
            //通过themeobtainStyledAttributes方法获取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;
        }
    }




}



    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值