自定义图片形状

自定义图片形状(通过ShapeDrawable):

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.PathShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.view.View;

public class ShapeView extends View{
    private Paint paint;
    private BitmapShader bitmapShader;
    private Bitmap bitmap;
    private ShapeDrawable shapeDrawable;
    private int centerx,centery,radius;
    /** 默认类型为圆形*/
    private int type=CIRCLE_BITMAP;
    private Path path;
    /** 外矩形 圆角半径 */
    private float[] outerR;
    /** 内部矩形与外部矩形的距离 */
    private RectF inset;
    /** 内矩形 圆角半径 */
    private float[] innerRadii = {20, 20, 20, 20, 20, 20, 20, 20};
    /** 圆形图片*/
    public static final int CIRCLE_BITMAP=11;
    /** 矩形图片*/
    public static final int RECT_BITMAP=12;
    /** 椭圆图片*/
    public static final int OVAL_BITMAP=13;
    /** 圆角图片*/
    public static final int ROUNDRECT_BITMAP=14;
    /** 弧形图片*/
    public static final int ARC_BITMAP=15;
    /** path路径图片*/
    public static final int PATH_BITMAP=16;
    public ShapeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public ShapeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ShapeView(Context context) {
        super(context);
    }
    /**
     * 设置图片类型
     * @param type
     */
    public void SetType(int type){
        this.type=type;
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        centerx=w/2;
        centery=h/2;
        radius=Math.min(centerx,centery);
        paint=new Paint();
        bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.shape)).getBitmap();
        // 构造渲染器BitmapShader  
        bitmapShader=new android.graphics.BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    }
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        switch (type) {
        case CIRCLE_BITMAP:
//          paint.setShader(bitmapShader);
//          canvas.drawCircle(centerx,centery, radius, paint);
            shapeDrawable=new ShapeDrawable(new OvalShape());//添加椭圆模型
            shapeDrawable.setBounds(0, 0, radius*2, radius*2);//设置显示区域 为圆形
            break;
        case RECT_BITMAP:
            shapeDrawable=new ShapeDrawable(new RectShape());//添加矩形模型
            shapeDrawable.setBounds(0, 0, radius*2, radius*2);//设置显示区域 为圆形
            break;
        case OVAL_BITMAP:
            shapeDrawable=new ShapeDrawable(new OvalShape());//添加椭圆模型
            shapeDrawable.setBounds(0, 0, centerx*2, centery*2);//设置显示区域 为矩形
            break;
        case ROUNDRECT_BITMAP:
            //外矩形 左上、右上、右下、左下 圆角半径 
            outerR = new float[] { 50, 50, 50, 50, 50, 50, 50, 50};
            //内矩形距外矩形,左上角x,y距离, 右下角x,y距离  , 内部矩形与外部矩形的距离 
            inset = new RectF(150,150, 200,200); 
            shapeDrawable=new ShapeDrawable(new RoundRectShape(outerR,null, null));//添加圆角模型
//          shapeDrawable=new ShapeDrawable(new RoundRectShape(outerR,inset, innerRadii));//添加圆角模型
            shapeDrawable.setBounds(0, 0, centerx*2, centery*2);//设置显示区域 为矩形
            break;
        case ARC_BITMAP:
            shapeDrawable=new ShapeDrawable(new ArcShape(135, 270));//添加弧形模型 (135:开始角度,270:扫描角度)
            shapeDrawable.setBounds(0, 0, radius*2, radius*2);//设置显示区域 为圆形
            break;
        case PATH_BITMAP:
            //绘制一个顶点为下列四个点的棱形  
            path = new Path();  
            path.moveTo(50, 0);  
            path.lineTo(0, 50);  
            path.lineTo(50, 100);  
            path.lineTo(100, 50); 
            shapeDrawable=new ShapeDrawable(new PathShape(path, 100, 200));//添加几何路径 // 100, 200 最终呈现出的坐标值(实际内部是缩放的canvas)
            shapeDrawable.setBounds(0, 0, radius*2, radius*2);//设置显示区域 为圆形
            break;
        default:
            break;
        }
        //得到画笔并设置渲染器    
        shapeDrawable.getPaint().setShader(bitmapShader);
        //绘制shapeDrawable  
        shapeDrawable.draw(canvas);
    }
}

关于shape:

有位盆友写的挺详细,有兴趣的可以看看:http://blog.csdn.net/jjwwmlp456/article/details/46928859

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值