Drawable

Drawable

概述

  • Drawable是一个抽象类,是对可绘制物体的抽象.与View不同的是Drawable没有事件和交互方法
  • Drawable 有好多子类操作具体类型资源,比如
    • BitmapDrawable是用来操作位图
    • ColorDrawable用来操作颜色
    • ClipDrawable用来操作剪切板

原理

  • draw(Canvas canvas)
  • setBounds()

BitmapDrawable

是对bitmap的一种包装,可一设置它包装的bitmap在BitmapDrawable区域内的绘制方式 如平铺填充,拉伸填充或者保持图片原始大小




LayerDrawable

可以管理一组drawable对象在LayerDrawable的drawable资源按照列表的顺序绘制,列表的最后一个drawable绘制在上层



    
        
    

    
        
    

StatusListDrawable

就是selector

LevelListDrawable



    

    


iv_name.setImageLevel(8); //根据设置不同的level显示不同的图片

TransitionDrawable

它只负责管理两层drawable,并且提供了一个透明度变化的动画,可以控制从一层drawable过度到另一层drawable的动画效果。
适合lian两幅同样的图片但是颜色不一样 显示过度效果



    
    


 TransitionDrawable drawable = (TransitionDrawable) iv_name.getDrawable();
                drawable.reverseTransition(1000);

InsertDrawable

效果和 pading差不多




ClipDrawable

  • ClicpDrawable是对Drawable尽享剪切操作,控制剪切区域
  • android中的进度条就是使用ClipDrawable实现效果的,它根据level的属性值,决定剪切区域的大小
  • ClipDrawable通过setLevel() 方法来设置剪切区域,level从0到1000,当为0时完全不显示,为10000时完全显示
    



 ClipDrawable background = (ClipDrawable) iv_name.getDrawable();
        background.setLevel(6000);

自定义Drawable

public class CircleDrawable extends Drawable {
    private Paint mPaint;
    private int mWidth;
    private Bitmap mBitmap;
    private final BitmapShader bitmapShader;

    public CircleDrawable(Bitmap mBitmap) {
        this.mBitmap = mBitmap;
        //着色器
        bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(bitmapShader);
        mWidth = Math.min(mBitmap.getHeight(), mBitmap.getWidth());
    }
    @Override
    public void draw(@NonNull Canvas canvas) {
        canvas.drawCircle(mWidth/2,mWidth/2,mWidth/2,mPaint);
    }
    @Override
    public void setAlpha(int i) {
            mPaint.setAlpha(i);//设置画笔的透明度
    }
    @Override //设置画笔的ColorFilter
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
          mPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
    @Override
    public int getIntrinsicWidth() {
        return mWidth;
    }
    @Override
    public int getIntrinsicHeight() {
        return mWidth;
    }
}


  ImageView iv_name = findViewById(R.id.iv_name);
        CircleDrawable circleDrawable = new CircleDrawable(BitmapFactory.decodeResource(getResources(),R.drawable.th));
        iv_name.setImageDrawable(circleDrawable);
    public class Rect1Drawable extends Drawable {
    private Paint mPaint;
    private Bitmap mBitmap;
    private RectF rect;
    private final BitmapShader bitmapShader;

    public Rect1Drawable(Bitmap mBitmap) {
        this.mBitmap = mBitmap;
        //着色器
        bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(bitmapShader);

    }
    @Override
    public void draw(@NonNull Canvas canvas) {
        canvas.drawRoundRect(rect,60,60,mPaint);
    }


    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        rect=new RectF(left, top, right, bottom);
    }

    @Override
    public void setAlpha(int i) {
            mPaint.setAlpha(i);//设置画笔的透明度
    }
    @Override //设置画笔的ColorFilter
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
          mPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
    @Override
    public int getIntrinsicWidth() {
        return mBitmap.getWidth();
    }
    @Override
    public int getIntrinsicHeight() {
        return mBitmap.getHeight();
    }
}

你可能感兴趣的:(Drawable)