画布之ShapeDrawable

Posted loaderman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了画布之ShapeDrawable相关的知识,希望对你有一定的参考价值。

package com.loaderman.customviewdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.ShapeDrawable;
import android.util.AttributeSet;
import android.view.View;


public class ShapeView extends View {
    private ShapeDrawable mShapeDrawable;
    public ShapeView(Context context) {
        super(context);
        init();
    }

    public ShapeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ShapeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init(){
        setLayerType(LAYER_TYPE_SOFTWARE,null);
//        //RectShape
//        mShapeDrawable = new ShapeDrawable(new RectShape());
//        mShapeDrawable.setBounds(new Rect(50,50,200,100));
//        mShapeDrawable.getPaint().setColor(Color.YELLOW);

//        //OvalShape
//        mShapeDrawable = new ShapeDrawable(new OvalShape());
//        mShapeDrawable.setBounds(new Rect(50,50,200,100));
//        mShapeDrawable.getPaint().setColor(Color.YELLOW);

//       //ArcShape 15968145727
//        mShapeDrawable = new ShapeDrawable(new ArcShape(0,300));
//        mShapeDrawable.setBounds(new Rect(50,50,200,100));
//        mShapeDrawable.getPaint().setColor(Color.YELLOW);

//        //RoundRectShape
//        float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
//        RectF inset = new RectF(6, 6, 6,6);
//        float[] innerR = new float[] { 50, 12, 0, 0, 12, 50, 0, 0 };
//        mShapeDrawable = new ShapeDrawable(new RoundRectShape(outerR,inset,innerR));
//        mShapeDrawable.setBounds(new Rect(50,50,200,100));
//        mShapeDrawable.getPaint().setColor(Color.BLACK);

//        //PathShape
//        Path path = new Path();
//        path.moveTo(0,0);
//        path.lineTo(100,0);
//        path.lineTo(100,100);
//        path.lineTo(0,100);
//        // 封闭前面点所绘制的路径
//        path.close();
//        mShapeDrawable = new ShapeDrawable(new PathShape(path,100,200));
//        mShapeDrawable.setBounds(new Rect(0,0,250,150));
//        mShapeDrawable.getPaint().setColor(Color.YELLOW);

        //ReginShape
        //构造两个矩形
        Rect rect1 = new Rect(50,0,90,150);
        Rect rect2 = new Rect(0,50,250,100);
        //构造两个Region
        Region region = new Region(rect1);
        Region region2= new Region(rect2);
        //取两个区域的交集
        region.op(region2, Region.Op.XOR);
        mShapeDrawable = new ShapeDrawable(new RegionShape(region));
        mShapeDrawable.setBounds(new Rect(0,0,200,100));
        mShapeDrawable.getPaint().setColor(Color.YELLOW);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mShapeDrawable.draw(canvas);
    }
}
package com.loaderman.customviewdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class TelescopeView extends View {
    private Bitmap bitmap;
    private ShapeDrawable drawable;
// 放大镜的半径

    private static final int RADIUS = 80;
// 放大倍数

    private static final int FACTOR = 3;
    private final Matrix matrix = new Matrix();

    public TelescopeView(Context context) {
        super(context);
        init();
    }

    public TelescopeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TelescopeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }


    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        // 这个位置表示的是,画shader的起始位置
        matrix.setTranslate(RADIUS - x * FACTOR, RADIUS - y * FACTOR);
        drawable.getPaint().getShader().setLocalMatrix(matrix);

        // bounds,就是那个圆的外切矩形
        drawable.setBounds(x - RADIUS, y - RADIUS, x + RADIUS, y + RADIUS);
        invalidate();
        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (bitmap == null) {
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.scenery);
            bitmap = Bitmap.createScaledBitmap(bmp, getWidth(), getHeight(), false);

            BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(bitmap,
                    bitmap.getWidth() * FACTOR, bitmap.getHeight() * FACTOR, true),
                    Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            drawable = new ShapeDrawable(new OvalShape());
            drawable.getPaint().setShader(shader);
            drawable.setBounds(0, 0, RADIUS * 2, RADIUS * 2);
        }

        canvas.drawBitmap(bitmap, 0, 0, null);
        drawable.draw(canvas);


    }

}

效果:

技术分享图片

以上是关于画布之ShapeDrawable的主要内容,如果未能解决你的问题,请参考以下文章

LayaAir之小游戏开放域开发

在画布上绘制片段视图

javascript 有用的片段关于画布

如何使用 ShapeDrawable 创建可点击按钮?

如何删除画布周围的灰色边框

使用 Android ShapeDrawable 以编程方式绘制圆